Skip to content

Directory Depth counts separator characters: "/" and "/home" both have depth 1, and "a" (0) and "./a" (1) differ although they name the same directory #466

Closed

Description

@matt-edmondson

What's wrong

AbsoluteDirectoryPath.Depth (Semantics.Paths/Implementations/AbsoluteDirectoryPath.cs:80-98) and RelativeDirectoryPath.Depth (RelativeDirectoryPath.cs:84-100) count every / or \ character in the string:

for (int i = 0; i < span.Length; i++)
{
    if (span[i] == Path.DirectorySeparatorChar || span[i] == Path.AltDirectorySeparatorChar)
    {
        depth++;

The root's own separator therefore counts as one level, and . and .. segments count like real directories.

Observed (ktsu.Semantics.Paths 5.12.1; the code is unchanged on main)

Path Depth Expected
/ 1 0
/home 1 1
C:\ (Windows) 1 0
C:\Users (Windows) 1 1
a (relative) 0 0
./a (relative) 1 0 (same directory as a)
a/.. (relative) 1 the starting directory

Consequences:

  • x.Parent.Depth < x.Depth does not hold when x is a child of the root.
  • Sorting or comparing by depth, as PathIntegrationTests.DirectoryDepth_Comparison does, puts the root level with its own children.
  • Two spellings of the same relative directory report different depths.

Suggested fix / acceptance criteria

  • Count non-empty segments after the root, skipping . and resolving .. lexically. The root then has depth 0 and /home has depth 1.
  • For relative paths, compute depth from the normalised form. Keep the current convention that a is 0 and a/b is 1, so existing tests that rely on it still pass.
  • Add tests for the root, a child of the root, a trailing separator (/home/), ./a, and a/.., on both POSIX and Windows-style roots.

Related but distinct: #446 (Parent and GetAncestors ignore . and ..).

Activity

  1. added
    bug
    on Oct 18, 2026
  2. matt-edmondson commented on Oct 18, 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