Files and folders generated during publish aren't copied from wwwroot to output
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:
dotnet restoredotnet 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
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?
@nguerrera @dsplaisted Any chance that the published files are being calculated before the tasks in the prepublish target run?
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>
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 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>
Moving to 'discussion' milestone. This isn't really a CLI issue but I don't want to reset the conversation history.
cc @vijayrkn
@piotrpMSFT would this be more appropriately re-raised on aspnet/websdk as linked by @dsplaisted ?
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?
Closing this, since it is being tracked in the aspnet/websdk repo.
Could we put this work around in the xproj to csproj conversion docs?
@mairaw for the conversion docs.
I think the web SDK is intending to add a target so this doesn't happen.
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
Who still has similar issue try to read my comment
@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 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.
Is there a solution for ASP.NET project for exactly the same issue?
@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 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