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
25 changes: 25 additions & 0 deletions src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,5 +358,30 @@ public void TaskEnvironment_MultithreadedEnvironment_ShouldBeIsolatedFromSystem(
Environment.SetEnvironmentVariable(testVarName, null);
}
}

[Theory]
[MemberData(nameof(EnvironmentTypes))]
public void TaskEnvironment_GetAbsolutePath_WithInvalidPathChars_ShouldNotThrow(string environmentType)
{
// Construct a path containing an invalid path character
char invalidChar = Path.GetInvalidPathChars().FirstOrDefault();
Comment thread
AR-May marked this conversation as resolved.
string invalidPath = "invalid" + invalidChar + "path";

var taskEnvironment = CreateTaskEnvironment(environmentType);

try
{
// Should not throw on invalid path characters
var absolutePath = taskEnvironment.GetAbsolutePath(invalidPath);

// The result should contain the invalid path combined with the base directory
absolutePath.Value.ShouldNotBeNullOrEmpty();
absolutePath.Value.ShouldContain(invalidPath);
}
finally
{
DisposeTaskEnvironment(taskEnvironment);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
#if NETFRAMEWORK
using Microsoft.IO;
#else
using System.IO;
#endif
using Microsoft.Build.Framework;
using Microsoft.Build.Internal;

Expand Down Expand Up @@ -41,7 +45,10 @@ public AbsolutePath ProjectDirectory
/// <inheritdoc/>
public AbsolutePath GetAbsolutePath(string path)
{
return new AbsolutePath(Path.GetFullPath(path), ignoreRootedCheck: true);
// This function should not throw when path has illegal characters.
// For .NET Framework, Microsoft.IO.Path.Combine should be used instead of System.IO.Path.Combine to achieve it.
// For .NET Core, System.IO.Path.Combine already does not throw in this case.
return new AbsolutePath(Path.Combine(NativeMethodsShared.GetCurrentDirectory(), path), ignoreRootedCheck: true);
Comment thread
AR-May marked this conversation as resolved.
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public AbsolutePath ProjectDirectory
{
_currentDirectory = value;
// Keep the thread-static in sync for use by Expander and Modifiers during property/item expansion.
// This allows Path.GetFullPath and %(FullPath) to resolve relative paths correctly in multithreaded mode.
// This allows Path.GetFullPath and %(FullPath) functions used in project files to resolve relative paths correctly in multithreaded mode.
FileUtilities.CurrentThreadWorkingDirectory = value.Value;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/TaskEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AbsolutePath ProjectDirectory

/// <summary>
/// Converts a relative or absolute path string to an absolute path.
/// This function resolves paths relative to ProjectDirectory.
/// This function resolves paths relative to <see cref="ProjectDirectory"/>.
/// </summary>
/// <param name="path">The path to convert.</param>
/// <returns>An absolute path representation.</returns>
Expand Down Expand Up @@ -77,4 +77,4 @@ public AbsolutePath ProjectDirectory
/// </summary>
internal void Dispose() => _driver.Dispose();
}
}
}