Skip to content

Commit 910bc42

Browse files
committed
Additional fix for --no-build in pack with transitive references
Turns out that the .NET SDK lifts transitive project references as direct references (without any additional metadata), and this causes the second-level dependency from being built unexpectedly (see dotnet/sdk#478 and dotnet/project-system#199). Since we don't want to disrupt the IDE (BuildingInsideVisualStudio) and we only want to fix this for the very specific case of running from the CLI `dotnet pack --no-build`, we make the fix very constrained for that scenario. We check for `NoBuild` but ALSO for `_IsPacking`, which is passed by the `dotnet pack` command. This ensures minimal impact in all other scenarios, since we're essentially turning off a built-in behavior in the SDK that has explicit side-effects (by design and desirable) and we should preserve. #Fixes 501
1 parent f4bfcdf commit 910bc42

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

src/NuGetizer.Tasks/NuGetizer.Inference.targets

+8
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,14 @@ Copyright (c) .NET Foundation. All rights reserved.
574574
$(_AllProjectCapabilities.Contains('WinRTReferences')) or
575575
$(_AllProjectCapabilities.Contains('SDKReferences'))">true</_SupportsReferences>
576576

577+
<!-- Very constrained scenario for CLI pack with dotnet pack -no-build, which otherwise triggers
578+
transitive project references to be built when that's not what the user wants. -->
579+
<DisableTransitiveProjectReferences Condition="'$(_SupportsReferences)' == 'true' and
580+
'$(_IsPacking)' == 'true' and
581+
'$(NoBuild)' == 'true' and
582+
'$(BuildingInsideVisualStudio)' != 'true' and
583+
'$(DesignTimeBuild)' != 'true'" >true</DisableTransitiveProjectReferences>
584+
577585
<InferPackageContentsDependsOn Condition="'$(_SupportsReferences)' == 'true'">
578586
ResolveReferences;
579587
_UpdatePackageReferenceVersions;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="$([MSBuild]::GetPathOfFileAbove(Scenario.props, $(MSBuildThisFileDirectory)))" />
3+
4+
<PropertyGroup>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<PackageId>Foo</PackageId>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="Project2.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="$([MSBuild]::GetPathOfFileAbove(Scenario.props, $(MSBuildThisFileDirectory)))" />
3+
4+
<PropertyGroup>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="Project3.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="$([MSBuild]::GetPathOfFileAbove(Scenario.props, $(MSBuildThisFileDirectory)))" />
3+
4+
<PropertyGroup>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="NuGetizer" Version="1.2.2" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Xunit;
2+
using Xunit.Abstractions;
3+
4+
namespace NuGetizer
5+
{
6+
public class given_transitive_projects
7+
{
8+
readonly ITestOutputHelper output;
9+
10+
public given_transitive_projects(ITestOutputHelper output)
11+
{
12+
this.output = output;
13+
using var disable = OpenBuildLogAttribute.Disable();
14+
15+
Builder.BuildScenario(nameof(given_transitive_projects), target: "Restore", projectName: "Project1")
16+
.AssertSuccess(output);
17+
18+
Builder.BuildScenario(nameof(given_transitive_projects), target: "Build", projectName: "Project1")
19+
.AssertSuccess(output);
20+
}
21+
22+
[Fact]
23+
public void when_pack_no_build_then_succeeds()
24+
{
25+
var result = Builder.BuildScenario(nameof(given_transitive_projects),
26+
projectName: "Project1",
27+
// These are the properties passed by dotnet pack --no-build
28+
properties: new { NoBuild = "true", _IsPacking = "true" },
29+
target: "GetPackageContents,Pack");
30+
31+
result.AssertSuccess(output);
32+
33+
Assert.True(result.BuildResult.HasResultsForTarget("GetPackageContents"));
34+
35+
var items = result.BuildResult.ResultsByTarget["GetPackageContents"];
36+
37+
Assert.Contains(items.Items, item => item.Matches(new
38+
{
39+
PackagePath = "lib/net8.0/Project1.dll"
40+
}));
41+
42+
Assert.Contains(items.Items, item => item.Matches(new
43+
{
44+
PackagePath = "lib/net8.0/Project2.dll"
45+
}));
46+
47+
Assert.Contains(items.Items, item => item.Matches(new
48+
{
49+
PackagePath = "lib/net8.0/Project3.dll"
50+
}));
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)