Skip to content

Fix System.IO.File/Directory property functions with relative paths in -mt mode - #13239

Merged
JanProvaznik merged 3 commits into
dotnet:mainfrom
JanProvaznik:propfn-mt
Mar 18, 2026
Merged

JanProvaznik merged 3 commits into
dotnet:mainfrom
JanProvaznik:propfn-mt

Conversation

@JanProvaznik

@JanProvaznik JanProvaznik commented Feb 11, 2026 •

Copy link
Copy Markdown
Member

Summary

Property functions like $([System.IO.File]::Exists('relative.txt')) and $([System.IO.File]::ReadAllText('data.txt')) resolved relative paths against Environment.CurrentDirectory (process-global), which produces wrong results in -mt mode where multiple projects build concurrently on different threads sharing the same process.

This change resolves relative path arguments for System.IO.File and System.IO.Directory property functions against FrameworkFileUtilities.CurrentThreadWorkingDirectory (the thread-local working directory set by MultiThreadedTaskEnvironmentDriver), matching the existing behavior of Path.GetFullPath which was already intercepted in WellKnownFunctions.

In multiprocess mode, CurrentThreadWorkingDirectory is null and the resolution is a no-op — zero behavior change.

Changes

src/Framework/FileUtilities.cs

  • Add MakeFullPathFromThreadWorkingDirectory(string path) helper next to other AbsolutePath helpers (NormalizePath, FixFilePath).
  • Uses Path.IsPathFullyQualified to skip already-absolute paths and Path.GetFullPath(path, basePath) (2-arg overload) for correct handling of drive-relative paths (\foo, C:foo) on Windows.
  • Falls back to Path.Combine for invalid paths (wildcards, illegal chars) to preserve the non-throwing behavior of File.Exists/Directory.Exists.
  • Callers must call FixFilePath before passing paths (documented in XML doc).

src/Build/Evaluation/Expander.cs

  • Add IsFileOrDirectoryPathArgument(string methodName, int argIndex) to identify which arguments are file/directory paths (arg0 = always a path, arg1 = dest for Copy/Move/Replace, arg2 = backup for Replace). CreateSymbolicLink arg1 is intentionally excluded — its target path is semantically relative.
  • Add path resolution in Function.Execute() arg assembly loop for System.IO.File and System.IO.Directory receiver types. Resolution happens after UnescapeAll so that the working directory path (a real filesystem path) is not corrupted by MSBuild %xx unescape processing.
  • Fix pre-existing bug: Type comparisons used typeof(Directory) which resolves to Microsoft.IO.Directory on net472 due to FEATURE_MSIOREDIST alias, but _receiverType from AvailableStaticMethods is always System.IO.Directory. Changed to fully-qualified typeof(System.IO.Directory) etc.

src/Shared/FileUtilities.cs

  • Removed duplicate CurrentThreadWorkingDirectory and MakeFullPathFromThreadWorkingDirectory that were incorrectly added here. Shared files are compiled into multiple binaries, creating separate static fields — the canonical AsyncLocal lives in FrameworkFileUtilities.

src/Build.UnitTests/Evaluation/Expander_Tests.cs

  • 30 unit tests covering File methods, Directory methods, extended methods (MSBUILDENABLEALLPROPERTYFUNCTIONS=1), absolute path passthrough, multi-path methods (Copy, Move), parent traversal, and regular (non-mt) mode.
  • Wired ITestOutputHelper to all TestEnvironment.Create(_output) calls per repo conventions.
  • FileGetAttributes test marked [WindowsOnlyFact] (Archive attribute is Windows-specific).

Design Notes

  • Path.GetFullPath was already handled for -mt mode in WellKnownFunctions.cs — this PR extends the same pattern to all File/Directory property functions.
  • The guard string.IsNullOrEmpty(CurrentThreadWorkingDirectory) ensures zero cost in multiprocess mode (one AsyncLocal read + null check -> return null -> skip).
  • Item functions share the same Function.Execute() codepath, so they are covered automatically.

Fixes #13237

Copilot AI review requested due to automatic review settings February 11, 2026 13:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes MSBuild property function evaluation in multi-threaded (-mt) builds by resolving relative path arguments for System.IO.File/System.IO.Directory against MSBuild’s thread-local working directory rather than the process-global Environment.CurrentDirectory, aligning behavior with existing Path.GetFullPath interception.

Changes:

  • Added FileUtilities.MakeFullPathFromThreadWorkingDirectory to resolve relative paths using the thread-local working directory.
  • Updated Expander.Function<T>.Execute() to apply thread-local relative-path resolution for System.IO.File/System.IO.Directory path arguments and fixed receiver type comparisons to use System.IO.* explicitly.
  • Added extensive unit tests validating correct resolution behavior (including .. traversal and backslash normalization cases).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
src/Shared/FileUtilities.cs Introduces helper to resolve relative paths against CurrentThreadWorkingDirectory.
src/Build/Evaluation/Expander.cs Applies the helper during property function argument assembly and corrects System.IO.* type comparisons.
src/Build.UnitTests/Evaluation/Expander_Tests.cs Adds coverage for relative/absolute path behavior across File/Directory APIs in -mt and non--mt modes.

Comment thread src/Build/Evaluation/Expander.cs Resolved
Comment thread src/Shared/FileUtilities.cs Resolved
Comment thread src/Shared/FileUtilities.cs Resolved
…n -mt mode

Property functions like $([System.IO.File]::Exists('relative.txt')) resolved
relative paths against Environment.CurrentDirectory (process-global), which is
wrong in -mt mode where multiple projects build concurrently on different threads.

This change resolves relative path arguments for File/Directory property functions
against FileUtilities.CurrentThreadWorkingDirectory (the thread-local working
directory set by the multithreaded task environment driver).

Changes:
- Add FileUtilities.MakeFullPathFromThreadWorkingDirectory helper that resolves
  relative paths using AbsolutePath + NormalizePath, with FixFilePath called before
  resolution (critical on Linux where backslash is a valid filename character)
- Add path resolution in Function<T>.Execute() arg assembly loop for File/Directory
  receiver types, covering all methods generically (both default-allowed and extended)
- Fix pre-existing bug: type comparisons in arg assembly loop used 	ypeof(Directory)
  which resolves to Microsoft.IO.Directory on net472 due to FEATURE_MSIOREDIST alias,
  but _receiverType from AvailableStaticMethods is always System.IO.Directory.
  Changed to fully-qualified 	ypeof(System.IO.Directory) etc.
- Add 38 unit tests covering File methods, Directory methods, extended methods,
  absolute path passthrough, multi-path methods, parent traversal, and regular mode

Fixes dotnet#13237
Comment thread src/Shared/FileUtilities.cs Resolved
Comment thread src/Shared/FileUtilities.cs Resolved
Comment thread src/Shared/FileUtilities.cs Resolved
Comment thread src/Framework/FileUtilities.cs Resolved
…ed, UnescapeAll ordering, tests

- Move MakeFullPathFromThreadWorkingDirectory and CurrentThreadWorkingDirectory
  usage to FrameworkFileUtilities (src/Framework/FileUtilities.cs) so state is
  shared across all MSBuild binaries instead of duplicated per-compilation-unit
- Remove CLR2COMPATIBILITY guards (Shared/FileUtilities.cs not compiled into TaskHost)
- Use Path.IsPathFullyQualified instead of IsPathRooted to handle drive-relative paths
- Use Path.GetFullPath(path, basePath) 2-arg overload for correct resolution
- Place MT resolution AFTER UnescapeAll to avoid corrupting working directory paths
- Remove CreateSymbolicLink from arg1 resolution (target path is semantically relative)
- Add ITestOutputHelper to tests, WindowsOnlyFact for FileGetAttributes
- Remove double FixFilePath call (caller already normalizes)
- Add clarifying comment for missing File alias in FEATURE_MSIOREDIST block

Co-authored-by: Copilot <[email protected]>
Comment thread src/Build/Evaluation/Expander.cs Resolved
Comment thread src/Build/Evaluation/Expander.cs Resolved
Resolve merge conflicts in src/Framework/FileUtilities.cs:
- Using directives: Accept upstream's NewPath/Path alias structure
- Methods: Keep both our MakeFullPathFromThreadWorkingDirectory and
  upstream's new methods (GetHexHash, GetPathsHash, etc.)

Post-merge fixes:
- Use NewPath instead of Path for IsPathFullyQualified/GetFullPath
  (upstream changed Path = System.IO.Path, which lacks these on net472)
- Update FrameworkFileUtilities -> FileUtilities (upstream moved
  Shared/FileUtilities.cs into Framework/FileUtilities.cs)
- Remove AggressiveInlining from IsFileOrDirectoryPathArgument
  (per rainersigwald review comment)

Co-authored-by: Copilot <[email protected]>
@JanProvaznik
JanProvaznik merged commit 960ca3c into dotnet:main Mar 18, 2026
10 checks passed
AR-May pushed a commit to AR-May/msbuild that referenced this pull request Mar 19, 2026
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.

Property functions using System.IO.File with relative paths break in -mt mode due to shared Environment.CurrentDirectory

4 participants