Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,37 @@ public void IsDisabledForProjectsWithPackagesConfigOrDoNotSupportPackageReferenc
enableCentralPackageVersions.ShouldBe("false");
}

[Fact]
public void LogErrorIfImportedInDirectoryBuildProps()
{
ProjectCreator.Create()
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.props"))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets"))
.Save(GetTempFile("Directory.Build.props"));

ProjectCreator.Create()
.Save(GetTempFile("Directory.Build.targets"));

ProjectCreator.Templates
.PackagesProps(
path: GetTempFile("Packages.props"),
packageReferences: new Dictionary<string, string>
{
["Foo"] = "1.2.3",
})
.Save();

ProjectCreator.Templates
.SdkCsproj(projectCreator: creator => creator
.ItemPackageReference("Foo"))
.Save(GetTempFile("Test.csproj"))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeFalse(buildOutput.GetConsoleLog());

buildOutput.Errors.ShouldBe(new[] { "Microsoft.Build.CentralPackageVersions was not imported in Directory.Build.targets. See https://github.com/microsoft/MSBuildSdks/tree/main/src/CentralPackageVersions for more information on how to include this SDK." }, buildOutput.GetConsoleLog());
}

[Theory]
[InlineData(".csproj")]
[InlineData(".fsproj")]
Expand Down
2 changes: 0 additions & 2 deletions src/CentralPackageVersions/Sdk/Sdk.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@
<UsingMicrosoftCentralPackageVersionsSdk>true</UsingMicrosoftCentralPackageVersionsSdk>
</PropertyGroup>

<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition=" '$(MicrosoftCommonPropsHasBeenImported)' != 'true' And Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props') "/>

<Import Project="$(CustomAfterCentralPackageVersionsProps)" Condition=" '$(CustomAfterCentralPackageVersionsProps)' != '' And Exists('$(CustomAfterCentralPackageVersionsProps)') " />
</Project>
12 changes: 11 additions & 1 deletion src/CentralPackageVersions/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
Condition=" '$(EnableCentralPackageVersions)' != 'false' And '$(CustomBeforeCentralPackageVersionsTargets)' != '' And Exists('$(CustomBeforeCentralPackageVersionsTargets)') " />

<PropertyGroup Condition=" '$(EnableCentralPackageVersions)' != 'false' ">
<!--
Keep track if this file was imported before Directory.Build.targets
-->
<_WasMicrosoftCentralPackageVersionsSdkImportedInDirectoryBuildTargets Condition="'$(_WasMicrosoftCentralPackageVersionsSdkImportedInDirectoryBuildTargets)' == '' And '$(DirectoryBuildTargetsPath)' == ''">false</_WasMicrosoftCentralPackageVersionsSdkImportedInDirectoryBuildTargets>
<!--
Walk up the directory tree looking for a Packages.props, unless a user has already specified a path.
-->
Expand Down Expand Up @@ -151,7 +155,13 @@
<_DuplicateGlobalPackageReference Include="@(_OriginalPackageReference)"
Condition=" '@(GlobalPackageReference)' == '@(_OriginalPackageReference)' and '%(Identity)' != '' " />
</ItemGroup>


<!--
Log an error if this was imported before Directory.Build.targets
-->
<Error Text="Microsoft.Build.CentralPackageVersions was not imported in Directory.Build.targets. See https://github.com/microsoft/MSBuildSdks/tree/main/src/CentralPackageVersions for more information on how to include this SDK."
Condition="'$(_WasMicrosoftCentralPackageVersionsSdkImportedInDirectoryBuildTargets)' == 'false'"
File="$(MSBuildProjectFullPath)" />
<!--
Log an error if there are any duplicate <PackageReference /> items where a <GlobalPackageReference /> is already defined.
-->
Expand Down