Skip to content

Commit c6bb232

Browse files
authored
Catch the illegal argument exception in Net Framework! (dotnet#8839)
Fixes dotnet#8762 Context Catch the exceptions when extensionsPathPropValue is null or importExpandedWithDefaultPath is empty. In NET Framework, Path.* function also throws exceptions if the path contains invalid characters Changes Made Catch the exception. Testing FallbackImportWithInvalidProjectValue Notes
1 parent 9afc09a commit c6bb232

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/Build.UnitTests/Evaluation/ImportFromMSBuildExtensionsPath_Tests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.Build.Execution;
1111
using Microsoft.Build.Shared;
1212
using Xunit;
13+
using Xunit.NetCore.Extensions;
1314

1415
#nullable disable
1516

@@ -842,6 +843,43 @@ public void FallbackImportWithFileNotFoundWhenPropertyNotDefined()
842843
FileUtilities.DeleteDirectoryNoThrow(extnDir1, true);
843844
}
844845
}
846+
/// <summary>
847+
/// Fall-back search path on a property that is not valid. https://github.com/dotnet/msbuild/issues/8762
848+
/// </summary>
849+
/// <param name="projectValue">imported project value expression</param>
850+
[Theory]
851+
[InlineData("")]
852+
[InlineData("|")]
853+
public void FallbackImportWithInvalidProjectValue(string projectValue)
854+
{
855+
string mainTargetsFileContent = $"""
856+
<Project>
857+
<PropertyGroup>
858+
<VSToolsPath>{projectValue}</VSToolsPath>
859+
</PropertyGroup>
860+
<Import Project="$(VSToolsPath)"/>
861+
</Project>
862+
""";
863+
864+
using TestEnvironment testEnvironment = TestEnvironment.Create();
865+
string mainProjectPath = testEnvironment.CreateTestProjectWithFiles("main.proj", mainTargetsFileContent).ProjectFile;
866+
var projectCollection = GetProjectCollection();
867+
projectCollection.ResetToolsetsForTests(WriteConfigFileAndGetReader("VSToolsPath", "temp"));
868+
var logger = new MockLogger();
869+
projectCollection.RegisterLogger(logger);
870+
Assert.Throws<InvalidProjectFileException>(() => projectCollection.LoadProject(mainProjectPath));
871+
872+
if (string.IsNullOrEmpty(projectValue))
873+
{
874+
logger.AssertLogContains("MSB4102");
875+
}
876+
else
877+
{
878+
#if NETFRAMEWORK
879+
logger.AssertLogContains("MSB4102");
880+
#endif
881+
}
882+
}
845883

846884
private void CreateAndBuildProjectForImportFromExtensionsPath(string extnPathPropertyName, Action<Project, MockLogger> action)
847885
{

src/Build/Evaluation/Evaluator.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2536,7 +2536,16 @@ private void ThrowForImportedProjectWithSearchPathsNotFound(ProjectImportPathMat
25362536
importElement.Project.Replace(searchPathMatch.MsBuildPropertyFormat, extensionsPathPropValue),
25372537
ExpanderOptions.ExpandProperties, importElement.ProjectLocation);
25382538

2539-
relativeProjectPath = FileUtilities.MakeRelative(extensionsPathPropValue, importExpandedWithDefaultPath);
2539+
try
2540+
{
2541+
relativeProjectPath = FileUtilities.MakeRelative(extensionsPathPropValue, importExpandedWithDefaultPath);
2542+
}
2543+
catch (ArgumentException ex)
2544+
{
2545+
// https://github.com/dotnet/msbuild/issues/8762 .Catch the exceptions when extensionsPathPropValue is null or importExpandedWithDefaultPath is empty. In NET Framework, Path.* function also throws exceptions if the path contains invalid characters.
2546+
ProjectErrorUtilities.ThrowInvalidProject(importElement.Location, "InvalidAttributeValueWithException", importExpandedWithDefaultPath, XMakeAttributes.project, XMakeElements.import, ex.Message);
2547+
return;
2548+
}
25402549
}
25412550
else
25422551
{

0 commit comments

Comments
 (0)