Skip to content

Commit

Permalink
Revert "Enable central package management implicitly when Directory.P…
Browse files Browse the repository at this point in the history
…ackages.props exists (#5572)" (#5880)
  • Loading branch information
jeffkl authored Jun 28, 2024
1 parent 4fe0c55 commit 324d727
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 178 deletions.
33 changes: 17 additions & 16 deletions src/NuGet.Core/NuGet.Build.Tasks/NuGet.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,33 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and
Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->

<Project>

<!--
Determine the path to the 'Directory.Packages.props' file, if the user did not:
1. Set $(ManagePackageVersionsCentrally) to false
2. Set $(ImportDirectoryPackagesProps) to false
3. Already specify the path to a 'Directory.Packages.props' file via $(DirectoryPackagesPropsPath)
Import 'Directory.Packages.props' which will contain centralized packages for all the projects and solutions under
the directory in which the file is present. This is similar to 'Directory.Build.props/targets' logic which is present
in the common props/targets which serve a similar purpose.
-->
<PropertyGroup Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And '$(DirectoryPackagesPropsPath)' == ''">
<_DirectoryPackagesPropsFile Condition="'$(_DirectoryPackagesPropsFile)' == ''">Directory.Packages.props</_DirectoryPackagesPropsFile>
<_DirectoryPackagesPropsBasePath Condition="'$(_DirectoryPackagesPropsBasePath)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove('$(MSBuildProjectDirectory)', '$(_DirectoryPackagesPropsFile)'))</_DirectoryPackagesPropsBasePath>
<DirectoryPackagesPropsPath Condition="'$(_DirectoryPackagesPropsBasePath)' != '' and '$(_DirectoryPackagesPropsFile)' != ''">$([MSBuild]::NormalizePath('$(_DirectoryPackagesPropsBasePath)', '$(_DirectoryPackagesPropsFile)'))</DirectoryPackagesPropsPath>

<PropertyGroup>
<ImportDirectoryPackagesProps Condition="'$(ImportDirectoryPackagesProps)' == ''">true</ImportDirectoryPackagesProps>
</PropertyGroup>

<!--
Default $(ManagePackageVersionsCentrally) to true, import Directory.Packages.props, and set $(CentralPackageVersionsFileImported) to true if the user did not:
1. Set $(ManagePackageVersionsCentrally) to false
2. Set $(ImportDirectoryPackagesProps) to false
3. The path specified in $(DirectoryPackagesPropsPath) exists
Determine the path to the 'Directory.Packages.props' file, if the user did not disable $(ImportDirectoryPackagesProps) and
they did not already specify an absolute path to use via $(DirectoryPackagesPropsPath)
-->
<PropertyGroup Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)')">
<ManagePackageVersionsCentrally Condition="'$(ManagePackageVersionsCentrally)' == ''">true</ManagePackageVersionsCentrally>
<PropertyGroup Condition="'$(ImportDirectoryPackagesProps)' == 'true' and '$(DirectoryPackagesPropsPath)' == ''">
<_DirectoryPackagesPropsFile Condition="'$(_DirectoryPackagesPropsFile)' == ''">Directory.Packages.props</_DirectoryPackagesPropsFile>
<_DirectoryPackagesPropsBasePath Condition="'$(_DirectoryPackagesPropsBasePath)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove('$(MSBuildProjectDirectory)', '$(_DirectoryPackagesPropsFile)'))</_DirectoryPackagesPropsBasePath>
<DirectoryPackagesPropsPath Condition="'$(_DirectoryPackagesPropsBasePath)' != '' and '$(_DirectoryPackagesPropsFile)' != ''">$([MSBuild]::NormalizePath('$(_DirectoryPackagesPropsBasePath)', '$(_DirectoryPackagesPropsFile)'))</DirectoryPackagesPropsPath>
</PropertyGroup>

<Import Project="$(DirectoryPackagesPropsPath)" Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)')" />
<Import Project="$(DirectoryPackagesPropsPath)" Condition="'$(ImportDirectoryPackagesProps)' == 'true' and '$(DirectoryPackagesPropsPath)' != '' and Exists('$(DirectoryPackagesPropsPath)')"/>

<PropertyGroup Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)')">
<PropertyGroup Condition="'$(ImportDirectoryPackagesProps)' == 'true' and '$(DirectoryPackagesPropsPath)' != '' and Exists('$(DirectoryPackagesPropsPath)')">
<CentralPackageVersionsFileImported>true</CentralPackageVersionsFileImported>
</PropertyGroup>

</Project>
6 changes: 1 addition & 5 deletions test/EndToEnd/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>
</Project>
<Project />
Original file line number Diff line number Diff line change
Expand Up @@ -9186,6 +9186,7 @@ public async Task RestoreNetCore_CPVMProject_DirectDependencyCentralVersionChang
"a",
pathContext.SolutionRoot,
NuGetFramework.Parse("net46"));
projectA.Properties.Add("ManagePackageVersionsCentrally", "true");
projectA.Properties.Add("RestoreLockedMode", "true");
projectA.Properties.Add("RestorePackagesWithLockFile", "true");

Expand Down Expand Up @@ -9270,6 +9271,7 @@ public async Task RestoreNetCore_CPVMProject_TransitiveDependencyCentralVersionC
"a",
pathContext.SolutionRoot,
NuGetFramework.Parse("net46"));
projectA.Properties.Add("ManagePackageVersionsCentrally", "true");
projectA.Properties.Add("RestoreLockedMode", "true");
projectA.Properties.Add("RestorePackagesWithLockFile", "true");

Expand Down Expand Up @@ -9352,6 +9354,7 @@ public async Task RestoreNetCore_CPVMProject_RemovedCentralDirectDependency_Fail
"a",
pathContext.SolutionRoot,
NuGetFramework.Parse("net46"));
projectA.Properties.Add("ManagePackageVersionsCentrally", "true");
projectA.Properties.Add("RestoreLockedMode", "true");
projectA.Properties.Add("RestorePackagesWithLockFile", "true");

Expand Down Expand Up @@ -9425,6 +9428,7 @@ public async Task RestoreNetCore_CPVMProject_RemovedCentralTransitiveDependency_
"a",
pathContext.SolutionRoot,
NuGetFramework.Parse("net46"));
projectA.Properties.Add("ManagePackageVersionsCentrally", "true");
projectA.Properties.Add("RestoreLockedMode", "true");
projectA.Properties.Add("RestorePackagesWithLockFile", "true");

Expand Down Expand Up @@ -9498,6 +9502,7 @@ public async Task RestoreNetCore_CPVMProject_MoveTransitiveDependnecyToCentralFi
"a",
pathContext.SolutionRoot,
NuGetFramework.Parse("net46"));
projectA.Properties.Add("ManagePackageVersionsCentrally", "true");
projectA.Properties.Add("RestoreLockedMode", "true");
projectA.Properties.Add("RestorePackagesWithLockFile", "true");

Expand Down Expand Up @@ -9570,6 +9575,7 @@ public async Task RestoreNetCore_CPVMProject_AddRemoveNotProjectRelatedEntriesTo
"a",
pathContext.SolutionRoot,
NuGetFramework.Parse("net46"));
projectA.Properties.Add("ManagePackageVersionsCentrally", "true");
projectA.Properties.Add("RestoreLockedMode", "true");
projectA.Properties.Add("RestorePackagesWithLockFile", "true");

Expand Down Expand Up @@ -9672,16 +9678,19 @@ public async Task RestoreNetCore_CPVMProject_DowngradedByCentralDirectDependency
"a",
pathContext.SolutionRoot,
netcoreapp2);
projectA.Properties.Add("ManagePackageVersionsCentrally", "true");

var projectB = SimpleTestProjectContext.CreateNETCore(
"b",
pathContext.SolutionRoot,
netcoreapp2);
projectB.Properties.Add("ManagePackageVersionsCentrally", "true");

var projectC = SimpleTestProjectContext.CreateNETCore(
"c",
pathContext.SolutionRoot,
netcoreapp2);
projectC.Properties.Add("ManagePackageVersionsCentrally", "true");

var packageX100 = new SimpleTestPackageContext()
{
Expand Down Expand Up @@ -9814,7 +9823,8 @@ SimpleTestPackageContext createTestPackage(string name, string version, List<Sim
"projectA",
pathContext.SolutionRoot,
NuGetFramework.Parse("netcoreapp2.0"));
projectA.Properties.Add(ProjectBuildProperties.CentralPackageTransitivePinningEnabled, bool.TrueString);
projectA.Properties.Add(ProjectBuildProperties.ManagePackageVersionsCentrally, "true");
projectA.Properties.Add(ProjectBuildProperties.CentralPackageTransitivePinningEnabled, "true");

// the package references defined in the project should not have version
var packageBNoVersion = createTestPackage("B", null, packagesForProject);
Expand Down Expand Up @@ -9940,13 +9950,13 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
var libraries = assetsFile.Libraries.Select(l => $"{l.Name}.{l.Version}").OrderBy(n => n).ToList();
Assert.Equal(expectedLibraries, libraries);

var centralFileDependencyGroups = assetsFile
var centralfileDependencyGroups = assetsFile
.CentralTransitiveDependencyGroups
.SelectMany(g => g.TransitiveDependencies.Select(t => $"{g.FrameworkName}_{t.LibraryRange.Name}.{t.LibraryRange.VersionRange.OriginalString}")).ToList();

var expectedCentralFileDependencyGroups = new List<string>() { $"{framework.DotNetFrameworkName}_P.[3.0.0, )", $"{framework.DotNetFrameworkName}_S.[3.0.0, )" };
var expectedCentralfileDependencyGroups = new List<string>() { $"{framework.DotNetFrameworkName}_P.[3.0.0, )", $"{framework.DotNetFrameworkName}_S.[3.0.0, )" };

Assert.Equal(expectedCentralFileDependencyGroups, centralFileDependencyGroups);
Assert.Equal(expectedCentralfileDependencyGroups, centralfileDependencyGroups);
}
}

Expand Down Expand Up @@ -10064,6 +10074,7 @@ SimpleTestPackageContext createTestPackage(string name, string version, List<Sim
"projectA",
pathContext.SolutionRoot,
framework);
projectA.Properties.Add("ManagePackageVersionsCentrally", "true");

// the package references defined in the project should not have version
var packageBNoVersion = createTestPackage("B", null, packagesForProject);
Expand Down Expand Up @@ -10101,70 +10112,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
var libraries = assetsFile.Libraries.Select(l => $"{l.Name}.{l.Version}").OrderBy(n => n).ToList();
Assert.Equal(expectedLibraries, libraries);

var centralFileDependencyGroups = assetsFile
var centralfileDependencyGroups = assetsFile
.CentralTransitiveDependencyGroups
.SelectMany(g => g.TransitiveDependencies.Select(t => $"{g.FrameworkName}_{t.LibraryRange.Name}.{t.LibraryRange.VersionRange.OriginalString}")).ToList();

Assert.Equal(0, centralFileDependencyGroups.Count);
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
[InlineData(null)]
public async Task RestoreNetCore_CPVMProject_ManagePackageVersionsCentrally_CanBeDisabled(bool? managePackageVersionsCentrally)
{
// Arrange
using var pathContext = new SimpleTestPathContext();

var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot);

var packageA = new SimpleTestPackageContext("A", "1.0.0");

await SimpleTestPackageUtility.CreateFolderFeedV3Async(pathContext.PackageSource, packageA);

var projectA = SimpleTestProjectContext.CreateNETCore("projectA", pathContext.SolutionRoot, "net472");

if (managePackageVersionsCentrally is null || managePackageVersionsCentrally == true)
{
packageA.Version = null;
}

projectA.AddPackageToAllFrameworks(packageA);

solution.CentralPackageVersionsManagementFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot, managePackageVersionsCentrally)
.SetPackageVersion("A", "1.0.0");

solution.Projects.Add(projectA);
solution.Create(pathContext.SolutionRoot);

// Act
var result = Util.RestoreSolution(pathContext, testOutputHelper: _testOutputHelper);

// Assert
result.Success.Should().BeTrue();

var assetFileReader = new LockFileFormat();
var assetsFile = assetFileReader.Read(projectA.AssetsFileOutputPath);

assetsFile.Libraries.Select(l => $"{l.Name}.{l.Version}").Should().BeEquivalentTo(new string[] { "A.1.0.0" });

var targetFramework = assetsFile.PackageSpec.TargetFrameworks.Should().ContainSingle();

if (managePackageVersionsCentrally is null || managePackageVersionsCentrally == true)
{
targetFramework.Subject.Dependencies.Should().ContainSingle()
.Which.VersionCentrallyManaged.Should().BeTrue();

targetFramework.Subject.CentralPackageVersions.Should().ContainSingle()
.Which.Value.Should().Be(new CentralPackageVersion("A", VersionRange.Parse("1.0.0")));
}
else
{
targetFramework.Subject.Dependencies.Should().ContainSingle()
.Which.VersionCentrallyManaged.Should().BeFalse();
targetFramework.Subject.CentralPackageVersions.Should().BeEmpty();
Assert.Equal(0, centralfileDependencyGroups.Count);
}
}

Expand Down Expand Up @@ -10298,6 +10250,8 @@ public async Task RestoreNetCore_CPVMProject_WithVersionOverride_Succeeds()

var projectA = SimpleTestProjectContext.CreateNETCore("projectA", pathContext.SolutionRoot, framework);

projectA.Properties.Add("ManagePackageVersionsCentrally", "true");

await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
new[]
Expand Down Expand Up @@ -10356,11 +10310,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
var libraries = assetsFile.Libraries.Select(l => $"{l.Name}.{l.Version}").OrderBy(n => n).ToList();
Assert.Equal(expectedLibraries, libraries);

var centralFileDependencyGroups = assetsFile
var centralfileDependencyGroups = assetsFile
.CentralTransitiveDependencyGroups
.SelectMany(g => g.TransitiveDependencies.Select(t => $"{g.FrameworkName}_{t.LibraryRange.Name}.{t.LibraryRange.VersionRange.OriginalString}")).ToList();

Assert.Equal(0, centralFileDependencyGroups.Count);
Assert.Equal(0, centralfileDependencyGroups.Count);
}
}

Expand All @@ -10377,6 +10331,7 @@ public async Task RestoreNetCore_CPVMProject_WithVersionOverrideDisabled_Fails()

var projectA = SimpleTestProjectContext.CreateNETCore("projectA", pathContext.SolutionRoot, framework);

projectA.Properties.Add(ProjectBuildProperties.ManagePackageVersionsCentrally, bool.TrueString);
projectA.Properties.Add(ProjectBuildProperties.CentralPackageVersionOverrideEnabled, bool.FalseString);

await SimpleTestPackageUtility.CreateFolderFeedV3Async(
Expand Down Expand Up @@ -11377,6 +11332,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(

var xml = projectA.GetXML();

ProjectFileUtils.AddProperty(
xml,
"ManagePackageVersionsCentrally",
"true");

ProjectFileUtils.AddItem(
xml,
"PackageReference",
Expand Down Expand Up @@ -11587,6 +11547,7 @@ SimpleTestProjectContext CreateProject(SimpleTestPathContext pathContext, string
pathContext.SolutionRoot,
"net472");

projectContext.Properties.Add("ManagePackageVersionsCentrally", "true");
projectContext.Properties.Add("CentralPackageTransitivePinningEnabled", centralPackageTransitivePinningEnabled.ToString());

if (referencedProject != null)
Expand Down
Loading

0 comments on commit 324d727

Please sign in to comment.