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

Ignore local sources when enforcing package source mapping with central package management #4764

Merged
merged 2 commits into from
Aug 31, 2022
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
12 changes: 6 additions & 6 deletions src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public async Task<RestoreResult> ExecuteAsync(CancellationToken token)
bool isPackageSourceMappingEnabled = _request?.PackageSourceMapping.IsEnabled ?? false;
telemetry.TelemetryEvent[PackageSourceMappingIsMappingEnabled] = isPackageSourceMappingEnabled;
telemetry.TelemetryEvent[SourcesCount] = _request.DependencyProviders.RemoteProviders.Count;
var httpSourcesCount = _request.DependencyProviders.RemoteProviders.Where(e => e.IsHttp).Count();
int httpSourcesCount = _request.DependencyProviders.RemoteProviders.Where(e => e.IsHttp).Count();
telemetry.TelemetryEvent[HttpSourcesCount] = httpSourcesCount;
telemetry.TelemetryEvent[LocalSourcesCount] = _request.DependencyProviders.RemoteProviders.Count - httpSourcesCount;
telemetry.TelemetryEvent[FallbackFoldersCount] = _request.DependencyProviders.FallbackPackageFolders.Count;
Expand Down Expand Up @@ -208,7 +208,7 @@ public async Task<RestoreResult> ExecuteAsync(CancellationToken token)
}
telemetry.TelemetryEvent[NoOpResult] = false; // Getting here means we did not no-op.

if (!await AreCentralVersionRequirementsSatisfiedAsync(_request))
if (!await AreCentralVersionRequirementsSatisfiedAsync(_request, httpSourcesCount))
{
// the errors will be added to the assets file
_success = false;
Expand Down Expand Up @@ -463,7 +463,7 @@ private bool HasValidPlatformVersions()
}
}

private async Task<bool> AreCentralVersionRequirementsSatisfiedAsync(RestoreRequest restoreRequest)
private async Task<bool> AreCentralVersionRequirementsSatisfiedAsync(RestoreRequest restoreRequest, int httpSourcesCount)
{
if (restoreRequest?.Project?.RestoreMetadata == null || !restoreRequest.Project.RestoreMetadata.CentralPackageVersionsEnabled)
{
Expand All @@ -483,10 +483,10 @@ private async Task<bool> AreCentralVersionRequirementsSatisfiedAsync(RestoreRequ
return false;
}

// Log a warning if there are more than one configured source and package source mapping is not enabled
if (restoreRequest.Project.RestoreMetadata.Sources.Count > 1 && !restoreRequest.PackageSourceMapping.IsEnabled)
if (!restoreRequest.PackageSourceMapping.IsEnabled && httpSourcesCount > 0)
{
await _logger.LogAsync(RestoreLogMessage.CreateWarning(NuGetLogCode.NU1507, string.Format(CultureInfo.CurrentCulture, Strings.Warning_CentralPackageVersions_MultipleSourcesWithoutPackageSourceMapping, restoreRequest.Project.RestoreMetadata.Sources.Count, string.Join(", ", restoreRequest.Project.RestoreMetadata.Sources.Select(i => i.Name)))));
// Log a warning if there are more than one configured source and package source mapping is not enabled
await _logger.LogAsync(RestoreLogMessage.CreateWarning(NuGetLogCode.NU1507, string.Format(CultureInfo.CurrentCulture, Strings.Warning_CentralPackageVersions_MultipleSourcesWithoutPackageSourceMapping, httpSourcesCount, string.Join(", ", restoreRequest.DependencyProviders.RemoteProviders.Where(i => i.IsHttp).Select(i => i.Source.Name)))));
}

// The dependencies should not have versions explicitly defined if cpvm is enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1682,8 +1682,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
// NU1507: There are 3 package sources defined in your configuration. When using central package management, please map your package sources with package source mapping (https://aka.ms/nuget-package-source-mapping) or specify a single package source. The following sources are defined: D:\NuGet\.test\work\298ed94f\653dd6db\source, https://feed1, https://feed2
logger.WarningMessages.Should()
.Contain(i => i.Contains(NuGetLogCode.NU1507.ToString()))
.Which.Should().Contain("There are 3 package sources defined in your configuration")
.And.Contain($"The following sources are defined: {pathContext.PackageSource}, https://feed1, https://feed2");
.Which.Should().Contain("There are 2 package sources defined in your configuration")
.And.Contain($"The following sources are defined: https://feed1, https://feed2");
}
}
}
Expand Down