Skip to content

Resolve . and .. before taking a relative directory's parent [patch] - #423

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/387-relative-parent-dotdot
Oct 13, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/387-relative-parent-dotdot

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #387

Problem

RelativeDirectoryPath.Parent used Path.GetDirectoryName, which strips the last segment as text and does not know what . or .. mean. For paths ending in either, Parent named a child of the path or the path itself:

path old Parent
.. ""
. ""
a/.. a

As a result, an upward walk built on Parent could move downward or loop forever.

Change

Parent now:

  1. normalizes lexically with the ..-preserving Normalize from RelativeDirectoryPath.Normalize() drops leading "..": "../sibling" becomes "sibling", so the path points somewhere else #288,
  2. drops the last segment when it is a named directory,
  3. appends .. when there is nothing to drop, either because the path is empty or ., or because it consists only of .. segments.
path new Parent
.. ../..
../.. ../../..
. / "" ..
a/.. ..
a "" (unchanged)
a/b a (unchanged)

Behaviour change to note: Parent of the empty path is now .. instead of "". The empty path names the starting directory, so "" claimed to be its own parent. This is what the issue's "append .." rule asks for, and it keeps the property below true. No code in this repository walks RelativeDirectoryPath.Parent. The only upward walk is on AbsoluteDirectoryPath, which is unaffected.

Tests

In RelativePathPropertyTests:

  • RelativeDirectoryPath_Parent_ResolvesDotSegments is a table test covering the acceptance criteria, plus a/../.., ../a and a/./b.
  • RelativeDirectoryPath_Parent_AgreesWithAbsoluteParent checks that p.Parent.AsAbsolute(b) == p.AsAbsolute(b).Parent for the same ten paths.

Verification

  • With the fix reverted, 13 of the new rows fail. With the fix, all 44 tests in the class pass.
  • Semantics.Paths builds for every target framework, netstandard2.0 included, with 0 warnings.
  • The full Semantics.Test suite passes on net10.0: 1638 passed, 8 skipped.

#422 (empty left operand to /) touches neighbouring behaviour but different code. The two PRs are independent of each other.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NAMc9xXW1LFVHGFLp4SN2n


Generated by Claude Code

RelativeDirectoryPath.Parent stripped the last segment of the text with
Path.GetDirectoryName, so the "parent" of `..` was "", of `a/..` was `a`
and of `.` was "": a child of the path, or the path itself. Parent now
normalizes lexically first, drops the last named segment, and climbs one
more level when the path only names the start directory or above it.

Fixes #387

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NAMc9xXW1LFVHGFLp4SN2n
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RelativeDirectoryPath.Parent ignores .. and .: the "parent" of .. is "" and the parent of a/.. is a, a child of the path itself

2 participants