Skip to content

Commit

Permalink
Checking for VS pathing when import not found (#9625)
Browse files Browse the repository at this point in the history
Fixes #8967

Context
Users sometimes use different distributions to build projects, however some files available in the VS distribution are not available with dotnet build. It used to throw a "File not found" error, but now it is switched to "this imported project is in a VS distribution".

An example message is:  error MSB4278: The imported project $(MSBuildE xtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\DSLTools\Microsoft.DslTools.settings.targets is from the Visual Studio distribution of MSBuild. Build with MSBuild.exe in a Developer Command Prompt.

Changes Made
Before throwing a invalidProject error for file not found, check the path of the file if it is within Visual Studio. If it is, throw new error instead.
  • Loading branch information
maridematte authored Feb 14, 2024
1 parent 299e051 commit 0e8b7ac
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/Build.UnitTests/Evaluation/Evaluator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,31 @@ public void MultipleImportsVerifyImportsIncludingDuplicates()
}
}

[DotNetOnlyFact("Tests .NET SDK-only error")]
public void ImportWithVSPathThrowsCorrectError()
{
string importPath = null;

using (TestEnvironment env = TestEnvironment.Create())
{
// Does not matter that the file or folder does not exist, we are checking for the VS pathing here
importPath = "path\\that\\does\\not\\exist\\Microsoft\\VisualStudio\\FileName.txt";

var content = env.CreateTestProjectWithFiles(@"
<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns='msbuildnamespace' >
<Import Project='" + importPath + @"'/>
</Project>
");

InvalidProjectFileException ex = Assert.Throws<InvalidProjectFileException>(() =>
{
Project project = new Project(content.ProjectFile, null, null);
});

Assert.Contains("MSB4278", ex.ErrorCode);
}
}

/// <summary>
/// RecordDuplicateButNotCircularImports should not record circular imports (which do come under the category of "duplicate imports".
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/Build/Evaluation/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2304,6 +2304,8 @@ private LoadImportsResult ExpandAndLoadImportsFromUnescapedImportExpression(stri
continue;
}

VerifyVSDistributionPath(importElement.Project, importLocationInProject);

ProjectErrorUtilities.ThrowInvalidProject(importLocationInProject, "ImportedProjectNotFound",
importFileUnescaped, importExpressionEscaped);
}
Expand Down Expand Up @@ -2577,6 +2579,8 @@ private void ThrowForImportedProjectWithSearchPathsNotFound(ProjectImportPathMat

string stringifiedListOfSearchPaths = StringifyList(onlyFallbackSearchPaths);

VerifyVSDistributionPath(importElement.Project, importElement.ProjectLocation);

#if FEATURE_SYSTEM_CONFIGURATION
string configLocation = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

Expand Down Expand Up @@ -2640,6 +2644,16 @@ private void SetAllProjectsProperty()
mayBeReserved: false);
}
}

[Conditional("FEATURE_GUIDE_TO_VS_ON_UNSUPPORTED_PROJECTS")]
private void VerifyVSDistributionPath(string path, ElementLocation importLocationInProject)
{
if (path.IndexOf("Microsoft\\VisualStudio", StringComparison.OrdinalIgnoreCase) >= 0
|| path.IndexOf("Microsoft/VisualStudio", StringComparison.OrdinalIgnoreCase) >= 0)
{
ProjectErrorUtilities.ThrowInvalidProject(importLocationInProject, "ImportedProjectFromVSDistribution", path);
}
}
}

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion src/Build/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@
<value>MSB4019: The imported project "{0}" was not found. Confirm that the expression in the Import declaration "{1}" is correct, and that the file exists on disk.</value>
<comment>{StrBegin="MSB4019: "}LOCALIZATION: &lt;Import&gt; should not be localized.</comment>
</data>
<data name="ImportedProjectFromVSDistribution" xml:space="preserve">
<value>MSB4278: The imported file "{0}" does not exist and appears to be part of a Visual Studio component. This file may require MSBuild.exe in order to be imported successfully, and so may fail to build in the dotnet CLI. </value>
<comment>{StrBegin="MSB4278: "}LOCALIZATION: &lt;Import&gt; should not be localized.</comment>
</data>
<data name="ImportedProjectFromExtensionsPathNotFoundFromAppConfig" xml:space="preserve">
<value>MSB4226: The imported project "{0}" was not found. Also, tried to find "{1}" in the fallback search path(s) for {2} - {3} . These search paths are defined in "{4}". Confirm that the path in the &lt;Import&gt; declaration is correct, and that the file exists on disk in one of the search paths.</value>
<comment>{StrBegin="MSB4226: "}LOCALIZATION: &lt;Import&gt; should not be localized.</comment>
Expand Down Expand Up @@ -2094,7 +2098,7 @@ Utilization: {0} Average Utilization: {1:###.0}</value>
<!--
The Build message bucket is: MSB4000 - MSB4999
Next message code should be MSB4278
Next message code should be MSB4279
Don't forget to update this comment after using a new code.
-->
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0e8b7ac

Please sign in to comment.