Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Framework.UnitTests/FileUtilities_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,35 @@ public void RelativePathMaybeAdjustFilePathWithBaseDirectory()
}
}

/// <summary>
/// MaybeAdjustFilePath should correctly convert backslashes to forward slashes
/// when CurrentThreadWorkingDirectory is set and baseDirectory is omitted.
/// </summary>
[UnixOnlyFact]
public void MaybeAdjustFilePath_UsesCurrentThreadWorkingDirectory_WhenBaseDirectoryOmitted()
{
string filePath = ObjectModelHelpers.CreateFileInTempProjectDirectory("obj/Debug/file.txt", string.Empty);
string projectDirectory = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(filePath)));
string unrelatedDirectory = Path.GetTempPath();
string oldCWD = Directory.GetCurrentDirectory();

try
{
// Set process CWD somewhere unrelated (simulating stale CWD in MT mode)
Directory.SetCurrentDirectory(unrelatedDirectory);

// Simulate MT mode: set thread-local working directory to the project dir
FileUtilities.CurrentThreadWorkingDirectory = projectDirectory;
FileUtilities.MaybeAdjustFilePath("obj\\Debug\\file.txt").ShouldBe("obj/Debug/file.txt");
}
finally
{
FileUtilities.CurrentThreadWorkingDirectory = null;
Directory.SetCurrentDirectory(oldCWD);
File.Delete(filePath);
}
}

private static string SystemSpecificAbsolutePath => typeof(BuildEnvironmentHelper).GetAssemblyPath();

[Fact]
Expand Down
7 changes: 7 additions & 0 deletions src/Framework/FileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,13 @@ internal static bool LooksLikeUnixFilePath(ReadOnlySpan<char> value, string base
return false;
}

// In MT mode the process CWD should not be used when resolving the first relative path segment. Use the
// thread-local working directory so the directory existence heuristic runs against the correct project directory.
if (string.IsNullOrEmpty(baseDirectory))
{
baseDirectory = CurrentThreadWorkingDirectory ?? "";
}
Comment thread
AR-May marked this conversation as resolved.

// The first slash will either be at the beginning of the string or after the first directory name
int directoryLength = value.Slice(1).IndexOf('/') + 1;
bool shouldCheckDirectory = directoryLength != 0;
Expand Down