Skip to content

Commit

Permalink
Fix issue with CentralPackageVersions logging error when using a cust…
Browse files Browse the repository at this point in the history
…om import pattern. (#325)

#322 didn't take into account that if Microsoft.Build.CentralPackageVersions is imported in a custom way.  This change now makes sure it was imported after Microsoft.Common.targets

Fixes #324
  • Loading branch information
jeffkl authored Dec 2, 2021
1 parent af63eab commit 99a3dc3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,44 @@ public void VersionOverridesWithoutCentralVersionsCanBeDisabled(string projectFi
buildOutput.Errors.ShouldBe(new[] { $"The package reference \'Orphan\' must have a version defined in \'{packagesProps.FullPath}\'." });
}

private ProjectCreator WritePackagesProps()
[Fact]
public void WorksWithCustomImportPattern()
{
ProjectCreator.Create()
.Save(Path.Combine(TestRootPath, "Directory.Build.props"));
WritePackagesProps(writeDirectoryBuildPropsTargets: false);

ProjectCreator.Create()
ProjectCreator customProps = ProjectCreator.Create()
.ImportSdk("Sdk.props", "Microsoft.NET.Sdk")
.Save(Path.Combine(TestRootPath, "Custom.props"));

ProjectCreator customTargets = ProjectCreator.Create()
.ImportSdk("Sdk.targets", "Microsoft.NET.Sdk")
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.props"))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets"))
.Save(Path.Combine(TestRootPath, "Directory.Build.targets"));
.Save(Path.Combine(TestRootPath, "Custom.targets"));

ProjectCreator.Create()
.Import(customProps)
.Property("TargetFramework", "net45")
.ItemPackageReference("Foo")
.Import(customTargets)
.Save(GetTempFileWithExtension(".csproj"))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeTrue(buildOutput.GetConsoleLog());
}

private ProjectCreator WritePackagesProps(bool writeDirectoryBuildPropsTargets = true)
{
if (writeDirectoryBuildPropsTargets)
{
ProjectCreator.Create()
.Save(Path.Combine(TestRootPath, "Directory.Build.props"));

ProjectCreator.Create()
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.props"))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets"))
.Save(Path.Combine(TestRootPath, "Directory.Build.targets"));
}

return ProjectCreator.Templates
.PackagesProps(
Expand Down
2 changes: 1 addition & 1 deletion src/CentralPackageVersions/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<!--
Keep track if this file was imported before Directory.Build.targets
-->
<_WasMicrosoftCentralPackageVersionsSdkImportedInDirectoryBuildTargets Condition="'$(_WasMicrosoftCentralPackageVersionsSdkImportedInDirectoryBuildTargets)' == '' And '$(DirectoryBuildTargetsPath)' == ''">false</_WasMicrosoftCentralPackageVersionsSdkImportedInDirectoryBuildTargets>
<_WasMicrosoftCentralPackageVersionsSdkImportedInDirectoryBuildTargets Condition="'$(_WasMicrosoftCentralPackageVersionsSdkImportedInDirectoryBuildTargets)' == '' And '$(CommonTargetsPath)' == ''">false</_WasMicrosoftCentralPackageVersionsSdkImportedInDirectoryBuildTargets>
<!--
Walk up the directory tree looking for a Packages.props, unless a user has already specified a path.
-->
Expand Down

0 comments on commit 99a3dc3

Please sign in to comment.