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

Fix ability for individual projects to enable or disable Central Package Management #5866

Merged
merged 2 commits into from
Jun 24, 2024
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
25 changes: 14 additions & 11 deletions src/NuGet.Core/NuGet.Build.Tasks/NuGet.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,32 @@ 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)
1. Set $(ImportDirectoryPackagesProps) to false
2. Already specify the path to a 'Directory.Packages.props' file via $(DirectoryPackagesPropsPath)
-->
<PropertyGroup Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And '$(DirectoryPackagesPropsPath)' == ''">
<PropertyGroup Condition="'$(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>

<!--
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
Default $(ManagePackageVersionsCentrally) to true if a Directory.Packages.props will be imported and a value is not already set.
-->
<PropertyGroup Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)')">
<PropertyGroup Condition="'$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)')">
<ManagePackageVersionsCentrally Condition="'$(ManagePackageVersionsCentrally)' == ''">true</ManagePackageVersionsCentrally>
</PropertyGroup>

<Import Project="$(DirectoryPackagesPropsPath)" Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)')" />
<!--
Import Directory.Packages.props if it exists and has not already been imported. It is imported even if $(ManagePackageVersionsCentrally)
is set to false so that users can enable it for particular projects.
-->
<Import Project="$(DirectoryPackagesPropsPath)" Condition="'$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)') And '$(CentralPackageVersionsFileImported)' != 'true'" />

<PropertyGroup Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)')">
<!--
Set a property indicating that the Directory.Packages.props file has been imported. This is used to prevent the file from being imported again.
-->
<PropertyGroup Condition="'$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)')">
<CentralPackageVersionsFileImported>true</CentralPackageVersionsFileImported>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ string FindMsbuildWithVsWhere()
internal CommandRunnerResult RunMsBuild(string workingDirectory, string args, bool ignoreExitCode = false, ITestOutputHelper testOutputHelper = null)
{
var restoreDllPath = Path.Combine(_testDir, "NuGet.Build.Tasks.dll");
var nugetRestorePropsPath = Path.Combine(_testDir, "NuGet.props");
var nugetRestoreTargetsPath = Path.Combine(_testDir, "NuGet.targets");

var result = CommandRunner.Run(_msbuildPath.Value,
workingDirectory,
$"/p:NuGetRestoreTargets={nugetRestoreTargetsPath} /p:RestoreTaskAssemblyFile={restoreDllPath} /p:ImportNuGetBuildTasksPackTargetsFromSdk=true {args}",
$"/p:NuGetPropsFile={nugetRestorePropsPath} /p:NuGetRestoreTargets={nugetRestoreTargetsPath} /p:RestoreTaskAssemblyFile={restoreDllPath} /p:ImportNuGetBuildTasksPackTargetsFromSdk=true {args}",
environmentVariables: _processEnvVars,
testOutputHelper: testOutputHelper);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1628,5 +1628,61 @@ await SimpleTestPackageUtility.CreatePackagesAsync(
replayedOutput.Should().Contain($"a known high severity vulnerability", Exactly.Twice());
replayedOutput.Should().Contain($"a known critical severity vulnerability", Exactly.Twice());
}

[Theory]
[InlineData(false, null)] // Disabled in Directory.Build.props, not set in Directory.Packages.props
[InlineData(false, false)] // Disabled in Directory.Build.props, disabled in Directory.Packages.props
[InlineData(null, false)] // Not set in Directory.Build.props, disabled in Directory.Packages.props
public async Task MsBuildRestore_WithCPMDisabled_IndividualProjectCanEnableCPM(bool? enabledInDirectoryBuildProps, bool? enabledInDirectoryPackagesProps)
{
// Arrange
using var pathContext = new SimpleTestPathContext();

var packageX = new SimpleTestPackageContext("x", "1.0.0");

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

// Directory.Build.props disables CPM which happens before Directory.Packages.props is imported
File.WriteAllText(
Path.Combine(pathContext.SolutionRoot, "Directory.Build.props"),
$@"<Project>
<PropertyGroup>
{(enabledInDirectoryBuildProps == true ? "<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>" : enabledInDirectoryBuildProps == false ? "<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>" : "")}
</PropertyGroup>
</Project>");

File.WriteAllText(
Path.Combine(pathContext.SolutionRoot, "Directory.Packages.props"),
$@"<Project>
<PropertyGroup>
{(enabledInDirectoryPackagesProps == true ? "<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>" : enabledInDirectoryPackagesProps == false ? "<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>" : "")}
</PropertyGroup>
<ItemGroup>
<PackageVersion Include=""{packageX.Id}"" Version=""{packageX.Version}"" />
</ItemGroup>
</Project>");

SimpleTestProjectContext projectA = SimpleTestProjectContext.CreateNETCoreWithSDK("a", pathContext.SolutionRoot, FrameworkConstants.CommonFrameworks.Net472.GetShortFolderName());

// The project enables CPM and Directory.Packages.props was already imported even if CPM was diabled
projectA.Properties.Add("ManagePackageVersionsCentrally", bool.TrueString);
projectA.Properties.Add("TreatWarningsAsErrors", bool.TrueString);

projectA.AddPackageToAllFrameworks(new SimpleTestPackageContext()
{
Id = packageX.Id,
Version = null
});

var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot, projectA);

solution.Create(pathContext.SolutionRoot);

// Act
CommandRunnerResult result = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore {projectA.ProjectPath}", ignoreExitCode: true, testOutputHelper: _testOutputHelper);

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