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

When running MSBuild static graph restore display additional log messages when project format is not known to MSBuild #4126

Merged
merged 9 commits into from
Jul 9, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ public async Task<bool> RestoreAsync(string entryProjectFilePath, IDictionary<st
return false;
}

if (string.Equals(Path.GetExtension(entryProjectFilePath), ".sln", StringComparison.OrdinalIgnoreCase)
&& dependencyGraphSpec.Restore.Count == 0)
{
MSBuildLogger.LogInformation(string.Format(CultureInfo.CurrentCulture, Strings.Log_NoProjectsForRestore));
nkolev92 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

try
{
return await BuildTasksUtility.RestoreAsync(
Expand Down Expand Up @@ -534,14 +541,26 @@ private static LibraryIncludeFlags GetLibraryIncludeFlags(string value, LibraryI
/// <param name="entryProjectPath">The full path to the main project or solution file.</param>
/// <param name="globalProperties">An <see cref="IDictionary{String,String}" /> representing the global properties for the project.</param>
/// <returns></returns>
private static List<ProjectGraphEntryPoint> GetProjectGraphEntryPoints(string entryProjectPath, IDictionary<string, string> globalProperties)
private List<ProjectGraphEntryPoint> GetProjectGraphEntryPoints(string entryProjectPath, IDictionary<string, string> globalProperties)
{
// If the project's extension is .sln, parse it as a Visual Studio solution and return the projects it contains
if (string.Equals(Path.GetExtension(entryProjectPath), ".sln", StringComparison.OrdinalIgnoreCase))
{
var solutionFile = SolutionFile.Parse(entryProjectPath);

return solutionFile.ProjectsInOrder.Where(i => i.ProjectType == SolutionProjectType.KnownToBeMSBuildFormat).Select(i => new ProjectGraphEntryPoint(i.AbsolutePath, globalProperties)).ToList();
IEnumerable<ProjectInSolution> projectsKnownToMSBuild = solutionFile.ProjectsInOrder.Where(i => i.ProjectType == SolutionProjectType.KnownToBeMSBuildFormat);
IEnumerable<ProjectInSolution> projectsNotKnownToMSBuild = solutionFile.ProjectsInOrder.Except(projectsKnownToMSBuild);

if (projectsNotKnownToMSBuild.Any())
{
IList<string> projects = projectsNotKnownToMSBuild.Select(project => project.ProjectName).ToList();

MSBuildLogger.LogInformation(string.Format(CultureInfo.CurrentCulture,
nkolev92 marked this conversation as resolved.
Show resolved Hide resolved
Strings.Log_ProjectsInSolutionNotKnowntoMSBuild,
projects.Count, string.Join(",", projects)));
}

return projectsKnownToMSBuild.Select(i => new ProjectGraphEntryPoint(i.AbsolutePath, globalProperties)).ToList();
}

// Return just the main project in a list if its not a solution file
Expand Down
2 changes: 2 additions & 0 deletions src/NuGet.Core/NuGet.Build.Tasks/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
static NuGet.Build.Tasks.Strings.Log_NoProjectsForRestore.get -> string
static NuGet.Build.Tasks.Strings.Log_ProjectsInSolutionNotKnowntoMSBuild.get -> string
22 changes: 20 additions & 2 deletions src/NuGet.Core/NuGet.Build.Tasks/Strings.Designer.cs

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

8 changes: 8 additions & 0 deletions src/NuGet.Core/NuGet.Build.Tasks/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,12 @@
<data name="Error_MissingRestoreGraphOutputPath" xml:space="preserve">
<value>Cannot write the dependency graph spec because the `RestoreGraphOutputPath` is missing.</value>
</data>
<data name="Log_ProjectsInSolutionNotKnowntoMSBuild" xml:space="preserve">
<value>The solution contains '{0}' project(s) '{1}' that are not known to MSBuild. Ensure that all projects are known to be MSBuild before running restore on the solution.</value>
<comment>0 - number of projects not known to msbuild
1 - list of projects</comment>
</data>
<data name="Log_NoProjectsForRestore" xml:space="preserve">
<value>The solution did not have any projects to restore, ensure that all projects are known to be MSBuild and that the projects exist.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,68 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
}
}

[PlatformFact(Platform.Windows)]
public Task MsbuildRestore_WithStaticGraphRestore_MessageLoggedAtDefaultVerbosityWhenThereAreNoProjectsToRestore()
{
using (var pathContext = new SimpleTestPathContext())
{
// Set up solution, project, and packages
var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot);

var net461 = NuGetFramework.Parse("net461");

var project = SimpleTestProjectContext.CreateLegacyPackageReference(
"a",
pathContext.SolutionRoot,
net461);

solution.Projects.Add(project);
solution.Create(pathContext.SolutionRoot);

var result = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore {pathContext.SolutionRoot} /p:RestoreUseStaticGraphEvaluation=true",
ignoreExitCode: true);

result.Success.Should().BeTrue(because: result.AllOutput);
result.AllOutput.Should().Contain("The solution did not have any projects to restore, ensure that all projects are known to " +
"be MSBuild and that the projects exist.", because: result.AllOutput);
}

return Task.CompletedTask;
}

[PlatformFact(Platform.Windows)]
public Task MsbuildRestore_WithStaticGraphRestore_MessageLoggedAtDefaultVerbosityWhenAProjectIsNotKnownToMSBuild()
{
using (var pathContext = new SimpleTestPathContext())
{
// Set up solution, project, and packages
var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot);

var net461 = NuGetFramework.Parse("net461");

var project = SimpleTestProjectContext.CreateLegacyPackageReference(
"a",
pathContext.SolutionRoot,
net461);

solution.Projects.Add(project);
solution.Create(pathContext.SolutionRoot);

string newSlnFileContent = File.ReadAllText(solution.SolutionPath);
newSlnFileContent = newSlnFileContent.Replace("FAE04EC0-301F-11D3-BF4B-00C04F79EFBC", Guid.Empty.ToString());
File.WriteAllText(solution.SolutionPath, newSlnFileContent);

var result = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore {pathContext.SolutionRoot} /p:RestoreUseStaticGraphEvaluation=true",
ignoreExitCode: true);

result.Success.Should().BeTrue(because: result.AllOutput);
result.AllOutput.Should().Contain($"The solution contains '{solution.Projects.Count}' project(s) '{project.ProjectName}' that are not known to MSBuild. " +
"Ensure that all projects are known to be MSBuild before running restore on the solution.", because: result.AllOutput);
}

return Task.CompletedTask;
}

[PlatformTheory(Platform.Windows)]
[InlineData(true)]
[InlineData(false)]
Expand Down