Skip to content

Commit 1f2a4a1

Browse files
Adjust ordering of Single-file publish targets (#10740)
* Adjust ordering of Single-file publish targets This change reorders certain publish targets in preparation for upcoming changes to single-file publish support in .net 5. The behavior of `dotnet publish /p:PublishSingleFile=true` is different in .net 5, compared to .net core 3, as explained in [this document](https://github.com/dotnet/designs/blob/master/accepted/single-file/design.md#build-system-interface). In some cases, the single-file bundler in HostModel library will leave certain types of files unbundled into the single-file app. These files need to be copied into the publish directory. Currently, the Single-file bundle generator after the copy step, which places files in the publish directory. However, this model is unsuitable in .net 5, because the files to be copied will only be determined after bundle-generation. Therefore, the `BundlePublishDirectory` targets now run before the copy step, along with the `ILLink` and `ReadyToRun` targets. In an upcoming change, the `GenerateBundle` task will generate outputs to trim `ResolvedFileToPublish` based on the `PublishSingleFile` settings. This change actually simplified a few other targets.
1 parent e596309 commit 1f2a4a1

File tree

1 file changed

+23
-41
lines changed

1 file changed

+23
-41
lines changed

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

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ Copyright (c) .NET Foundation. All rights reserved.
4747
<_CorePublishTargets>
4848
PrepareForPublish;
4949
ComputeAndCopyFilesToPublishDirectory;
50-
GeneratePublishDependencyFile;
51-
BundlePublishDirectory;
5250
</_CorePublishTargets>
5351

5452
<_PublishNoBuildAlternativeDependsOn>$(_BeforePublishNoBuildTargets);$(_CorePublishTargets)</_PublishNoBuildAlternativeDependsOn>
@@ -194,11 +192,8 @@ Copyright (c) .NET Foundation. All rights reserved.
194192
</RemoveDuplicates>
195193

196194
<ItemGroup>
197-
<_CurrentPublishFileWritesUnfiltered Include="@(ResolvedFileToPublish->'$(_NormalizedPublishDir)%(RelativePath)')" Condition="'$(PublishSingleFile)' != 'true'"/>
198-
<_CurrentPublishFileWritesUnfiltered Include="@(ResolvedFileToPublish->'$(_NormalizedPublishDir)%(RelativePath)')"
199-
Condition="'$(PublishSingleFile)' == 'true' And '%(ResolvedFileToPublish.ExcludeFromSingleFile)' == 'true'"/>
195+
<_CurrentPublishFileWritesUnfiltered Include="@(ResolvedFileToPublish->'$(_NormalizedPublishDir)%(RelativePath)')"/>
200196
<_CurrentPublishFileWritesUnfiltered Include="$(_NormalizedPublishDir)$(AssemblyName)$(_NativeExecutableExtension)" Condition="'$(UseAppHost)' == 'true'"/>
201-
<_CurrentPublishFileWritesUnfiltered Include="$(_NormalizedPublishDir)$(ProjectDepsFileName)" Condition="'$(GenerateDependencyFile)' == 'true' And '$(PublishSingleFile)' != 'true'"/>
202197
</ItemGroup>
203198

204199
<ConvertToAbsolutePath Paths="@(_CurrentPublishFileWritesUnfiltered)">
@@ -216,28 +211,20 @@ Copyright (c) .NET Foundation. All rights reserved.
216211
_CopyResolvedFilesToPublishPreserveNewest
217212
218213
Copy _ResolvedFileToPublishPreserveNewest items to the publish directory
219-
(except files that will be bundled, during PublishSingleFile).
220214
============================================================
221215
-->
222216
<Target Name="_CopyResolvedFilesToPublishPreserveNewest"
223217
DependsOnTargets="_ComputeResolvedFilesToPublishTypes"
224218
Inputs="@(_ResolvedFileToPublishPreserveNewest)"
225219
Outputs="@(_ResolvedFileToPublishPreserveNewest->'$(PublishDir)%(RelativePath)')">
226220

227-
<ItemGroup>
228-
<_ResolvedUnbundledFileToPublishPreserveNewest
229-
Include="@(_ResolvedFileToPublishPreserveNewest)"
230-
Condition="'$(PublishSingleFile)' != 'true' or
231-
'%(_ResolvedFileToPublishPreserveNewest.ExcludeFromSingleFile)'=='true'" />
232-
</ItemGroup>
233-
234221
<!--
235222
PreserveNewest means that we will only copy the source to the destination if the source is newer.
236223
SkipUnchangedFiles is not used for that purpose because it will copy if the source and destination
237224
differ by size too. Instead, this target uses inputs and outputs to only copy when the source is newer.
238225
-->
239-
<Copy SourceFiles = "@(_ResolvedUnbundledFileToPublishPreserveNewest)"
240-
DestinationFiles="@(_ResolvedUnbundledFileToPublishPreserveNewest->'$(PublishDir)%(RelativePath)')"
226+
<Copy SourceFiles = "@(_ResolvedFileToPublishPreserveNewest)"
227+
DestinationFiles="@(_ResolvedFileToPublishPreserveNewest->'$(PublishDir)%(RelativePath)')"
241228
OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"
242229
Retries="$(CopyRetryCount)"
243230
RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)"
@@ -255,26 +242,18 @@ Copyright (c) .NET Foundation. All rights reserved.
255242
_CopyResolvedFilesToPublishAlways
256243
257244
Copy _ResolvedFileToPublishAlways items to the publish directory
258-
(except files that will be bundled, during PublishSingleFile).
259245
============================================================
260246
-->
261247
<Target Name="_CopyResolvedFilesToPublishAlways"
262248
DependsOnTargets="_ComputeResolvedFilesToPublishTypes">
263249

264-
<ItemGroup>
265-
<_ResolvedUnbundledFileToPublishAlways
266-
Include="@(_ResolvedFileToPublishAlways)"
267-
Condition="'$(PublishSingleFile)' != 'true' or
268-
'%(_ResolvedFileToPublishAlways.ExcludeFromSingleFile)'=='true'" />
269-
</ItemGroup>
270-
271250
<!--
272251
Use SkipUnchangedFiles to prevent unnecessary file copies. The copy will occur if the
273252
destination doesn't exist, the source is newer than the destination, or if the source and
274253
destination differ by file size.
275254
-->
276-
<Copy SourceFiles = "@(_ResolvedUnbundledFileToPublishAlways)"
277-
DestinationFiles="@(_ResolvedUnbundledFileToPublishAlways->'$(PublishDir)%(RelativePath)')"
255+
<Copy SourceFiles = "@(_ResolvedFileToPublishAlways)"
256+
DestinationFiles="@(_ResolvedFileToPublishAlways->'$(PublishDir)%(RelativePath)')"
278257
SkipUnchangedFiles="$(SkipCopyUnchangedFiles)"
279258
OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"
280259
Retries="$(CopyRetryCount)"
@@ -475,7 +454,9 @@ Copyright (c) .NET Foundation. All rights reserved.
475454
<Target Name="ComputeFilesToPublish"
476455
DependsOnTargets="ComputeResolvedFilesToPublishList;
477456
ILLink;
478-
CreateReadyToRunImages">
457+
CreateReadyToRunImages;
458+
GeneratePublishDependencyFile;
459+
GenerateSingleFileBundle">
479460
</Target>
480461

481462
<PropertyGroup>
@@ -942,26 +923,23 @@ Copyright (c) .NET Foundation. All rights reserved.
942923

943924
<!--
944925
============================================================
945-
BundlePublishDirectory
926+
GenerateSingleFileBundle
946927
947-
Bundle the _ResolvedFileToPublish* items one file in PublishDir
948-
(except those marked ExcludeFromSingleFile)
928+
Bundle the ResolvedFileToPublish items into one file in PublishDir
929+
(except those marked ExcludeFromSingleFile)
949930
============================================================
950931
-->
951932
<Target Name="_ComputeFilesToBundle"
952933
Condition="'$(PublishSingleFile)' == 'true'">
953934

954935
<ItemGroup>
955-
<_FilesToBundle Include="@(_ResolvedFileToPublishPreserveNewest)"
956-
Condition="'%(_ResolvedFileToPublishPreserveNewest.ExcludeFromSingleFile)' != 'true'"/>
957-
<_FilesToBundle Include="@(_ResolvedFileToPublishAlways)"
958-
Condition="'%(_ResolvedFileToPublishAlways.ExcludeFromSingleFile)' != 'true'"/>
936+
<_FilesToBundle Include="@(ResolvedFileToPublish)" Condition="'%(ResolvedFileToPublish.ExcludeFromSingleFile)' != 'true'"/>
959937
</ItemGroup>
960938

961939
</Target>
962940

963941
<UsingTask TaskName="GenerateBundle" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
964-
<Target Name="BundlePublishDirectory"
942+
<Target Name="GenerateSingleFileBundle"
965943
Condition="'$(PublishSingleFile)' == 'true'"
966944
DependsOnTargets="_ComputeFilesToBundle"
967945
Inputs="@(_FilesToBundle)"
@@ -972,7 +950,13 @@ Copyright (c) .NET Foundation. All rights reserved.
972950
IncludeSymbols="$(IncludeSymbolsInSingleFile)"
973951
OutputDir="$(PublishDir)"
974952
ShowDiagnosticOutput="false"/>
975-
953+
954+
<ItemGroup>
955+
<ResolvedFileToPublish Remove="@(_FilesToBundle)"/>
956+
<!-- ResolvedFileToPublish shouldn't include the output; the single-file bundle is written directly to the publish directory -->
957+
</ItemGroup>
958+
959+
976960
</Target>
977961

978962
<!--
@@ -993,10 +977,8 @@ Copyright (c) .NET Foundation. All rights reserved.
993977
Condition="'$(GenerateDependencyFile)' == 'true' and '$(_UseBuildDependencyFile)' != 'true'">
994978

995979
<PropertyGroup>
996-
<PublishDepsFilePath Condition=" '$(PublishDepsFilePath)' == '' And '$(PublishSingleFile)' != 'true'">$(PublishDir)$(ProjectDepsFileName)</PublishDepsFilePath>
997-
<PublishDepsFilePath Condition=" '$(PublishDepsFilePath)' == '' And '$(PublishSingleFile)' == 'true'">$(IntermediateOutputPath)$(ProjectDepsFileName)</PublishDepsFilePath>
980+
<PublishDepsFilePath Condition=" '$(PublishDepsFilePath)' == ''">$(IntermediateOutputPath)$(ProjectDepsFileName)</PublishDepsFilePath>
998981
</PropertyGroup>
999-
1000982
<ItemGroup>
1001983
<ResolvedCompileFileDefinitions Remove="@(_PublishConflictPackageFiles)" Condition="'%(_PublishConflictPackageFiles.ConflictItemType)' == 'Reference'" />
1002984
<RuntimeTargetsCopyLocalItems Remove="@(_PublishConflictPackageFiles)" Condition="'%(_PublishConflictPackageFiles.ConflictItemType)' != 'Reference'" />
@@ -1036,9 +1018,9 @@ Copyright (c) .NET Foundation. All rights reserved.
10361018
RuntimeGraphPath="$(BundledRuntimeIdentifierGraphFile)"/>
10371019

10381020
<ItemGroup>
1039-
<_FilesToBundle Include="$(PublishDepsFilePath)" Condition="'$(PublishSingleFile)' == 'true'">
1021+
<ResolvedFileToPublish Include="$(PublishDepsFilePath)">
10401022
<RelativePath>$(ProjectDepsFileName)</RelativePath>
1041-
</_FilesToBundle>
1023+
</ResolvedFileToPublish>
10421024
</ItemGroup>
10431025

10441026
</Target>

0 commit comments

Comments
 (0)