Skip to content

[patch] Compute RelativePath.Make by comparing paths, not URIs - #413

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/315-relative-path-no-unescape
Oct 9, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/315-relative-path-no-unescape

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #315

What was wrong

SemanticRelativePath.Make built System.Uri values from file-system paths and passed the result through Uri.UnescapeDataString. On Linux, macOS and Windows alike, % is an ordinary file-name character, so a literal percent sequence in a name got decoded. A folder named %2E%2E came back as .., which escapes the base directory. The netstandard2.0 fallback PathPolyfill.GetRelativePath used the same Uri approach.

Change

  • Make (Semantics.Paths/SemanticRelativePath.cs) now calls PathPolyfill.GetRelativePath, which is Path.GetRelativePath on every target except netstandard2.0. Existing behaviour is kept in two ways:

    • a from that is not an IDirectoryPath is still resolved from the directory that contains it, as the Uri version did
    • separators are still normalised to /
  • The netstandard2.0 fallback is now GetRelativePathBySegments. It does a full-path, segment-by-segment comparison:

    • matching is case-insensitive on Windows and macOS, as Path.GetRelativePath is
    • it returns path when the roots differ
    • it returns . when both paths name the same directory

    It is compiled on every target so it can be tested against Path.GetRelativePath. The fallback is no longer Uri-based, so the now-unused AppendDirectorySeparatorChar helper is removed.

Related: the new fallback also removes both netstandard2.0 failures described in #385: a file target no longer gains a trailing separator, and a directory relative to itself is . rather than "". I left #385 open because the test project only runs on net10.0, so that target is covered by the equivalence test below rather than by a test running on netstandard2.0.

Behaviour change on net8+: Make from a directory to itself now returns . instead of an empty path, matching Path.GetRelativePath and the sibling GetRelativePathTo/AsRelative APIs.

Tests (Semantics.Test/Paths/RelativePathMakeTests.cs)

  • Make_KeepsPercentSequencesInNamesAsWritten: the four rows from the issue plus 50%20off. The result equals the name as written, and resolving it against from gives to. The four issue rows failed before the fix.
  • Make_FromAFile_IsRelativeToItsDirectory: pins the existing non-directory from behaviour.
  • SegmentFallback_MatchesPathGetRelativePath: eight cases (percent names, file target, child directory, self, sibling subtree, ancestor, a space in a name, from the root). The new fallback matches Path.GetRelativePath on all eight. Swapping the old Uri-based logic in temporarily made all eight fail.
  • Full Semantics.Test on net10.0: 1494 passed, 8 skipped, 0 failed. Semantics.Paths builds for all five TFMs with the local Sonar analyzers, with no findings on the changed files.

🤖 Generated with Claude Code

https://claude.ai/code/session_01A5MkmzQ4qxxRmHStdQ8oJA


Generated by Claude Code

Make built System.Uri values from file-system paths and unescaped the result, so a
literal percent sequence in a name was decoded: a folder named "%2E%2E" came back as
"..", escaping the base directory. Make now uses Path.GetRelativePath (through
PathPolyfill), resolving a non-directory "from" from the directory that contains it
as before. The netstandard2.0 fallback, which had the same Uri flaw, now compares
path segments, so it also stops adding a trailing separator to file targets and
returns "." for a directory relative to itself.

Fixes #315

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A5MkmzQ4qxxRmHStdQ8oJA
Comment thread Semantics.Test/Paths/RelativePathMakeTests.cs Fixed
Comment thread Semantics.Test/Paths/RelativePathMakeTests.cs Fixed
Comment thread Semantics.Test/Paths/RelativePathMakeTests.cs Fixed
Path.Combine drops earlier arguments when a later one is rooted, which would
mask a rooted result from Make. Path.Join keeps every segment.

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

sonarqubecloud Bot commented Oct 9, 2026

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.

RelativePath.Make decodes percent-escapes in real file names, so a folder named %2E%2E comes back as ..

2 participants