Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Build.Execution;
using Microsoft.Build.Shared;
using Xunit;
using Xunit.NetCore.Extensions;

#nullable disable

Expand Down Expand Up @@ -842,6 +843,47 @@ public void FallbackImportWithFileNotFoundWhenPropertyNotDefined()
FileUtilities.DeleteDirectoryNoThrow(extnDir1, true);
}
}
/// <summary>
/// Fall-back search path on a property that is not valid. https://github.com/dotnet/msbuild/issues/8762
/// </summary>
/// <param name="projectValue">imported project value expression</param>
[WindowsFullFrameworkOnlyTheory]
[InlineData("")]
[InlineData("|")]
public void FallbackImportWithInvalidProjectValue(string projectValue)
{
string mainTargetsFileContent = $"""
<Project>
<PropertyGroup>
<VSToolsPath>{projectValue}</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)"/>
</Project>
""";

string extnDir1 = null;
string mainProjectPath = null;

try
{
// The path to "extensions1" fallback should exist, but the file doesn't need to
extnDir1 = GetNewExtensionsPathAndCreateFile("extensions1", Path.Combine("file.props"), string.Empty);

mainProjectPath = ObjectModelHelpers.CreateFileInTempProjectDirectory("main.proj", mainTargetsFileContent);
var projectCollection = GetProjectCollection(new Dictionary<string, string> { ["FallbackExpandDir1"] = extnDir1 });
projectCollection.ResetToolsetsForTests(WriteConfigFileAndGetReader("VSToolsPath", @"$(FallbackExpandDir1)\Microsoft\VisualStudio\v99"));
var logger = new MockLogger();
projectCollection.RegisterLogger(logger);

Assert.Throws<InvalidProjectFileException>(() => projectCollection.LoadProject(mainProjectPath));
logger.AssertLogContains("MSB4020");
}
finally
{
FileUtilities.DeleteNoThrow(mainProjectPath);
FileUtilities.DeleteDirectoryNoThrow(extnDir1, true);
}
}

private void CreateAndBuildProjectForImportFromExtensionsPath(string extnPathPropertyName, Action<Project, MockLogger> action)
{
Expand Down
11 changes: 10 additions & 1 deletion src/Build/Evaluation/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2547,7 +2547,16 @@ private void ThrowForImportedProjectWithSearchPathsNotFound(ProjectImportPathMat
importElement.Project.Replace(searchPathMatch.MsBuildPropertyFormat, extensionsPathPropValue),
ExpanderOptions.ExpandProperties, importElement.ProjectLocation);

relativeProjectPath = FileUtilities.MakeRelative(extensionsPathPropValue, importExpandedWithDefaultPath);
try
{
relativeProjectPath = FileUtilities.MakeRelative(extensionsPathPropValue, importExpandedWithDefaultPath);
}
catch (ArgumentException)
{
// https://github.com/dotnet/msbuild/issues/8762 In NET Framework, Path.* function will throw exceptions if the path contains invalid characters.
ProjectErrorUtilities.ThrowInvalidProject(importElement.Location, "InvalidAttributeValue", importExpandedWithDefaultPath, XMakeAttributes.project, XMakeElements.import);
return;
}
}
else
{
Expand Down