Skip to content

Commit 2d2d813

Browse files
AR-MayJeremy Spedding
authored andcommitted
Refactor FileUtilities.cs and add methods for absolute paths. (dotnet#13079)
### Context PRs dotnet#12914 and dotnet#12868 showed that we need to be able to fix and normalize `AbsolutePath`. Corresponding methods for strings live in `FileUtilities.cs` that is a Shared file and cannot reference Framework classes like `AbsolutePath`. ### Changes Made - Moved some related functions to FrameworkFileUtilities class. - Added the needed functions for `AbsolutePath` ### Testing unit tests
1 parent 204393b commit 2d2d813

File tree

63 files changed

+353
-265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+353
-265
lines changed

src/Build.UnitTests/Construction/SolutionFile_OldParser_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2555,7 +2555,7 @@ public void AbsolutePathShouldHandleUriLikeRelativePathsOnUnix()
25552555

25562556
/// <summary>
25572557
/// Test to verify that the fix for issue #1769 works by directly testing
2558-
/// FileUtilities.FixFilePath integration in AbsolutePath.
2558+
/// FrameworkFileUtilities.FixFilePath integration in AbsolutePath.
25592559
/// This test simulates scenarios where intermediate path processing might
25602560
/// leave backslashes in the AbsolutePath on Unix systems.
25612561
/// </summary>

src/Build.UnitTests/Evaluation/Expander_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3455,7 +3455,7 @@ public void PropertyFunctionStaticMethodDirectoryNameOfFileAbove()
34553455

34563456
string result = expander.ExpandIntoStringAndUnescape(@"$([MSBuild]::GetDirectoryNameOfFileAbove($(StartingDirectory), $(FileToFind)))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance);
34573457

3458-
Assert.Equal(Microsoft.Build.Shared.FileUtilities.EnsureTrailingSlash(tempPath), Microsoft.Build.Shared.FileUtilities.EnsureTrailingSlash(result));
3458+
Assert.Equal(FrameworkFileUtilities.EnsureTrailingSlash(tempPath), FrameworkFileUtilities.EnsureTrailingSlash(result));
34593459

34603460
result = expander.ExpandIntoStringAndUnescape(@"$([MSBuild]::GetDirectoryNameOfFileAbove($(StartingDirectory), Hobbits))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance);
34613461

src/Build/BackEnd/Components/FileAccesses/FileAccessManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Build.BackEnd;
1010
using Microsoft.Build.Execution;
1111
using Microsoft.Build.Experimental.FileAccess;
12+
using Microsoft.Build.Framework;
1213
using Microsoft.Build.Shared;
1314
using Microsoft.Build.Shared.FileSystem;
1415

@@ -44,7 +45,7 @@ public void InitializeComponent(IBuildComponentHost host)
4445
{
4546
_scheduler = host.GetComponent(BuildComponentType.Scheduler) as IScheduler;
4647
_configCache = host.GetComponent(BuildComponentType.ConfigCache) as IConfigCache;
47-
_tempDirectory = FileUtilities.EnsureNoTrailingSlash(FileUtilities.TempFileDirectory);
48+
_tempDirectory = FrameworkFileUtilities.EnsureNoTrailingSlash(FileUtilities.TempFileDirectory);
4849
}
4950

5051
public void ShutdownComponent()

src/Build/BackEnd/Components/RequestBuilder/TargetUpToDateChecker.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ internal static bool IsAnyOutOfDate<T>(out DependencyAnalysisLogDetail dependenc
978978
// possibly the outputs list isn't actually the shortest list. However it always is the shortest
979979
// in the cases I've seen, and adding this optimization would make the code hard to read.
980980

981-
string oldestOutput = EscapingUtilities.UnescapeAll(FileUtilities.FixFilePath(outputs[0].ToString()));
981+
string oldestOutput = EscapingUtilities.UnescapeAll(FrameworkFileUtilities.FixFilePath(outputs[0].ToString()));
982982
ErrorUtilities.ThrowIfTypeDoesNotImplementToString(outputs[0]);
983983

984984
DateTime oldestOutputFileTime = DateTime.MinValue;
@@ -996,15 +996,15 @@ internal static bool IsAnyOutOfDate<T>(out DependencyAnalysisLogDetail dependenc
996996
if (oldestOutputFileTime == DateTime.MinValue)
997997
{
998998
// First output is missing: we must build the target
999-
string arbitraryInput = EscapingUtilities.UnescapeAll(FileUtilities.FixFilePath(inputs[0].ToString()));
999+
string arbitraryInput = EscapingUtilities.UnescapeAll(FrameworkFileUtilities.FixFilePath(inputs[0].ToString()));
10001000
ErrorUtilities.ThrowIfTypeDoesNotImplementToString(inputs[0]);
10011001
dependencyAnalysisDetailEntry = new DependencyAnalysisLogDetail(arbitraryInput, oldestOutput, null, null, OutofdateReason.MissingOutput);
10021002
return true;
10031003
}
10041004

10051005
for (int i = 1; i < outputs.Count; i++)
10061006
{
1007-
string candidateOutput = EscapingUtilities.UnescapeAll(FileUtilities.FixFilePath(outputs[i].ToString()));
1007+
string candidateOutput = EscapingUtilities.UnescapeAll(FrameworkFileUtilities.FixFilePath(outputs[i].ToString()));
10081008
ErrorUtilities.ThrowIfTypeDoesNotImplementToString(outputs[i]);
10091009
DateTime candidateOutputFileTime = DateTime.MinValue;
10101010
try
@@ -1022,7 +1022,7 @@ internal static bool IsAnyOutOfDate<T>(out DependencyAnalysisLogDetail dependenc
10221022
{
10231023
// An output is missing: we must build the target
10241024
string arbitraryInput =
1025-
EscapingUtilities.UnescapeAll(FileUtilities.FixFilePath(inputs[0].ToString()));
1025+
EscapingUtilities.UnescapeAll(FrameworkFileUtilities.FixFilePath(inputs[0].ToString()));
10261026
ErrorUtilities.ThrowIfTypeDoesNotImplementToString(inputs[0]);
10271027
dependencyAnalysisDetailEntry = new DependencyAnalysisLogDetail(arbitraryInput, candidateOutput, null, null, OutofdateReason.MissingOutput);
10281028
return true;
@@ -1039,7 +1039,7 @@ internal static bool IsAnyOutOfDate<T>(out DependencyAnalysisLogDetail dependenc
10391039
// Now compare the oldest output with each input and break out if we find one newer.
10401040
foreach (T input in inputs)
10411041
{
1042-
string unescapedInput = EscapingUtilities.UnescapeAll(FileUtilities.FixFilePath(input.ToString()));
1042+
string unescapedInput = EscapingUtilities.UnescapeAll(FrameworkFileUtilities.FixFilePath(input.ToString()));
10431043
ErrorUtilities.ThrowIfTypeDoesNotImplementToString(input);
10441044
DateTime inputFileTime = DateTime.MaxValue;
10451045
try
@@ -1127,8 +1127,8 @@ private void RecordUniqueInputsAndOutputs<T>(IList<T> inputs, IList<T> outputs)
11271127
/// <returns>true, if "input" is newer than "output"</returns>
11281128
private bool IsOutOfDate(string input, string output, string inputItemName, string outputItemName)
11291129
{
1130-
input = EscapingUtilities.UnescapeAll(FileUtilities.FixFilePath(input));
1131-
output = EscapingUtilities.UnescapeAll(FileUtilities.FixFilePath(output));
1130+
input = EscapingUtilities.UnescapeAll(FrameworkFileUtilities.FixFilePath(input));
1131+
output = EscapingUtilities.UnescapeAll(FrameworkFileUtilities.FixFilePath(output));
11321132
ProjectErrorUtilities.VerifyThrowInvalidProject(input.AsSpan().IndexOfAny(MSBuildConstants.InvalidPathChars) < 0, _project.ProjectFileLocation, "IllegalCharactersInFileOrDirectory", input, inputItemName);
11331133
ProjectErrorUtilities.VerifyThrowInvalidProject(output.AsSpan().IndexOfAny(MSBuildConstants.InvalidPathChars) < 0, _project.ProjectFileLocation, "IllegalCharactersInFileOrDirectory", output, outputItemName);
11341134
bool outOfDate = (CompareLastWriteTimes(input, output, out bool inputDoesNotExist, out bool outputDoesNotExist) == 1) || inputDoesNotExist;

src/Build/BackEnd/Components/SdkResolution/SdkResolverManifest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Text.RegularExpressions;
77
using System.Xml;
8+
using Microsoft.Build.Framework;
89
using Microsoft.Build.Shared;
910

1011
#nullable disable
@@ -79,7 +80,7 @@ internal static SdkResolverManifest Load(string filePath, string manifestFolder)
7980
{
8081
SdkResolverManifest manifest = ParseSdkResolverElement(reader, filePath);
8182

82-
manifest.Path = FileUtilities.FixFilePath(manifest.Path);
83+
manifest.Path = FrameworkFileUtilities.FixFilePath(manifest.Path);
8384
if (!System.IO.Path.IsPathRooted(manifest.Path))
8485
{
8586
manifest.Path = System.IO.Path.Combine(manifestFolder, manifest.Path);

src/Build/Construction/ProjectImportElement.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal ProjectImportElement(XmlElementWithLocation xmlElement, ProjectRootElem
5252
/// </summary>
5353
public string Project
5454
{
55-
get => FileUtilities.FixFilePath(GetAttributeValue(XMakeAttributes.project));
55+
get => FrameworkFileUtilities.FixFilePath(GetAttributeValue(XMakeAttributes.project));
5656
set
5757
{
5858
ErrorUtilities.VerifyThrowArgumentLength(value, XMakeAttributes.project);
@@ -71,7 +71,7 @@ public string Project
7171
/// </summary>
7272
public string Sdk
7373
{
74-
get => FileUtilities.FixFilePath(GetAttributeValue(XMakeAttributes.sdk));
74+
get => FrameworkFileUtilities.FixFilePath(GetAttributeValue(XMakeAttributes.sdk));
7575
set
7676
{
7777
ErrorUtilities.VerifyThrowArgumentLength(value, XMakeAttributes.sdk);

src/Build/Construction/ProjectRootElement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ public ProjectTargetElement AddTarget(string name)
12561256
/// </summary>
12571257
public ProjectUsingTaskElement AddUsingTask(string name, string assemblyFile, string assemblyName)
12581258
{
1259-
ProjectUsingTaskElement usingTask = CreateUsingTaskElement(name, FileUtilities.FixFilePath(assemblyFile), assemblyName);
1259+
ProjectUsingTaskElement usingTask = CreateUsingTaskElement(name, FrameworkFileUtilities.FixFilePath(assemblyFile), assemblyName);
12601260
AppendChild(usingTask);
12611261

12621262
return usingTask;

src/Build/Construction/ProjectUsingTaskElement.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6-
6+
using Microsoft.Build.Framework;
77
using Microsoft.Build.ObjectModelRemoting;
88
using Microsoft.Build.Shared;
99

@@ -48,14 +48,14 @@ private ProjectUsingTaskElement(XmlElementWithLocation xmlElement, ProjectRootEl
4848
/// </summary>
4949
public string AssemblyFile
5050
{
51-
get => FileUtilities.FixFilePath(
51+
get => FrameworkFileUtilities.FixFilePath(
5252
GetAttributeValue(XMakeAttributes.assemblyFile));
5353

5454
set
5555
{
5656
ErrorUtilities.VerifyThrowArgumentLength(value, XMakeAttributes.assemblyName);
5757
ErrorUtilities.VerifyThrowInvalidOperation(String.IsNullOrEmpty(AssemblyName), "OM_EitherAttributeButNotBoth", ElementName, XMakeAttributes.assemblyFile, XMakeAttributes.assemblyName);
58-
value = FileUtilities.FixFilePath(value);
58+
value = FrameworkFileUtilities.FixFilePath(value);
5959
SetOrRemoveAttribute(XMakeAttributes.assemblyFile, value, "Set usingtask AssemblyFile {0}", value);
6060
}
6161
}
@@ -249,7 +249,7 @@ internal static ProjectUsingTaskElement CreateDisconnected(string taskName, stri
249249

250250
if (!String.IsNullOrEmpty(assemblyFile))
251251
{
252-
usingTask.AssemblyFile = FileUtilities.FixFilePath(assemblyFile);
252+
usingTask.AssemblyFile = FrameworkFileUtilities.FixFilePath(assemblyFile);
253253
}
254254
else
255255
{

src/Build/Construction/Solution/SolutionProjectGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ private string GetMetaprojectName(ProjectInSolution project)
13081308
baseName = project.ProjectName;
13091309
}
13101310

1311-
baseName = FileUtilities.EnsureNoTrailingSlash(baseName);
1311+
baseName = FrameworkFileUtilities.EnsureNoTrailingSlash(baseName);
13121312

13131313
return GetMetaprojectName(baseName);
13141314
}

src/Build/Definition/Toolset.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
using Microsoft.Build.Collections;
1414
using Microsoft.Build.Construction;
1515
using Microsoft.Build.Execution;
16-
#if NET
1716
using Microsoft.Build.Framework;
18-
#endif
17+
1918
using Microsoft.Build.Internal;
2019
using Microsoft.Build.Shared;
2120
using Microsoft.Build.Shared.FileSystem;
@@ -367,7 +366,7 @@ private set
367366
// technically hurt anything, but it doesn't look nice.)
368367
string toolsPathToUse = value;
369368

370-
if (FileUtilities.EndsWithSlash(toolsPathToUse))
369+
if (FrameworkFileUtilities.EndsWithSlash(toolsPathToUse))
371370
{
372371
string rootPath = Path.GetPathRoot(Path.GetFullPath(toolsPathToUse));
373372

0 commit comments

Comments
 (0)