Skip to content

Commit ffb7660

Browse files
authored
Add support for building tool packages with runtime identifier set without building RID-specific packages (#51281)
2 parents b0f6af0 + 527c399 commit ffb7660

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.PackTool.targets

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ NOTE: This file is imported from the following contexts, so be aware when writin
5454
<_ToolRidsAreOnlyShims Condition="'$(RuntimeIdentifiers)' == '' and $(PackAsToolShimRuntimeIdentifiers) != '' ">true</_ToolRidsAreOnlyShims>
5555
<_UserSpecifiedToolPackageRids Condition="'$(ToolPackageRuntimeIdentifiers)' != ''">$(ToolPackageRuntimeIdentifiers)</_UserSpecifiedToolPackageRids>
5656
<_UserSpecifiedToolPackageRids Condition="'$(_UserSpecifiedToolPackageRids)' == ''">$(RuntimeIdentifiers)</_UserSpecifiedToolPackageRids>
57-
<_HasRIDSpecificTools Condition=" '$(_UserSpecifiedToolPackageRids)' != '' ">true</_HasRIDSpecificTools>
58-
<_HasRIDSpecificTools Condition="'$(_HasRIDSpecificTools)' == ''">false</_HasRIDSpecificTools>
57+
<CreateRidSpecificToolPackages Condition="'$(CreateRidSpecificToolPackages)' == '' And '$(_UserSpecifiedToolPackageRids)' != '' ">true</CreateRidSpecificToolPackages>
58+
<CreateRidSpecificToolPackages Condition="'$(CreateRidSpecificToolPackages)' == ''">false</CreateRidSpecificToolPackages>
5959

6060
<!-- NOTE: this line is load-bearing. This impacts Restore behaviors significantly, so we can't prevent the import of these targets _in general_. -->
6161
<RuntimeIdentifiers Condition="'$(PackAsToolShimRuntimeIdentifiers)' != ''">$(_UserSpecifiedToolPackageRids);$(PackAsToolShimRuntimeIdentifiers)</RuntimeIdentifiers>
@@ -65,7 +65,7 @@ NOTE: This file is imported from the following contexts, so be aware when writin
6565
<!-- We need to know if the inner builds are _intended_ to be AOT even if we then explicitly disable AOT for the outer builds.
6666
Knowing this lets us correctly decide to create the RID-specific inner tools or not when packaging the outer tool. -->
6767
<_InnerToolsPublishAot>false</_InnerToolsPublishAot>
68-
<_InnerToolsPublishAot Condition="$(_HasRIDSpecificTools) and '$(PublishAot)' == 'true'">true</_InnerToolsPublishAot>
68+
<_InnerToolsPublishAot Condition="$(CreateRidSpecificToolPackages) and '$(PublishAot)' == 'true'">true</_InnerToolsPublishAot>
6969

7070
<!-- determining if it's safe to change publish-related properties for this evaluation. We can only override default publishing
7171
behavior if
@@ -164,7 +164,7 @@ NOTE: This file is imported from the following contexts, so be aware when writin
164164
</PropertyGroup>
165165

166166
<!-- inner-build tool packages get a RID suffix -->
167-
<PropertyGroup Condition="'$(_HasRIDSpecificTools)' != '' And '$(RuntimeIdentifier)' != ''">
167+
<PropertyGroup Condition="$(CreateRidSpecificToolPackages) And '$(RuntimeIdentifier)' != ''">
168168
<PackageId>$(PackageId).$(RuntimeIdentifier)</PackageId>
169169
</PropertyGroup>
170170
</Target>
@@ -390,7 +390,7 @@ NOTE: This file is imported from the following contexts, so be aware when writin
390390
<Target Name="SetDotnetToolPackageType" Returns="$(_ToolPackageType)">
391391

392392
<PropertyGroup>
393-
<_ToolPackageType Condition="'$(RuntimeIdentifier)' != '' And '$(_HasRIDSpecificTools)' != ''">DotnetToolRidPackage</_ToolPackageType>
393+
<_ToolPackageType Condition="'$(RuntimeIdentifier)' != '' And $(CreateRidSpecificToolPackages)">DotnetToolRidPackage</_ToolPackageType>
394394
<_ToolPackageType Condition="'$(_ToolPackageType)' == ''">DotnetTool</_ToolPackageType>
395395
</PropertyGroup>
396396

@@ -427,7 +427,7 @@ NOTE: This file is imported from the following contexts, so be aware when writin
427427
We can't call this for AOT'd tools because we can't AOT cross-architecture and cross-platform in .NET today. -->
428428
<Target Name="_CreateRIDSpecificToolPackages"
429429
Condition="'$(RuntimeIdentifier)' == ''
430-
and $(_HasRIDSpecificTools)
430+
and $(CreateRidSpecificToolPackages)
431431
and !$(_InnerToolsPublishAot)">
432432
<PropertyGroup>
433433
<_PackageRids>$(ToolPackageRuntimeIdentifiers)</_PackageRids>

test/Microsoft.NET.ToolPack.Tests/GivenThatWeWantToPackAToolProject.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,5 +323,42 @@ public void Given_targetplatform_set_It_should_error()
323323
result.Should().Fail().And.HaveStdOutContaining("NETSDK1146");
324324

325325
}
326+
327+
[Fact]
328+
public void It_packs_with_RuntimeIdentifier()
329+
{
330+
var testProject = new TestProject("ToolWithRuntimeIdentifier")
331+
{
332+
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
333+
IsExe = true,
334+
RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid()
335+
};
336+
testProject.AdditionalProperties["PackAsTool"] = "true";
337+
testProject.AdditionalProperties["ImplicitUsings"] = "enable";
338+
testProject.AdditionalProperties["CreateRidSpecificToolPackages"] = "false";
339+
testProject.AdditionalProperties["UseAppHost"] = "false";
340+
341+
342+
var testAsset = _testAssetsManager.CreateTestProject(testProject);
343+
344+
var packCommand = new PackCommand(testAsset);
345+
346+
packCommand.Execute().Should().Pass();
347+
348+
packCommand.GetPackageDirectory().Should().HaveFile($"{testProject.Name}.1.0.0.nupkg");
349+
packCommand.GetPackageDirectory().Should().NotHaveFile($"{testProject.Name}.{testProject.RuntimeIdentifier}.1.0.0.nupkg");
350+
351+
var nupkgPath = packCommand.GetNuGetPackage();
352+
353+
using (var nupkgReader = new PackageArchiveReader(nupkgPath))
354+
{
355+
var toolSettingsItem = nupkgReader.GetToolItems().SelectMany(g => g.Items).SingleOrDefault(i => i.Equals($"tools/{testProject.TargetFrameworks}/{testProject.RuntimeIdentifier}/DotnetToolSettings.xml"));
356+
toolSettingsItem.Should().NotBeNull();
357+
358+
var toolSettingsXml = XDocument.Load(nupkgReader.GetStream(toolSettingsItem));
359+
toolSettingsXml.Root.Attribute("Version").Value.Should().Be("1");
360+
}
361+
362+
}
326363
}
327364
}

0 commit comments

Comments
 (0)