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

Improve restore error messages in source mapping scenarios #4374

Merged
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
6 changes: 1 addition & 5 deletions src/NuGet.Core/NuGet.Commands/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'XElement BuildAssetsUtils.GenerateContentFilesItem(string path, LockFileContentFile item, string packageId, string packageVersion)', validate parameter 'item' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.BuildAssetsUtils.GenerateContentFilesItem(System.String,NuGet.ProjectModel.LockFileContentFile,System.String,System.String)~System.Xml.Linq.XElement")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'string BuildAssetsUtils.GetLanguage(string nugetLanguage)', validate parameter 'nugetLanguage' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.BuildAssetsUtils.GetLanguage(System.String)~System.String")]
[assembly: SuppressMessage("Build", "CA1307:The behavior of 'string.EndsWith(string)' could vary based on the current user's locale settings. Replace this call in 'NuGet.Commands.BuildAssetsUtils.GetLanguageGroups(System.Collections.Generic.IEnumerable<System.Tuple<NuGet.ProjectModel.LockFileTargetLibrary, NuGet.ProjectModel.LockFileContentFile, string>>)' with a call to 'string.EndsWith(string, System.StringComparison)'.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.BuildAssetsUtils.GetLanguageGroups(System.Collections.Generic.IEnumerable{System.Tuple{NuGet.ProjectModel.LockFileTargetLibrary,NuGet.ProjectModel.LockFileContentFile,System.String}})~System.Collections.Generic.IEnumerable{NuGet.Commands.MSBuildRestoreItemGroup}")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'string BuildAssetsUtils.GetMSBuildFilePath(PackageSpec project, RestoreRequest request, string extension)', validate parameter 'project' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.BuildAssetsUtils.GetMSBuildFilePath(NuGet.ProjectModel.PackageSpec,NuGet.Commands.RestoreRequest,System.String)~System.String")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'string BuildAssetsUtils.GetMSBuildFilePathForPackageReferenceStyleProject(PackageSpec project, string extension)', validate parameter 'project' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.BuildAssetsUtils.GetMSBuildFilePathForPackageReferenceStyleProject(NuGet.ProjectModel.PackageSpec,System.String)~System.String")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'List<MSBuildOutputFile> BuildAssetsUtils.GetMSBuildOutputFiles(PackageSpec project, LockFile assetsFile, IEnumerable<RestoreTargetGraph> targetGraphs, IReadOnlyList<NuGetv3LocalRepository> repositories, RestoreRequest request, string assetsFilePath, bool restoreSuccess, ILogger log)', validate parameter 'assetsFile' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.BuildAssetsUtils.GetMSBuildOutputFiles(NuGet.ProjectModel.PackageSpec,NuGet.ProjectModel.LockFile,System.Collections.Generic.IEnumerable{NuGet.Commands.RestoreTargetGraph},System.Collections.Generic.IReadOnlyList{NuGet.Repositories.NuGetv3LocalRepository},NuGet.Commands.RestoreRequest,System.String,System.Boolean,NuGet.Common.ILogger)~System.Collections.Generic.List{NuGet.Commands.MSBuildOutputFile}")]
[assembly: SuppressMessage("Build", "CA1801:Parameter log of method GetMSBuildOutputFiles is never used. Remove the parameter or use it in the method body.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.BuildAssetsUtils.GetMSBuildOutputFiles(NuGet.ProjectModel.PackageSpec,NuGet.ProjectModel.LockFile,System.Collections.Generic.IEnumerable{NuGet.Commands.RestoreTargetGraph},System.Collections.Generic.IReadOnlyList{NuGet.Repositories.NuGetv3LocalRepository},NuGet.Commands.RestoreRequest,System.String,System.Boolean,NuGet.Common.ILogger)~System.Collections.Generic.List{NuGet.Commands.MSBuildOutputFile}")]
Expand Down Expand Up @@ -191,9 +190,6 @@
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'IEnumerable<RestoreLogMessage> UnexpectedDependencyMessages.GetMissingLowerBounds(IEnumerable<IRestoreTargetGraph> graphs, ISet<string> ignoreIds)', validate parameter 'graphs' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.UnexpectedDependencyMessages.GetMissingLowerBounds(System.Collections.Generic.IEnumerable{NuGet.Commands.IRestoreTargetGraph},System.Collections.Generic.ISet{System.String})~System.Collections.Generic.IEnumerable{NuGet.Common.RestoreLogMessage}")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'IEnumerable<RestoreLogMessage> UnexpectedDependencyMessages.GetProjectDependenciesMissingLowerBounds(PackageSpec project)', validate parameter 'project' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.UnexpectedDependencyMessages.GetProjectDependenciesMissingLowerBounds(NuGet.ProjectModel.PackageSpec)~System.Collections.Generic.IEnumerable{NuGet.Common.RestoreLogMessage}")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task UnexpectedDependencyMessages.LogAsync(IEnumerable<IRestoreTargetGraph> graphs, PackageSpec project, ILogger logger)', validate parameter 'logger' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.UnexpectedDependencyMessages.LogAsync(System.Collections.Generic.IEnumerable{NuGet.Commands.IRestoreTargetGraph},NuGet.ProjectModel.PackageSpec,NuGet.Common.ILogger)~System.Threading.Tasks.Task")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<RestoreLogMessage> UnresolvedMessages.GetMessageAsync(string targetGraphName, LibraryRange unresolved, IList<IRemoteDependencyProvider> remoteLibraryProviders, SourceCacheContext sourceCacheContext, ILogger logger, CancellationToken token)', validate parameter 'unresolved' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.UnresolvedMessages.GetMessageAsync(System.String,NuGet.LibraryModel.LibraryRange,System.Collections.Generic.IList{NuGet.DependencyResolver.IRemoteDependencyProvider},NuGet.Protocol.Core.Types.SourceCacheContext,NuGet.Common.ILogger,System.Threading.CancellationToken)~System.Threading.Tasks.Task{NuGet.Common.RestoreLogMessage}")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task UnresolvedMessages.LogAsync(IEnumerable<IRestoreTargetGraph> graphs, RemoteWalkContext context, ILogger logger, CancellationToken token)', validate parameter 'logger' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.UnresolvedMessages.LogAsync(System.Collections.Generic.IEnumerable{NuGet.Commands.IRestoreTargetGraph},NuGet.DependencyResolver.RemoteWalkContext,NuGet.Common.ILogger,System.Threading.CancellationToken)~System.Threading.Tasks.Task")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task UnresolvedMessages.LogAsync(IList<DownloadDependencyResolutionResult> downloadDependencyResults, IList<IRemoteDependencyProvider> remoteLibraryProviders, SourceCacheContext sourceCacheContext, ILogger logger, CancellationToken token)', validate parameter 'downloadDependencyResults' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.UnresolvedMessages.LogAsync(System.Collections.Generic.IList{NuGet.Commands.DownloadDependencyResolutionResult},System.Collections.Generic.IList{NuGet.DependencyResolver.IRemoteDependencyProvider},NuGet.Protocol.Core.Types.SourceCacheContext,NuGet.Common.ILogger,System.Threading.CancellationToken)~System.Threading.Tasks.Task")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'void UpdateSourceRunner.Run(UpdateSourceArgs args, Func<ILogger> getLogger)', validate parameter 'getLogger' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.UpdateSourceRunner.Run(NuGet.Commands.UpdateSourceArgs,System.Func{NuGet.Common.ILogger})")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<int> VerifyCommandRunner.ExecuteCommandAsync(VerifyArgs verifyArgs)', validate parameter 'verifyArgs' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.VerifyCommandRunner.ExecuteCommandAsync(NuGet.Commands.VerifyArgs)~System.Threading.Tasks.Task{System.Int32}")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'bool WarningPropertiesCollection.ApplyNoWarnProperties(IRestoreLogMessage message)', validate parameter 'message' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Commands.WarningPropertiesCollection.ApplyNoWarnProperties(NuGet.Common.IRestoreLogMessage)~System.Boolean")]
Expand Down Expand Up @@ -233,4 +229,4 @@
[assembly: SuppressMessage("Build", "CA2237:Add [Serializable] to SignCommandException as this type implements ISerializable", Justification = "<Pending>", Scope = "type", Target = "~T:NuGet.Commands.SignCommandException")]
[assembly: SuppressMessage("Build", "CA1307:The behavior of 'string.GetHashCode()' could vary based on the current user's locale settings. Replace this call in 'Microsoft.Extensions.Internal.HashCodeCombiner.Add(string)' with a call to 'string.GetHashCode(System.StringComparison)'.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.Extensions.Internal.HashCodeCombiner.Add(System.String)")]
[assembly: SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "API already shipped", Scope = "member", Target = "~M:NuGet.Commands.RestoreSummary.Log(NuGet.Common.ILogger,System.Collections.Generic.IReadOnlyList{NuGet.Commands.RestoreSummary},System.Boolean)")]

[assembly: SuppressMessage("Globalization", "CA1307:Specify StringComparison", Justification = "Annoying and dumb", Scope = "member", Target = "~M:NuGet.Commands.UnresolvedMessages.GetMessageAsync(System.String,NuGet.LibraryModel.LibraryRange,System.Collections.Generic.IList{NuGet.DependencyResolver.IRemoteDependencyProvider},System.Boolean,System.Collections.Generic.IList{NuGet.DependencyResolver.IRemoteDependencyProvider},NuGet.Protocol.Core.Types.SourceCacheContext,NuGet.Common.ILogger,System.Threading.CancellationToken)~System.Threading.Tasks.Task{NuGet.Common.RestoreLogMessage}")]
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ internal static class UnresolvedMessages
/// </summary>
internal static async Task LogAsync(IEnumerable<IRestoreTargetGraph> graphs, RemoteWalkContext context, CancellationToken token)
{
var tasks = graphs.SelectMany(graph => graph.Unresolved.Select(e => GetMessageAsync(graph.TargetGraphName, e, context.FilterDependencyProvidersForLibrary(e), context.CacheContext, context.Logger, token))).ToArray();
var tasks = graphs.SelectMany(graph => graph.Unresolved.Select(e =>
GetMessageAsync(
graph.TargetGraphName,
e,
context.FilterDependencyProvidersForLibrary(e),
context.PackageSourceMapping.IsEnabled,
context.RemoteLibraryProviders,
context.CacheContext,
context.Logger,
token))).ToArray();

var messages = await Task.WhenAll(tasks);

await context.Logger.LogMessagesAsync(DiagnosticUtility.MergeOnTargetGraph(messages));
Expand All @@ -45,6 +55,8 @@ internal static async Task LogAsync(IList<DownloadDependencyResolutionResult> do
ddi.Framework.ToString(),
unresolved,
context.FilterDependencyProvidersForLibrary(unresolved),
context.PackageSourceMapping.IsEnabled,
context.RemoteLibraryProviders,
context.CacheContext,
context.Logger,
token));
Expand All @@ -60,7 +72,9 @@ internal static async Task LogAsync(IList<DownloadDependencyResolutionResult> do
/// </summary>
internal static async Task<RestoreLogMessage> GetMessageAsync(string targetGraphName,
LibraryRange unresolved,
IList<IRemoteDependencyProvider> remoteLibraryProviders,
IList<IRemoteDependencyProvider> applicableRemoteLibraryProviders,
bool isPackageSourceMappingEnabled,
IList<IRemoteDependencyProvider> allRemoteLibraryProviders,
SourceCacheContext sourceCacheContext,
ILogger logger,
CancellationToken token)
Expand Down Expand Up @@ -91,11 +105,11 @@ internal static async Task<RestoreLogMessage> GetMessageAsync(string targetGraph
message = string.Format(CultureInfo.CurrentCulture, Strings.Error_ProjectDoesNotExist, unresolved.Name);
}
}
else if (unresolved.TypeConstraintAllows(LibraryDependencyTarget.Package) && remoteLibraryProviders.Count > 0)
else if (unresolved.TypeConstraintAllows(LibraryDependencyTarget.Package) && applicableRemoteLibraryProviders.Count > 0)
{
// Package
var range = unresolved.VersionRange ?? VersionRange.All;
var sourceInfo = await GetSourceInfosForIdAsync(unresolved.Name, remoteLibraryProviders, sourceCacheContext, logger, token);
var sourceInfo = await GetSourceInfosForIdAsync(unresolved.Name, applicableRemoteLibraryProviders, sourceCacheContext, logger, token);
var allVersions = new SortedSet<NuGetVersion>(sourceInfo.SelectMany(e => e.Value));

if (allVersions.Count == 0)
Expand All @@ -107,6 +121,15 @@ internal static async Task<RestoreLogMessage> GetMessageAsync(string targetGraph
.OrderBy(e => e, StringComparer.OrdinalIgnoreCase));

message = string.Format(CultureInfo.CurrentCulture, Strings.Error_NoPackageVersionsExist, unresolved.Name, sourceList);

if (isPackageSourceMappingEnabled && applicableRemoteLibraryProviders.Count != allRemoteLibraryProviders.Count)
{
string sourcesNotConsidered = FormatProviderNames(GetUnusedLibraryProviders(applicableRemoteLibraryProviders, allRemoteLibraryProviders));

message += ". " + string.Format(CultureInfo.CurrentCulture,
Strings.Log_SourceMappingEnabledNoMatchingPackageSources,
sourcesNotConsidered);
}
}
else
{
Expand All @@ -132,6 +155,15 @@ internal static async Task<RestoreLogMessage> GetMessageAsync(string targetGraph

lines.AddRange(sourceInfo.Select(e => FormatSourceInfo(e, range)));

if (isPackageSourceMappingEnabled && allRemoteLibraryProviders.Count != applicableRemoteLibraryProviders.Count)
{
lines.AddRange(GetUnusedLibraryProviders(applicableRemoteLibraryProviders, allRemoteLibraryProviders)
.OrderBy(e => e.Source.Name)
.Select(packageSource => string.Format(CultureInfo.CurrentCulture,
Strings.SourceNotConsidered,
packageSource.Source.Name)));
}

message = DiagnosticUtility.GetMultiLineMessage(lines);
}
}
Expand All @@ -144,13 +176,33 @@ internal static async Task<RestoreLogMessage> GetMessageAsync(string targetGraph
unresolved.ToString(),
targetGraphName);

if (isPackageSourceMappingEnabled && applicableRemoteLibraryProviders.Count != allRemoteLibraryProviders.Count)
{
message += " " + string.Format(CultureInfo.CurrentCulture,
Strings.Log_SourceMappingEnabledNoMatchingPackageSources,
FormatProviderNames(allRemoteLibraryProviders));
}

// Set again for clarity
code = NuGetLogCode.NU1100;
}

return RestoreLogMessage.CreateError(code, message, unresolved.Name, targetGraphName);
}

private static IEnumerable<IRemoteDependencyProvider> GetUnusedLibraryProviders(IList<IRemoteDependencyProvider> applicableRemoteLibraryProviders, IList<IRemoteDependencyProvider> allRemoteLibraryProviders)
{
return allRemoteLibraryProviders
.Where(e => !applicableRemoteLibraryProviders.Contains(e))
.Select(e => e);
}

private static string FormatProviderNames(IEnumerable<IRemoteDependencyProvider> allRemoteLibraryProviders)
{
return string.Join(", ", allRemoteLibraryProviders.Select(e => e.Source.Name)
.OrderBy(e => e, StringComparer.OrdinalIgnoreCase));
}

/// <summary>
/// True if no stable versions satisfy the range
/// but a pre-release version is found.
Expand Down
Loading