Skip to content

netstandard2.0: relative paths to files gain a trailing separator ("src/file.cs/") and a directory relative to itself is "" so AsAbsolute then throws #385

Closed

Description

@matt-edmondson

What's wrong

Semantics.Paths ships a netstandard2.0 target. On that target, PathPolyfill.GetRelativePath (Semantics.Paths/Polyfills.cs L19-46) is used instead of Path.GetRelativePath. The callers are:

  • AbsoluteDirectoryPath.GetRelativePathTo and AsRelative (Implementations/AbsoluteDirectoryPath.cs:255, :286)
  • AbsoluteFilePath.AsRelative (Implementations/AbsoluteFilePath.cs:122)
  • DirectoryPath (Primitives/DirectoryPath.cs:54) and FilePath (Primitives/FilePath.cs:33)

The fallback appends a directory separator to both paths before building Uris, then returns MakeRelativeUri verbatim. That gives two failures that #315 (percent-decoding) does not cover:

  1. A file target gets a trailing separator. src/file.cs comes back as src/file.cs/, because the target was turned into .../file.cs/ before the URI was built. Every AbsoluteFilePath.AsRelative(...) result on netstandard2.0 names a directory-shaped path. Directory results also carry a trailing / (src/) that Path.GetRelativePath never returns, so the same input gives different values and equality on different TFMs.
  2. A directory relative to itself is "", not ".". MakeRelativeUri of two identical URIs is empty, so the result is an empty RelativeDirectoryPath. Resolving it back with AsAbsolute(dir) reaches the netstandard2.0 PathPolyfill.GetFullPath fallback, which throws ArgumentException("Path cannot be empty.") (Polyfills.cs ~L105). On net8+ the same round trip works.

Cross-drive input on Windows (C:\a relative to D:\x) also looks wrong, since both file: URIs share a scheme, so the Scheme check does not catch it. That case is traced from MakeRelativeUri semantics and was not run.

Reproduction

The fallback logic was copied verbatim and run on .NET 10 (Linux) next to Path.GetRelativePath:

relativeTo path fallback Path.GetRelativePath
/home/u/proj /home/u/proj '' .
/home/u/proj /home/u/proj/src src/ src
/home/u/proj /home/u/proj/src/file.cs src/file.cs/ src/file.cs
/home/u/proj /home/u/other ../other/ ../other
/home/u/proj /home/u/my%41dir ../myAdir/ ../my%41dir (this row is #315)

The test project only targets modern .NET, so CI never runs this code path.

Suggested fix / acceptance criteria

  • Replace the Uri-based fallback with a segment-wise common-prefix walk:
    • Compare case-insensitively on Windows and ordinally elsewhere.
    • Return "." for equal paths.
    • Return path unchanged when Path.GetPathRoot differs.
    • Never add a trailing separator.
  • This also fixes RelativePath.Make decodes percent-escapes in real file names, so a folder named %2E%2E comes back as .. #315's netstandard2.0 half; the two could share a PR.
  • Add tests that run against the polyfill directly, since it is internal and InternalsVisibleTo already exists. That gets coverage without a netstandard2.0 test host. Assert that the polyfill matches Path.GetRelativePath for the rows above, plus a nested .. case.

Activity

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

    @matt-edmondson
    ContributorAuthor

    Triage

    Recommended next step: Rewrite PathPolyfill.GetRelativePath as a segment-wise common-prefix walk. Then add polyfill-direct tests through the existing InternalsVisibleTo, asserting it matches Path.GetRelativePath for the table rows, plus a nested .. case and the %2E%2E row from #315.


    Generated by Claude Code

  3. matt-edmondson commented on Oct 29, 2026

    @matt-edmondson
    ContributorAuthor

    Already fixed on main by #413 (c00d0cd, "Compute RelativePath.Make by comparing paths, not URIs"). On netstandard2.0, PathPolyfill.GetRelativePath now goes through GetRelativePathBySegments, a segment-wise common-prefix walk. It returns . for equal paths, returns path when the roots differ, and never adds a trailing separator. RelativePathMakeTests.SegmentFallback_MatchesPathGetRelativePath checks it against Path.GetRelativePath for every row in this issue's table, plus a nested .. case. Closing.


    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