Skip to content

RemoveExtension() on a dotfile (.bashrc, .gitignore) returns the parent directory, or /, as a file path #325

Closed

Description

@matt-edmondson

What's wrong

AbsoluteFilePath.RemoveExtension() (Semantics.Paths/Implementations/AbsoluteFilePath.cs around lines 76–79) and the same method on RelativeFilePath do this:

string pathWithoutExtension = Path.ChangeExtension(WeakString, null) ?? "";
return Create<AbsoluteFilePath>(pathWithoutExtension);

.NET treats a leading-dot file name as consisting entirely of an extension. So Path.ChangeExtension("/home/user/.bashrc", null) returns "/home/user/". SemanticPath.MakeCanonical then strips the trailing separator, and the result is the parent directory, typed as a file path.

Repro (net10.0, Linux)

Call Result
AbsoluteFilePath.Create("/home/user/.bashrc").RemoveExtension() "/home/user", the same as its own AbsoluteDirectoryPath
AbsoluteFilePath.Create("/.profile").RemoveExtension() "/", the filesystem root as a file
RelativeFilePath.Create("src/.editorconfig").RemoveExtension() "src"
RelativeFilePath.Create(".gitignore").RemoveExtension() ""
ChangeExtension(".bak") on /home/user/.bashrc "/home/user/.bak", which renames the file instead of adding an extension

Why it matters

Code such as File.Move(path, path.RemoveExtension()) or a cleanup that deletes path.RemoveExtension() silently targets a directory. The strong typing is supposed to rule out exactly this kind of mistake.

Suggested fix

Operate on the file name only. If Path.GetFileName has no . after index 0, RemoveExtension() returns the path unchanged and ChangeExtension appends the new extension. Apply the same dotfile rule to the FileExtension/FullFileExtension getters that #287 is deciding, so all extension members agree.

Acceptance: tests cover /home/user/.bashrc, /.profile, .gitignore and src/.editorconfig for both RemoveExtension and ChangeExtension, on absolute and relative file paths. RemoveExtension returns a path whose file name is unchanged, and ChangeExtension(".bak") yields .bashrc.bak.

Activity

  1. added
    bug
    on Sep 29, 2026
  2. matt-edmondson commented on Sep 29, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: High. RemoveExtension() on a dotfile silently returns the parent directory, or /, typed as a file path. ChangeExtension renames the file instead of adding an extension. Any move or delete built on the result targets the wrong filesystem object. That is exactly the kind of mistake the strongly typed paths are meant to prevent.
    • Area / suggested assignment: Semantics.Paths (AbsoluteFilePath / RelativeFilePath RemoveExtension and ChangeExtension)
    • Duplicates: none. Closely related to FileExtension/FullFileExtension throw ArgumentException for any path with a dot in a directory name ("./notes.txt", "/home/u/.config/a.json") #287 (dotfile semantics for FileExtension/FullFileExtension). Settle one dotfile rule and apply it to every extension member, ideally in one PR.
    • In progress: no matching open PR.

    Suggested next step: work on Path.GetFileName only, and treat a name whose only . is at index 0 as having no extension. Cover the four repro paths on both absolute and relative types.


    Generated by Claude Code

  3. added a commit that references this issue on Oct 14, 2026
    0f0725a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions