sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Files and folders generated during publish aren't copied from wwwroot to output

Open damienpontifex opened this issue 9 years ago • 23 comments

Steps to reproduce

See sample project for reproduction here https://github.com/damienpontifex/NotPublishingGeneratedFiles

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
    <Exec Command="npm install" />
    <Exec Command="npm run build" />
</Target>

npm build script

"scripts": {
    "build": "tsc"
}

tsconfig.json has the property outFile = wwwroot/ts/app.js

PrepareForPublish compiles typescript into wwwroot/ts folder.

This folder is also removed on dotnet clean and ignored by git

Steps to reproduce:

  1. dotnet restore
  2. dotnet publish -c Release -o <your output dir>

If you re-run the publish step, the files are appropriately copied as they existed at the time the publish was started.

If you run dotnet clean to clear away generated files and re-run publish step, they are again missed when copying to output as per original restore and publish.

I have also tried changing the PrepublishScript target value of BeforeTargets to ComputeFilesToPublish from looking at targets here https://github.com/dotnet/sdk/blob/master/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Publish.targets but with no success

n.b. If I generate the files into a folder that does exist and has content (e.g. wwwroot/js has site.js in it) then the copy works fine. It's only if I generate both a new folder and the files inside it in this step

Expected behavior

Files generated into wwwroot to be in output directory under wwwroot

Actual behavior

Files are generated in source wwwroot folder, but not copied into publish output wwwroot folder

Environment data

dotnet --info output: .NET Command Line Tools (1.0.0-rc3-004530)

Product Information: Version: 1.0.0-rc3-004530 Commit SHA-1 hash: 0de3338607

Runtime Environment: OS Name: Mac OS X OS Version: 10.12 OS Platform: Darwin RID: osx.10.12-x64 Base Path: /usr/local/share/dotnet/sdk/1.0.0-rc3-004530

damienpontifex avatar Jan 28 '17 08:01 damienpontifex

I share the same concern. Is there any guidance on how to hook up tasks (e.g. Typescript, Webpack, etc.) to run during CLI builds, such that their output is picked up by the build/publish process?

nil4 avatar Jan 28 '17 14:01 nil4

@nguerrera @dsplaisted Any chance that the published files are being calculated before the tasks in the prepublish target run?

livarcocc avatar Jan 28 '17 21:01 livarcocc

I have the same issue with this prepublish task

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish" Condition=" '$(IsCrossTargetingBuild)' != 'true' "> <Exec Command="npm install" /> <Exec Command="bower install" /> <Exec Command="gulp clean" /> <Exec Command="gulp min" /> </Target>

armartinez avatar Jan 29 '17 10:01 armartinez

Any chance that the published files are being calculated before the tasks in the prepublish target run?

Yes, the content items from wwwroot are calculated during evaluation before any targets are run.

@damienpontifex @nil4 @armartinez As a workaround, you can add this target to your project file:

  <Target Name="AddGeneratedContentItems" BeforeTargets="AssignTargetPaths" DependsOnTargets="PrepareForPublish">
    <ItemGroup>
      <Content Include="wwwroot/**" CopyToPublishDirectory="PreserveNewest" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);@(Content)" />
    </ItemGroup>
  </Target>

@mlorbetske @vijayrkn Should we consider adding something like this to the Web SDK so that files added to wwwroot by npm tasks or similar get included in the publish output?

dsplaisted avatar Jan 29 '17 18:01 dsplaisted

@dsplaisted thank you for suggesting this workaround! Unfortunately I found it to kick in during every build in Visual Studio, which would slow down development significantly.

My use case is a Webpack task that should run after the C# Web project is compiled, but only before it's published; the publish output should pick up the files generated by the Webpack task in wwwroot/dist.

I cobbled together something to unblock myself for now, through a painful half-day of running dotnet publish with diagnostic verbosity to figure this out. This feels fragile and I'm sure a future update would break it. I am hoping for a better solution and thus 👍 to adding something supported to the Web SDK.

Here is the task I used in case someone else finds it useful:

<Target Name="RunWebpack" BeforeTargets="PrepareForPublish">
    <Exec Command="npm run webpack:bundle" />
    <ItemGroup>
       <!-- Inspired by https://github.com/aspnet/JavaScriptServices/blob/0200489bfcbf66c35c17c549498fb122ec9edd56/templates/AureliaSpa/Aurelia.csproj#L72 -->
       <_WebpackFiles Include="wwwroot\dist\**" />
       <ContentWithTargetPath Include="@(_WebpackFiles->'%(FullPath)')" RelativePath="%(_WebpackFiles.Identity)" TargetPath="%(_WebpackFiles.Identity)" CopyToPublishDirectory="Always" />
    </ItemGroup>
</Target>

nil4 avatar Jan 29 '17 18:01 nil4

Moving to 'discussion' milestone. This isn't really a CLI issue but I don't want to reset the conversation history.

TheRealPiotrP avatar Feb 01 '17 09:02 TheRealPiotrP

cc @vijayrkn

mlorbetske avatar Feb 01 '17 20:02 mlorbetske

@piotrpMSFT would this be more appropriately re-raised on aspnet/websdk as linked by @dsplaisted ?

damienpontifex avatar Feb 01 '17 22:02 damienpontifex

Yes, we should be able to add something like this to the websdk to handle files generated inside wwwroot

https://github.com/dotnet/cli/issues/5498#issuecomment-275932671

@damienpontifex - Can you create a bug in the https://github.com/aspnet/websdk repo?

vijayrkn avatar Feb 01 '17 22:02 vijayrkn

Closing this, since it is being tracked in the aspnet/websdk repo.

livarcocc avatar Feb 07 '17 01:02 livarcocc

Could we put this work around in the xproj to csproj conversion docs?

JasonRowe avatar Mar 20 '17 20:03 JasonRowe

@mairaw for the conversion docs.

livarcocc avatar Mar 20 '17 20:03 livarcocc

I think the web SDK is intending to add a target so this doesn't happen.

dsplaisted avatar Mar 21 '17 02:03 dsplaisted

Good examples of running webpack only before publish and not every build.

update 9/29/2017 - example moved to templating https://github.com/aspnet/templating/blob/95f2b8d375256ca7057cbe88dcbfff644697f183/src/Microsoft.AspNetCore.SpaTemplates/content/Vue-CSharp/VueSpa.csproj

JasonRowe avatar Mar 22 '17 21:03 JasonRowe

Who still has similar issue try to read my comment

t0xicDen avatar Sep 13 '17 08:09 t0xicDen

@JasonRowe 404 on that. Also how do you properly debug if you only copy Gulp/Grunt/Webpack/NPM compiled assets out on a publish?

VictorioBerra avatar Sep 27 '17 22:09 VictorioBerra

@VictorioBerra WebPack has its own webserver - we do all the dev & debugging on that with live-reload. The release Gulp is only required on publish, and we publish to test or staging.

alasdaircs avatar Oct 06 '17 07:10 alasdaircs

Is there a solution for ASP.NET project for exactly the same issue?

blteblte avatar Sep 11 '18 12:09 blteblte

@blteblte This is supposed to be fixed for ASP.NET Core projects, if you are having a similar problem please file a new issue.

dsplaisted avatar Sep 11 '18 18:09 dsplaisted

@dsplaisted Sorry. I was not clear enough - I meant ASP.NET Web Application (.NET framework) project - not core. I know this is not stack overflow, but this seems the only place the problem is recognized.

But nvm - found how to hookup stuff generating files before publish (so publish picks it up) anyways

blteblte avatar Sep 12 '18 06:09 blteblte