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 6 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
38 changes: 38 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,44 @@ public void MultipleImportsVerifyImportsIncludingDuplicates()
}
}

[Fact]
maridematte marked this conversation as resolved.
Show resolved Hide resolved
public void ImportWithVSPathThrowsCorrectError()
{
InvalidProjectFileException ex = Assert.Throws<InvalidProjectFileException>(() =>
maridematte marked this conversation as resolved.
Show resolved Hide resolved
{
string projectPath = null;
string importPath = null;

try
{
// 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";
projectPath = FileUtilities.GetTemporaryFileName();

string import = ObjectModelHelpers.CleanupFileContents(@"
<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns='msbuildnamespace' >
</Project>
");

File.WriteAllText(projectPath, import);
maridematte marked this conversation as resolved.
Show resolved Hide resolved

string content = ObjectModelHelpers.CleanupFileContents(@"
<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns='msbuildnamespace' >
<Import Project='" + importPath + @"'/>
</Project>
");
maridematte marked this conversation as resolved.
Show resolved Hide resolved

Project project = new Project(XmlReader.Create(new StringReader(content)));
}
finally
{
File.Delete(projectPath);
}
});

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

/// <summary>
/// RecordDuplicateButNotCircularImports should not record circular imports (which do come under the category of "duplicate imports".
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions src/Build/Evaluation/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2304,6 +2304,10 @@ ProjectRootElement InnerCreate(string _, ProjectRootElementCacheBase __)
continue;
}

#if !FEATURE_VISUALSTUDIOSETUP
maridematte marked this conversation as resolved.
Show resolved Hide resolved
VerifyVSDistributionPath(importElement.Project, importLocationInProject);
#endif

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

string stringifiedListOfSearchPaths = StringifyList(onlyFallbackSearchPaths);

#if !FEATURE_VISUALSTUDIOSETUP
VerifyVSDistributionPath(importElement.Project, importElement.ProjectLocation);
#endif

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

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

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 project {0} does not exist and appears to be part of Visual Studio. This project may require MSBuild.exe and fail to build in the dotnet CLI. </value>
maridematte marked this conversation as resolved.
Show resolved Hide resolved
<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