Skip to content

Commit f0b40be

Browse files
jjonesczCopilot
andauthored
Remove MSBuild hacks for virtual project building (#49745)
Co-authored-by: Copilot <[email protected]>
1 parent 9d737ea commit f0b40be

File tree

2 files changed

+16
-69
lines changed

2 files changed

+16
-69
lines changed

src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -90,70 +90,28 @@ internal sealed class VirtualProjectBuildingCommand : CommandBase
9090
/// </remarks>
9191
private static readonly IEnumerable<string> s_ignorableProperties =
9292
[
93-
// This is set by default by `dotnet run`, so it must be ignored otherwise the CSC optimization would not kick in by default.
93+
// These are set by default by `dotnet run`, so at least these must be ignored otherwise the CSC optimization would not kick in by default.
9494
"NuGetInteractive",
95+
"_BuildNonexistentProjectsByDefault",
96+
"RestoreUseSkipNonexistentTargets",
9597
];
9698

9799
public static string TargetFrameworkVersion => Product.TargetFrameworkVersion;
98100

99-
internal static readonly string TargetOverrides = """
100-
<!--
101-
Override targets which don't work with project files that are not present on disk.
102-
See https://github.com/NuGet/Home/issues/14148.
103-
-->
104-
105-
<Target Name="_FilterRestoreGraphProjectInputItems"
106-
DependsOnTargets="_LoadRestoreGraphEntryPoints">
107-
<!-- No-op, the original output is not needed by the overwritten targets. -->
108-
</Target>
109-
110-
<Target Name="_GetAllRestoreProjectPathItems"
111-
DependsOnTargets="_FilterRestoreGraphProjectInputItems;_GenerateRestoreProjectPathWalk"
112-
Returns="@(_RestoreProjectPathItems)">
113-
<!-- Output from dependency _GenerateRestoreProjectPathWalk. -->
114-
</Target>
115-
116-
<Target Name="_GenerateRestoreGraph"
117-
DependsOnTargets="_FilterRestoreGraphProjectInputItems;_GetAllRestoreProjectPathItems;_GenerateRestoreGraphProjectEntry;_GenerateProjectRestoreGraph"
118-
Returns="@(_RestoreGraphEntry)">
119-
<!-- Output partly from dependency _GenerateRestoreGraphProjectEntry and _GenerateProjectRestoreGraph. -->
120-
121-
<ItemGroup>
122-
<_GenerateRestoreGraphProjectEntryInput Include="@(_RestoreProjectPathItems)" Exclude="$(MSBuildProjectFullPath)" />
123-
</ItemGroup>
124-
125-
<MSBuild
126-
BuildInParallel="$(RestoreBuildInParallel)"
127-
Projects="@(_GenerateRestoreGraphProjectEntryInput)"
128-
Targets="_GenerateRestoreGraphProjectEntry"
129-
Properties="$(_GenerateRestoreGraphProjectEntryInputProperties)">
130-
131-
<Output
132-
TaskParameter="TargetOutputs"
133-
ItemName="_RestoreGraphEntry" />
134-
</MSBuild>
135-
136-
<MSBuild
137-
BuildInParallel="$(RestoreBuildInParallel)"
138-
Projects="@(_GenerateRestoreGraphProjectEntryInput)"
139-
Targets="_GenerateProjectRestoreGraph"
140-
Properties="$(_GenerateRestoreGraphProjectEntryInputProperties)">
141-
142-
<Output
143-
TaskParameter="TargetOutputs"
144-
ItemName="_RestoreGraphEntry" />
145-
</MSBuild>
146-
</Target>
147-
""";
148-
149101
public VirtualProjectBuildingCommand(
150102
string entryPointFileFullPath,
151103
MSBuildArgs msbuildArgs)
152104
{
153105
Debug.Assert(Path.IsPathFullyQualified(entryPointFileFullPath));
154106

155107
EntryPointFileFullPath = entryPointFileFullPath;
156-
MSBuildArgs = msbuildArgs;
108+
MSBuildArgs = msbuildArgs.CloneWithAdditionalProperties(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
109+
{
110+
// See https://github.com/dotnet/msbuild/blob/main/documentation/specs/build-nonexistent-projects-by-default.md.
111+
{ "_BuildNonexistentProjectsByDefault", bool.TrueString },
112+
{ "RestoreUseSkipNonexistentTargets", bool.FalseString },
113+
}
114+
.AsReadOnly());
157115
}
158116

159117
public string EntryPointFileFullPath { get; }
@@ -856,7 +814,6 @@ public static void WriteProjectFile(
856814
<Clean Include="{EscapeValue(artifactsPath)}/*" />
857815
</ItemGroup>
858816
859-
<!-- We need to explicitly import Sdk props/targets so we can override the targets below. -->
860817
""");
861818

862819
if (firstSdkVersion is null)
@@ -1049,11 +1006,7 @@ public static void WriteProjectFile(
10491006
""");
10501007
}
10511008

1052-
writer.WriteLine($"""
1053-
1054-
{TargetOverrides}
1055-
1056-
""");
1009+
writer.WriteLine();
10571010
}
10581011

10591012
writer.WriteLine("""

test/dotnet.Tests/CommandTests/Run/RunFileTests.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,8 +1051,11 @@ public void BinaryLog_EvaluationData()
10511051
new FileInfo(binaryLogPath).Should().Exist();
10521052

10531053
var records = BinaryLog.ReadRecords(binaryLogPath).ToList();
1054-
records.Count(static r => r.Args is ProjectEvaluationStartedEventArgs).Should().Be(2);
1055-
records.Count(static r => r.Args is ProjectEvaluationFinishedEventArgs).Should().Be(2);
1054+
1055+
// There should be at least two - one for restore, one for build.
1056+
// But the restore targets might re-evaluate the project via inner MSBuild task invocations.
1057+
records.Count(static r => r.Args is ProjectEvaluationStartedEventArgs).Should().BeGreaterThanOrEqualTo(2);
1058+
records.Count(static r => r.Args is ProjectEvaluationFinishedEventArgs).Should().BeGreaterThanOrEqualTo(2);
10561059
}
10571060

10581061
/// <summary>
@@ -2650,7 +2653,6 @@ public void Api()
26502653
<Clean Include="/artifacts/*" />
26512654
</ItemGroup>
26522655
2653-
<!-- We need to explicitly import Sdk props/targets so we can override the targets below. -->
26542656
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
26552657
<Import Project="Sdk.props" Sdk="Aspire.Hosting.Sdk" Version="9.1.0" />
26562658
@@ -2681,8 +2683,6 @@ public void Api()
26812683
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
26822684
<Import Project="Sdk.targets" Sdk="Aspire.Hosting.Sdk" Version="9.1.0" />
26832685
2684-
{VirtualProjectBuildingCommand.TargetOverrides}
2685-
26862686
</Project>
26872687
26882688
""")}},"Diagnostics":[]}
@@ -2720,7 +2720,6 @@ public void Api_Diagnostic_01()
27202720
<Clean Include="/artifacts/*" />
27212721
</ItemGroup>
27222722
2723-
<!-- We need to explicitly import Sdk props/targets so we can override the targets below. -->
27242723
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
27252724
27262725
<PropertyGroup>
@@ -2744,8 +2743,6 @@ public void Api_Diagnostic_01()
27442743
27452744
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
27462745
2747-
{VirtualProjectBuildingCommand.TargetOverrides}
2748-
27492746
</Project>
27502747
27512748
""")}},"Diagnostics":
@@ -2787,7 +2784,6 @@ public void Api_Diagnostic_02()
27872784
<Clean Include="/artifacts/*" />
27882785
</ItemGroup>
27892786
2790-
<!-- We need to explicitly import Sdk props/targets so we can override the targets below. -->
27912787
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
27922788
27932789
<PropertyGroup>
@@ -2811,8 +2807,6 @@ public void Api_Diagnostic_02()
28112807
28122808
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
28132809
2814-
{VirtualProjectBuildingCommand.TargetOverrides}
2815-
28162810
</Project>
28172811
28182812
""")}},"Diagnostics":

0 commit comments

Comments
 (0)