sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Publish only includes files that exist before publish

Open Gekctek opened this issue 9 years ago • 13 comments

I am and have been having an issue for a long time when I have my npm/bower/gulp processes as publish scripts (in project.json/.csproj). With the new .csproj files, it seems that the publish process only includes files/folders that existed before the 'dotnet publish' command was run. Example cases: (All these cases assume that my project directory is something like src/WebProject and I am running dotnet publish in that directory and the publish scripts run with the publish)

  1. I dont have the wwwroot folder in the source and I run dotnet publish, the output from the publish will not have a wwwroot folder or any of the files within it anywhere
  2. I have the wwwroot folder int the source with css1.css in it, the output will only include wwwroot/css1.css even though my source wwwroot has more files in it from the publish scripts
  3. Same scenario as 2, but I change the contents of css1.css to something different that what it will be post publish. I then publish and the wwwroot/css1.css is only there like before but the contents of the file are NOT what I manually changed it to. So the content of it is the result of the publish process. So the publish must see it before it starts, mark it so that it will be apart of the publish, update it with the gulp publish process, and then copy the updated version to the output directory.

My publish scripts:

    <Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
		<Exec Command="npm install" />
		<Exec Command="bower install" />
		<Exec Command="gulp clean" />
		<Exec Command="gulp copy_appsettings_not_exists" />
		<Exec Command="gulp copy" />
	</Target>

Nothing changes if I change the BeforeTarget to something earlier like Build

My includes:


	<ItemGroup>
		<Content Include="appsettings.json.template" CopyToPublishDirectory="Always" />
		<Content Update="wwwroot\**\*;Views\**\*" CopyToPublishDirectory="Always" />
		<Content Remove="appsettings.json" />
		<Content Remove="bower.json" />
		<Content Remove="package.json" />
	</ItemGroup>

I have also played with this in changing the Conent to None or CopyToPublishDirectory to CopyToOutputDirectory and it is all the same.

I can work around this by running my gulp before the dotnet publish but I would rather not. Any help would be great

Gekctek avatar Mar 08 '17 21:03 Gekctek

Forgot case 4: If i have the wwroot and all sub folders/files in the source before dotnet publish then they will end up in the publish output with no problems

Gekctek avatar Mar 08 '17 21:03 Gekctek

cc @mlorbetske I remember there is a different target that you can set your BeforeTargets to that gets this to work, but I don't recall exactly what it is. And of course, I may be wrong.

livarcocc avatar Mar 09 '17 01:03 livarcocc

I have already tried all that I could find and it didn't change the result

On Wed, Mar 8, 2017, 17:15 Livar [email protected] wrote:

cc @mlorbetske https://github.com/mlorbetske I remember there is a different target that you can set your BeforeTargets to that gets this to work, but I don't recall exactly what it is. And of course, I may be wrong.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dotnet/sdk/issues/960#issuecomment-285223584, or mute the thread https://github.com/notifications/unsubscribe-auth/AB2GaU6kBRFDqlp5g2n_bJGCH8Xgtsteks5rj1K6gaJpZM4MXT9K .

Gekctek avatar Mar 09 '17 01:03 Gekctek

@vijayrkn any ideas?

mlorbetske avatar Mar 09 '17 04:03 mlorbetske

Looks like a dupe of https://github.com/aspnet/websdk/issues/114. (There is an ugly workaround that works on first and subsequent publish operations without duplicate items: https://github.com/aspnet/websdk/issues/114#issuecomment-280076533)

dasMulli avatar Mar 09 '17 05:03 dasMulli

Yes, this is the same issue as https://github.com/aspnet/websdk/issues/114. A simpler workaround would be to add something like this: https://github.com/dotnet/cli/issues/5498#issuecomment-275932671

vijayrkn avatar Mar 09 '17 17:03 vijayrkn

This has been fixed (or mostly fixed) in the Web SDK.

dsplaisted avatar May 16 '17 01:05 dsplaisted

Hi. For everybody who has similar issue. Yes, it's really fixed in .NET Core 2.0 sdk. You can look code here

  <!--***************************************************************-->
  <!-- Target _IncludePrePublishGeneratedContent                     -->
  <!--***************************************************************-->
  
  <Target Name="_IncludePrePublishGeneratedContent" BeforeTargets="GetCopyToPublishDirectoryItems" Condition=" '$(EnableDefaultItems)' == 'true' And '$(EnableDefaultContentItems)' == 'true' ">
    <ItemGroup>
      <_WebProjectGeneratedContent Include="wwwroot\**" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);@(_ContentIncludedByDefault)" />
      <ContentWithTargetPath Include="@(_WebProjectGeneratedContent)" TargetPath="%(Identity)" CopyToPublishDirectory="PreserveNewest" />
    </ItemGroup>
  </Target>

At first some target scan wwwroot folder and store all files paths. After executing 'PrepublishScript' target SDK run scanning again (as you can see in code upward). But this target refresh only wwwroot folder. In my case PrepublishScript target using gulp to generate and add custom files, that do not belong to wwwroot folder. In this case my generated files will be ignored.

How to fix?

My simple decision was to add similar "refresh" target in .csproj file. This target will add my custom folder\files to copying list. (PS: My generated by gulp folder is Modules)

  <Target Name="_IncludeModules" BeforeTargets="GetCopyToPublishDirectoryItems">
    <ItemGroup>
      <_GeneratedModules Include="Modules\**" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);@(_ContentIncludedByDefault)" />
      <ContentWithTargetPath Include="@(_GeneratedModules)" TargetPath="%(Identity)" CopyToPublishDirectory="Always" />
    </ItemGroup>
  </Target>

@mlorbetske Can you comment please, why can't sdk get all items to copy after PrepublishScript or why sdk refresh data of wwwroot only?

t0xicDen avatar Sep 13 '17 08:09 t0xicDen

This is still an issue... While I'm using .net core 3.1, which is supposed to be out since 2019-12-03 https://en.wikipedia.org/wiki/.NET_Core#History I run a script that downloads native libraries from artifactory during build (conan install -g deploy) The workaround I use now is to run publish twice... which is a waste of time I don't even understand why the files are not re-scan just before copying the files, because by definition a build step will generate new files

-- below just craps all over dotnet, skip at will -- Generally dotnet has been a waste of time Doc is not up to level, and often doesn't explain much but paraphrasing titles and items it is supposed to explain Every step that would be simple and well documented on linux happened to be painful with dotnet Not mentioning my hate of "implicit" targets to make the .csproj less verbose... Don't do implicit, be explicit and stop using XML with humans ! Ridiculous framework -- back to the point --

$ dotnet --info .NET Core SDK (reflecting any global.json): Version: 3.1.404 Commit: 470f6754b3

Runtime Environment: OS Name: centos OS Version: 8 OS Platform: Linux RID: centos.8-x64 Base Path: /usr/share/dotnet/sdk/3.1.404/

Host (useful for support): Version: 3.1.10 Commit: 1721e39439

.NET Core SDKs installed: 3.1.404 [/usr/share/dotnet/sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.App 3.1.10 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.10 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download

barchstien avatar Nov 16 '20 18:11 barchstien

In the end, I worked around that issue with using first build, then publish with --no-build : dotnet build service.sln --framework netcoreapp3.1 dotnet publish service.sln --framework netcoreapp3.1 --output ./publish --no-build

More generally, I would advise to not use dotnet or csharp, but modern C++ (14, 17) together with cmake and conan

barchstien avatar Nov 16 '20 19:11 barchstien

Any chance of an update WRT @t0xicDen's pertinent question

Can you comment please, why can't sdk get all items to copy after PrepublishScript or why sdk refresh data of wwwroot only?

p-m-j avatar Sep 28 '21 05:09 p-m-j

I believe the discussion was that at least for the wwwroot directory, one can assume both the intent to publish this directory due to project structure conventions AND the likelyhood (and reports) to execute custom build logic.

For other directories, this might be dangerous or unintended. That said, custom build logic can always emit more items (creating ContentWithTargetPath or ResolvedFileToPublish (expects full absolute path as Identity) items) and update the project before the publish logic, although it is harder to implement.

dasMulli avatar Sep 28 '21 10:09 dasMulli

<ItemGroup>
    <Content Include="Foo/**" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
</ItemGroup>

I'm quite explicitly stating that I want to publish the contents of the Foo directory, the issue being that if Foo directory is created and populated by a target from a nuget package that runs before build the instructions are ignored.

# working
$ dotnet build
$ dotnet publish --no-build
# not working
$ dotnet publish 

It seems odd that an aditional target with ContentWithTargetPath should be required to make this work.

Files are created before the build target executes so why should the publish target not pick them up.

p-m-j avatar Sep 28 '21 10:09 p-m-j