public void IsLockFileStillValid_TransitiveVersionsMovedToCentralFile_InvalidateLockFile() { // Arrange var framework = CommonFrameworks.NetStandard20; var projectName = "project"; var cpvm1 = new CentralPackageVersion("cpvm1", VersionRange.Parse("1.0.0")); var cpvm2 = new CentralPackageVersion("cpvm2", VersionRange.Parse("1.0.0")); var dependency1 = new LibraryDependency( new LibraryRange("cpvm1", versionRange: null, LibraryDependencyTarget.Package), LibraryDependencyType.Default, LibraryIncludeFlags.All, LibraryIncludeFlags.All, new List <Common.NuGetLogCode>(), autoReferenced: false, generatePathProperty: true, versionCentrallyManaged: false, LibraryDependencyReferenceType.Direct, aliases: "stuff"); var tfm = new TargetFrameworkInformation(); tfm.FrameworkName = framework; tfm.CentralPackageVersions.Add("cpvm1", cpvm1); tfm.CentralPackageVersions.Add("cpvm2", cpvm2); tfm.Dependencies.Add(dependency1); LibraryDependency.ApplyCentralVersionInformation(tfm.Dependencies, tfm.CentralPackageVersions); var project = new PackageSpec(new List <TargetFrameworkInformation>() { tfm }); project.RestoreMetadata = new ProjectRestoreMetadata() { ProjectUniqueName = projectName, CentralPackageVersionsEnabled = true }; DependencyGraphSpec dgSpec = new DependencyGraphSpec(); dgSpec.AddRestore(projectName); dgSpec.AddProject(project); var lockFile = new PackagesLockFileBuilder() .WithTarget(target => target .WithFramework(CommonFrameworks.NetStandard20) .WithDependency(dep => dep .WithId("cpvm1") .WithRequestedVersion(VersionRange.Parse("1.0.0")) .WithType(PackageDependencyType.Direct)) .WithDependency(dep => dep .WithId("cpvm2") .WithRequestedVersion(VersionRange.Parse("1.0.0")) .WithType(PackageDependencyType.Transitive))) .Build(); // The central package version cpvm2 has was changed from transitive to central var actual = PackagesLockFileUtilities.IsLockFileStillValid(dgSpec, lockFile); Assert.False(actual); }
public void LibraryDependency_ApplyCentralVersionInformation_NullArgumentCheck() { // Arrange List <LibraryDependency> packageReferences = new List <LibraryDependency>(); Dictionary <string, CentralPackageVersion> centralPackageVersions = new Dictionary <string, CentralPackageVersion>(); // Act + Assert Assert.Throws <ArgumentNullException>(() => LibraryDependency.ApplyCentralVersionInformation(null, centralPackageVersions)); Assert.Throws <ArgumentNullException>(() => LibraryDependency.ApplyCentralVersionInformation(packageReferences, null)); }
private static void AddCentralPackageVersions(PackageSpec spec, IEnumerable <IMSBuildItem> items) { var centralVersionsDependencies = CreateCentralVersionDependencies(items, spec.TargetFrameworks); foreach (var targetAlias in centralVersionsDependencies.Keys) { var frameworkInfo = spec.TargetFrameworks.FirstOrDefault(f => targetAlias.Equals(f.TargetAlias, StringComparison.OrdinalIgnoreCase)); frameworkInfo.CentralPackageVersions.AddRange(centralVersionsDependencies[targetAlias]); LibraryDependency.ApplyCentralVersionInformation(frameworkInfo.Dependencies, frameworkInfo.CentralPackageVersions); } }
public void LibraryDependency_ApplyCentralVersionInformation_CPVIsMergedTpPackageVersions() { var dep1 = new LibraryDependency() { LibraryRange = new LibraryRange() { Name = "fooMerged" }, }; var dep2 = new LibraryDependency() { LibraryRange = new LibraryRange() { Name = "barNotMerged", VersionRange = VersionRange.Parse("1.0.0") }, }; var dep3 = new LibraryDependency() { LibraryRange = new LibraryRange() { Name = "bazNotMerged" }, AutoReferenced = true }; List <LibraryDependency> deps = new List <LibraryDependency>() { dep1, dep2, dep3 }; var cpv1 = new CentralPackageVersion(dep1.Name.ToLower(), VersionRange.Parse("2.0.0")); var cpv2 = new CentralPackageVersion(dep2.Name.ToLower(), VersionRange.Parse("2.0.0")); var cpv3 = new CentralPackageVersion(dep3.Name.ToLower(), VersionRange.Parse("2.0.0")); Dictionary <string, CentralPackageVersion> cpvs = new Dictionary <string, CentralPackageVersion>(StringComparer.OrdinalIgnoreCase) { [cpv1.Name] = cpv1, [cpv2.Name] = cpv2, [cpv3.Name] = cpv3 }; // Act LibraryDependency.ApplyCentralVersionInformation(deps, cpvs); // Assert Assert.True(dep1.VersionCentrallyManaged); Assert.False(dep2.VersionCentrallyManaged); Assert.False(dep3.VersionCentrallyManaged); Assert.Equal("[2.0.0, )", dep1.LibraryRange.VersionRange.ToNormalizedString()); Assert.Equal("[1.0.0, )", dep2.LibraryRange.VersionRange.ToNormalizedString()); Assert.Null(dep3.LibraryRange.VersionRange); }
private static TargetFrameworkInformation CreateTargetFrameworkInformation(int centralVersionsDummyLoadCount = 0) { var nugetFramework = new NuGetFramework("net40"); var dependencyFoo = new LibraryDependency( new LibraryRange("foo", versionRange: null, LibraryDependencyTarget.Package), LibraryIncludeFlags.All, LibraryIncludeFlags.All, new List <Common.NuGetLogCode>(), autoReferenced: false, generatePathProperty: true, versionCentrallyManaged: false, LibraryDependencyReferenceType.Direct, aliases: null, versionOverride: null); var centralVersionFoo = new CentralPackageVersion("foo", VersionRange.Parse("1.0.0")); var centralVersionBar = new CentralPackageVersion("bar", VersionRange.Parse("2.0.0")); var dependencies = new List <LibraryDependency>() { dependencyFoo }; var assetTargetFallback = true; var warn = false; var tfi = new TargetFrameworkInformation() { AssetTargetFallback = assetTargetFallback, Dependencies = dependencies, Warn = warn, FrameworkName = nugetFramework, }; tfi.CentralPackageVersions.Add(centralVersionFoo.Name, centralVersionFoo); tfi.CentralPackageVersions.Add(centralVersionBar.Name, centralVersionBar); LibraryDependency.ApplyCentralVersionInformation(tfi.Dependencies, tfi.CentralPackageVersions); for (int i = 0; i < centralVersionsDummyLoadCount; i++) { var dummy = new CentralPackageVersion($"Dummy{i}", VersionRange.Parse("1.0.0")); tfi.CentralPackageVersions.Add(dummy.Name, dummy); } return(tfi); }
private static TargetFrameworkInformation CreateTargetFrameworkInformation(List <LibraryDependency> dependencies, List <CentralPackageVersion> centralVersionsDependencies) { var nugetFramework = new NuGetFramework("net40"); var tfi = new TargetFrameworkInformation() { AssetTargetFallback = true, Warn = false, FrameworkName = nugetFramework, Dependencies = dependencies, }; foreach (CentralPackageVersion cvd in centralVersionsDependencies) { tfi.CentralPackageVersions.Add(cvd.Name, cvd); } LibraryDependency.ApplyCentralVersionInformation(tfi.Dependencies, tfi.CentralPackageVersions); return(tfi); }
/// <summary> /// Emulates a JSON deserialization from project.json to PackageSpec in a post-project.json world /// </summary> private async Task <PackageSpec> GetPackageSpecAsync(ISettings settings) { await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync(); var projectReferences = await ProjectServices .ReferencesReader .GetProjectReferencesAsync(Common.NullLogger.Instance, CancellationToken.None); var targetFramework = await _vsProjectAdapter.GetTargetFrameworkAsync(); var packageReferences = (await ProjectServices .ReferencesReader .GetPackageReferencesAsync(targetFramework, CancellationToken.None)) .ToList(); var packageTargetFallback = MSBuildStringUtility.Split(_vsProjectAdapter.PackageTargetFallback) .Select(NuGetFramework.Parse) .ToList(); var assetTargetFallback = MSBuildStringUtility.Split(_vsProjectAdapter.AssetTargetFallback) .Select(NuGetFramework.Parse) .ToList(); var projectTfi = new TargetFrameworkInformation { FrameworkName = targetFramework, Dependencies = packageReferences, }; bool isCpvmEnabled = await IsCentralPackageManagementVersionsEnabledAsync(); if (isCpvmEnabled) { // Add the central version information and merge the information to the package reference dependencies projectTfi.CentralPackageVersions.AddRange(await GetCentralPackageVersionsAsync()); LibraryDependency.ApplyCentralVersionInformation(projectTfi.Dependencies, projectTfi.CentralPackageVersions); } // Apply fallback settings AssetTargetFallbackUtility.ApplyFramework(projectTfi, packageTargetFallback, assetTargetFallback); // Build up runtime information. var runtimes = await _vsProjectAdapter.GetRuntimeIdentifiersAsync(); var supports = await _vsProjectAdapter.GetRuntimeSupportsAsync(); var runtimeGraph = new RuntimeGraph(runtimes, supports); // In legacy CSProj, we only have one target framework per project var tfis = new TargetFrameworkInformation[] { projectTfi }; var projectName = _projectName ?? _projectUniqueName; string specifiedPackageId = await GetSpecifiedPackageIdAsync(); if (!string.IsNullOrWhiteSpace(specifiedPackageId)) { projectName = specifiedPackageId; } else { string specifiedAssemblyName = await GetSpecifiedAssemblyNameAsync(); if (!string.IsNullOrWhiteSpace(specifiedAssemblyName)) { projectName = specifiedAssemblyName; } } return(new PackageSpec(tfis) { Name = projectName, Version = new NuGetVersion(_vsProjectAdapter.Version), FilePath = _projectFullPath, RuntimeGraph = runtimeGraph, RestoreMetadata = new ProjectRestoreMetadata { ProjectStyle = ProjectStyle.PackageReference, OutputPath = await GetMSBuildProjectExtensionsPathAsync(), ProjectPath = _projectFullPath, ProjectName = projectName, ProjectUniqueName = _projectFullPath, OriginalTargetFrameworks = tfis .Select(tfi => tfi.FrameworkName.GetShortFolderName()) .ToList(), TargetFrameworks = new List <ProjectRestoreMetadataFrameworkInfo> { new ProjectRestoreMetadataFrameworkInfo(tfis[0].FrameworkName) { ProjectReferences = projectReferences?.ToList() } }, SkipContentFileWrite = true, CacheFilePath = await GetCacheFilePathAsync(), PackagesPath = GetPackagesPath(settings), Sources = GetSources(settings), FallbackFolders = GetFallbackFolders(settings), ConfigFilePaths = GetConfigFilePaths(settings), ProjectWideWarningProperties = WarningProperties.GetWarningProperties( treatWarningsAsErrors: _vsProjectAdapter.TreatWarningsAsErrors, noWarn: _vsProjectAdapter.NoWarn, warningsAsErrors: _vsProjectAdapter.WarningsAsErrors), RestoreLockProperties = new RestoreLockProperties( await _vsProjectAdapter.GetRestorePackagesWithLockFileAsync(), await _vsProjectAdapter.GetNuGetLockFilePathAsync(), await _vsProjectAdapter.IsRestoreLockedAsync()), CentralPackageVersionsEnabled = isCpvmEnabled } }); }
internal static TargetFrameworkInformation ToTargetFrameworkInformation( IVsTargetFrameworkInfo targetFrameworkInfo, bool cpvmEnabled, string projectFullPath) { var tfi = new TargetFrameworkInformation { FrameworkName = GetTargetFramework(targetFrameworkInfo.Properties, projectFullPath), TargetAlias = GetPropertyValueOrNull(targetFrameworkInfo.Properties, ProjectBuildProperties.TargetFramework) }; var ptf = MSBuildStringUtility.Split(GetPropertyValueOrNull(targetFrameworkInfo.Properties, ProjectBuildProperties.PackageTargetFallback)) .Select(NuGetFramework.Parse) .ToList(); var atf = MSBuildStringUtility.Split(GetPropertyValueOrNull(targetFrameworkInfo.Properties, ProjectBuildProperties.AssetTargetFallback)) .Select(NuGetFramework.Parse) .ToList(); // Update TFI with fallback properties AssetTargetFallbackUtility.ApplyFramework(tfi, ptf, atf); tfi.RuntimeIdentifierGraphPath = GetPropertyValueOrNull(targetFrameworkInfo.Properties, ProjectBuildProperties.RuntimeIdentifierGraphPath); if (targetFrameworkInfo.PackageReferences != null) { tfi.Dependencies.AddRange( targetFrameworkInfo.PackageReferences .Cast <IVsReferenceItem>() .Select(pr => ToPackageLibraryDependency(pr, cpvmEnabled))); } if (targetFrameworkInfo is IVsTargetFrameworkInfo2 targetFrameworkInfo2) { if (targetFrameworkInfo2.PackageDownloads != null) { tfi.DownloadDependencies.AddRange( targetFrameworkInfo2.PackageDownloads .Cast <IVsReferenceItem>() .Select(ToPackageDownloadDependency)); } if (cpvmEnabled && targetFrameworkInfo is IVsTargetFrameworkInfo3 targetFrameworkInfo3) { if (targetFrameworkInfo3.CentralPackageVersions != null) { tfi.CentralPackageVersions.AddRange( targetFrameworkInfo3.CentralPackageVersions .Cast <IVsReferenceItem>() .Select(ToCentralPackageVersion) .Distinct(CentralPackageVersionNameComparer.Default) .ToDictionary(cpv => cpv.Name)); } // Merge the central version information to the package information LibraryDependency.ApplyCentralVersionInformation(tfi.Dependencies, tfi.CentralPackageVersions); } if (targetFrameworkInfo2.FrameworkReferences != null) { PopulateFrameworkDependencies(tfi, targetFrameworkInfo2); } } return(tfi); }