Repository navigation
Fix System.IO.File/Directory property functions with relative paths in -mt mode - #13239
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
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.MakeFullPathFromThreadWorkingDirectoryto resolve relative paths using the thread-local working directory. - Updated
Expander.Function<T>.Execute()to apply thread-local relative-path resolution forSystem.IO.File/System.IO.Directorypath arguments and fixed receiver type comparisons to useSystem.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. |
JanProvaznik
force-pushed
the
propfn-mt
branch
from
February 11, 2026 13:21
f361efb to
6f927f8
Compare
…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
…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]>
rainersigwald
approved these changes
Mar 11, 2026
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]>
This was referenced Mar 18, 2026
AR-May
pushed a commit
to AR-May/msbuild
that referenced
this pull request
Mar 19, 2026
…n -mt mode (dotnet#13239) Co-authored-by: Copilot <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Property functions like
$([System.IO.File]::Exists('relative.txt'))and$([System.IO.File]::ReadAllText('data.txt'))resolved relative paths againstEnvironment.CurrentDirectory(process-global), which produces wrong results in-mtmode where multiple projects build concurrently on different threads sharing the same process.This change resolves relative path arguments for
System.IO.FileandSystem.IO.Directoryproperty functions againstFrameworkFileUtilities.CurrentThreadWorkingDirectory(the thread-local working directory set byMultiThreadedTaskEnvironmentDriver), matching the existing behavior ofPath.GetFullPathwhich was already intercepted inWellKnownFunctions.In multiprocess mode,
CurrentThreadWorkingDirectoryis null and the resolution is a no-op — zero behavior change.Changes
src/Framework/FileUtilities.csMakeFullPathFromThreadWorkingDirectory(string path)helper next to otherAbsolutePathhelpers (NormalizePath,FixFilePath).Path.IsPathFullyQualifiedto skip already-absolute paths andPath.GetFullPath(path, basePath)(2-arg overload) for correct handling of drive-relative paths (\foo,C:foo) on Windows.Path.Combinefor invalid paths (wildcards, illegal chars) to preserve the non-throwing behavior ofFile.Exists/Directory.Exists.FixFilePathbefore passing paths (documented in XML doc).src/Build/Evaluation/Expander.csIsFileOrDirectoryPathArgument(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).CreateSymbolicLinkarg1 is intentionally excluded — its target path is semantically relative.Function.Execute()arg assembly loop forSystem.IO.FileandSystem.IO.Directoryreceiver types. Resolution happens afterUnescapeAllso that the working directory path (a real filesystem path) is not corrupted by MSBuild%xxunescape processing.typeof(Directory)which resolves toMicrosoft.IO.Directoryon net472 due toFEATURE_MSIOREDISTalias, but_receiverTypefromAvailableStaticMethodsis alwaysSystem.IO.Directory. Changed to fully-qualifiedtypeof(System.IO.Directory)etc.src/Shared/FileUtilities.csCurrentThreadWorkingDirectoryandMakeFullPathFromThreadWorkingDirectorythat were incorrectly added here. Shared files are compiled into multiple binaries, creating separate static fields — the canonicalAsyncLocallives inFrameworkFileUtilities.src/Build.UnitTests/Evaluation/Expander_Tests.csMSBUILDENABLEALLPROPERTYFUNCTIONS=1), absolute path passthrough, multi-path methods (Copy, Move), parent traversal, and regular (non-mt) mode.ITestOutputHelperto allTestEnvironment.Create(_output)calls per repo conventions.FileGetAttributestest marked[WindowsOnlyFact](Archiveattribute is Windows-specific).Design Notes
Path.GetFullPathwas already handled for -mt mode inWellKnownFunctions.cs— this PR extends the same pattern to allFile/Directoryproperty functions.string.IsNullOrEmpty(CurrentThreadWorkingDirectory)ensures zero cost in multiprocess mode (one AsyncLocal read + null check -> return null -> skip).Function.Execute()codepath, so they are covered automatically.Fixes #13237