diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommand.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommand.cs index f9dcecc933b..00dd0f19ca1 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommand.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommand.cs @@ -126,7 +126,7 @@ public async Task 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; @@ -208,7 +208,7 @@ public async Task 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; @@ -463,7 +463,7 @@ private bool HasValidPlatformVersions() } } - private async Task AreCentralVersionRequirementsSatisfiedAsync(RestoreRequest restoreRequest) + private async Task AreCentralVersionRequirementsSatisfiedAsync(RestoreRequest restoreRequest, int httpSourcesCount) { if (restoreRequest?.Project?.RestoreMetadata == null || !restoreRequest.Project.RestoreMetadata.CentralPackageVersionsEnabled) { @@ -483,10 +483,10 @@ private async Task 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. diff --git a/test/NuGet.Core.Tests/NuGet.Commands.Test/RestoreCommandTests.cs b/test/NuGet.Core.Tests/NuGet.Commands.Test/RestoreCommandTests.cs index fbe4d659c16..8bb91211c8e 100644 --- a/test/NuGet.Core.Tests/NuGet.Commands.Test/RestoreCommandTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Commands.Test/RestoreCommandTests.cs @@ -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"); } } }