Skip to content

DirectoryPath.Parent still treats . and .. as directory names: the parent of /home/user/.. is /home/user, and the parent of .. is "" #478

Closed

Description

@matt-edmondson

What's wrong

DirectoryPath.Parent calls Path.GetDirectoryName on the raw string (Semantics.Paths/Primitives/DirectoryPath.cs#L16-L23):

string? parentPath = Path.GetDirectoryName(WeakString);
return Create<DirectoryPath>(parentPath ?? "");

SemanticPath.MakeCanonical only normalises separators and trailing slashes, so . and .. segments survive and are dropped as if they were ordinary names.

The same bug was already fixed for RelativeDirectoryPath in #387, which normalises first, and for AbsoluteDirectoryPath in #446, which resolves with Path.GetFullPath first. Neither fix covers the polymorphic DirectoryPath primitive. That primitive is also the type returned by SemanticFilePath.DirectoryPath and by the default SemanticDirectoryPath.GetContents().

Reproduction (net10.0, Linux, observed)

DirectoryPath.Create<DirectoryPath>("/home/user/..").Parent; // "/home/user"  expected "/"   (the path names /home)
DirectoryPath.Create<DirectoryPath>("/home/user/.").Parent;  // "/home/user"  expected "/home"
DirectoryPath.Create<DirectoryPath>("a/..").Parent;          // "a"           expected ".."  (currently a child of the path itself)
DirectoryPath.Create<DirectoryPath>("..").Parent;            // ""            expected "../.."
DirectoryPath.Create<DirectoryPath>("a/./b").Parent;         // "a/."         expected "a"

Code that walks upwards from a DirectoryPath visits the wrong directories. For a/.. it moves down into a.

Suggested fix

  • Branch on whether the path is rooted:
    • rooted: resolve it, as AbsoluteDirectoryPath.Parent now does;
    • relative: normalise lexically and drop or append .., as RelativeDirectoryPath.ComputeParent does.
  • Ideally, delegate to those two implementations so the three cannot drift apart again.
  • Keep the documented "empty path for a root" behaviour, or align it with AbsoluteDirectoryPath, but document whichever is chosen.

Acceptance criteria

  • All five inputs above return the resolved parent.
  • Tests sit next to Semantics.Test/Paths/AbsoluteDirectoryPathParentTests.cs.

Activity

  1. added
    bug
    on Oct 20, 2026
  2. matt-edmondson commented on Oct 20, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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