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.

Target "copy" task doesn't run in .Net Core project if project is "up-to-date"

See original GitHub issue

Issue moved from microsoft/msbuild#5450


From @Dean-NC on Saturday, June 20, 2020 2:10:45 PM

Steps to reproduce

In the attached demo project, there is an external .js file that gets linked to a destination in the project, but intentionally outside the wwwroot folder, so that it can maintain it’s linked status, otherwise .Net core project system will fight and overtake it, removing the linked status after building and the copy target runs.

There’s a “copy” target set to copy any changes to the original .js file to the wwwroot/js folder so the app can use it. I want this target to run regardless of whether the project is “up-to-date” or not, so I used a BeforeTargets = “BeforeBuild” (I also tried BeforeTargets = “Build”, AfterTargets and all variations). The target runs if there’s changes to the project (C# or cshtml or .csproj), but changes to the linked .js file alone won’t trigger the target to run.

  1. Build the project, start project without debugging, which will run the Index.cshtml page.
  2. While in Visual Studio, open Linked\module1.js, and make some changes.
  3. Build the project in Visual Studio. Note that the updated .js file won’t get copied. If you have the Output Window verbosity set to Detailed, you will see nothing happens since the project is considered up-to-date.

Project file

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
  </ItemGroup>
  
  <ItemGroup>
    <None Include="..\module1.js" Link="Linked\module1.js" />
  </ItemGroup>
  
  <ItemGroup>
    <Content Update="wwwroot\js\common\*" Visible="False" />
    <None Update="Linked\**\*" CopyToPublishDirectory="Never" />
  </ItemGroup>
  
  <Target Name="CopyLinkedFiles" BeforeTargets="BeforeBuild">
    <Copy SourceFiles="%(None.Identity)" DestinationFolder="wwwroot\js\common" Condition="'%(None.Link)' != '' And '%(Extension)' == '.js'" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" />
  </Target>

</Project>

The reason I’ve hidden the wwwroot\js\common files, is because these will be linked, and I don’t want to accidentally change these in the common folder, but in my “Linked” folder instead. For the ItemGroup link, I’ve tried “Content” instead of “None” which didn’t help.

zzTempWeb2a.zip

Expected behavior

In my traditional .Net framework web apps, the same kind of custom Target will always run, even if the project is up-to-date. I expect the same here.

Actual behavior

The msbuild target doesn’t run if the project is up-to-date.

Environment data

.NET Core SDK (reflecting any global.json): Version: 3.1.301

Runtime Environment: OS Name: Windows OS Version: 10.0.18363 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.1.301\

Host (useful for support): Version: 3.1.5 Commit: 65cd789777

.NET Core SDKs installed: 3.1.300 [C:\Program Files\dotnet\sdk] 3.1.301 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Microsoft Visual Studio Community 2019 Version 16.6.0 VisualStudio.16.Release/16.6.0+30114.105 Microsoft .NET Framework Version 4.8.03752

Installed Version: Community

ASP.NET and Web Tools 2019 16.6.936.3669 ASP.NET and Web Tools 2019

ASP.NET Core Razor Language Services 16.1.0.2020603+b3ac44798c16fff5b95dbcfe62dea84aa9a1bd72 Provides languages services for ASP.NET Core Razor.

ASP.NET Web Frameworks and Tools 2019 16.6.936.3669

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Dean-NCcommented, Apr 1, 2022

@drewnoakes thanks for your thoughts. The UpToDateCheckInput/Output is an interesting thought. I don’t remember if I tried using TargetPathwith CopyToOutputDirectory. Let me try TargetPath and if that doesn’t work I’ll try UpToDateCheckInput/Output. I’ll let you know the results.

0reactions
Dean-NCcommented, Nov 8, 2022

@drewnoakes Thanks for letting me know about those input/output project settings, I had no idea they existed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Target "copy" task doesn't run in .Net Core project if ...
I want this target to run regardless of whether the project is "up-to-date" or not, so I used a BeforeTargets = "BeforeBuild" (I...
Read more >
msbuild - .NET Core 2.1 : How to trigger Copy task defined ...
1 Answer 1 · Make the items' source and target paths known before · Register them to the up-to-date check system. (Also needs...
Read more >
Copy Task - MSBuild
Learn how to use the MSBuild Copy task to copy files to a new file or folder location in the file system.
Read more >
Jeremy Bytes: May 2020
Notice that "AfterTargets" parameter is set to "Build". So this will run after the build. If the build fails, then this will not...
Read more >
Visual Studio 2019 Always Rebuilds Up-To-Date Projects
I have a WPF project that is several years old now but has been under intermittent development. I have never had any issues...
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