question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:8
  • Comments:23 (12 by maintainers)

github_iconTop GitHub Comments

14reactions
nil4commented, Mar 5, 2017

@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>
13reactions
dsplaistedcommented, Jan 29, 2017

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Files and folders generated during publish aren't copied ...
My use case is a Webpack task that should run after the C# Web project is compiled, but only before it's published; the...
Read more >
Some files in "wwwroot" folder are not published in ASP. ...
I solved the issue. The solution is to edit the .csproj file. Remove all the ItemGroup tags related to wwwroot and then add...
Read more >
[Blazor] Anyone else getting the issue where wwwroot isnt ...
I have tried manually copying it over and i dont wanna mess with the folders properties and end up having to re-create the...
Read more >
Keeping Content Out of the Publish Folder for WebDeploy
I've run into issues with keeping files from publishing with WebDeploy on numerous occasions. When working with large projects it's not ...
Read more >
dotnet publish command - .NET CLI
The dotnet publish command publishes a .NET project or solution to a directory.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found