Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking for VS pathing when import not found #9625

Merged
merged 10 commits into from
Feb 14, 2024
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/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)
maridematte marked this conversation as resolved.
Show resolved Hide resolved
{
if (path.IndexOf("Microsoft\\VisualStudio", StringComparison.OrdinalIgnoreCase) >= 0
|| path.IndexOf("Microsoft/VisualStudio", StringComparison.OrdinalIgnoreCase) >= 0)
maridematte marked this conversation as resolved.
Show resolved Hide resolved
{
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 @@ -527,6 +527,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 @@ -2013,7 +2017,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