diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs index 1fd094bfc6b..fe0d1680812 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs @@ -132,10 +132,11 @@ public bool AreCentralVersionRequirementsSatisfied(PackageReferenceArgs packageR string directoryPackagesPropsPath = project.GetPropertyValue(DirectoryPackagesPropsPathPropertyName); // Get VersionOverride if it exisits in the package reference. - IEnumerable dependenciesWithVersionOverride = packageSpec.TargetFrameworks.SelectMany(tfm => tfm.Dependencies.Where(d => !d.AutoReferenced && d.VersionOverride != null)); + IEnumerable dependenciesWithVersionOverride = null; if (packageSpec.RestoreMetadata.CentralPackageVersionOverrideDisabled) { + dependenciesWithVersionOverride = packageSpec.TargetFrameworks.SelectMany(tfm => tfm.Dependencies.Where(d => !d.AutoReferenced && d.VersionOverride != null)); // Emit a error if VersionOverride was specified for a package reference but that functionality is disabled foreach (var item in dependenciesWithVersionOverride) { @@ -188,7 +189,7 @@ public bool AreCentralVersionRequirementsSatisfied(PackageReferenceArgs packageR ProjectItem packageReference = project.Items.Where(item => item.ItemType == PACKAGE_REFERENCE_TYPE_TAG && item.EvaluatedInclude.Equals(packageReferenceArgs.PackageId)).LastOrDefault(); ProjectItem packageVersionInProps = packageVersions.LastOrDefault(); - var versionOverride = dependenciesWithVersionOverride.FirstOrDefault(d => d.Name.Equals(packageReferenceArgs.PackageId)); + var versionOverride = dependenciesWithVersionOverride?.FirstOrDefault(d => d.Name.Equals(packageReferenceArgs.PackageId)); // If package reference exists and the user defined a VersionOverride or PackageVersions but didn't specified a version, no-op if (packageReference != null && (versionOverride != null || packageVersionInProps != null) && packageReferenceArgs.NoVersion)