Skip to content

New diagnostic proposal: erroring on CopyToOutputDirectory="Always" #7654

@NickCraver

Description

@NickCraver

One of the common pain points I've hit is builds re-running when they don't need to and a leading cause of this is CopyToOutputDirectory="Always" set somewhere on a Compile/None/EmbeddedResource/etc.

These are already collected up in GetCopyToOutputDirectoryItems so I propose we add a diagnostic (opt-in) to let people error on this in the base SDK (Microsoft.CurrentVersion.targets). Here's my rudamentary attempt at such a Task working locally:

<Target Name="ComplainAboutCopyAlways" 
        AfterTargets="_GetCopyToOutputDirectoryItemsFromThisProject"
        BeforeTargets="GetCopyToPublishDirectoryItems">
  <CallTarget Targets="_GetCopyToOutputDirectoryItemsFromThisProject">
    <Output TaskParameter="TargetOutputs" ItemName="_ThisProjectItemsToCopyToOutputDirectory" />
  </CallTarget>
  
  <!-- Note: due to this being an error, it only errors on the first...not sure if we can make it error on many correctly... -->
  <Error Text="Item '%(_ThisProjectItemsToCopyToOutputDirectory.TargetPath)' set as CopyToOutputDirectory=&quot;Always&quot;, use CopyToOutputDirectory=&quot;PreserveNewest&quot; instead." 
          Condition="'%(_ThisProjectItemsToCopyToOutputDirectory.CopyToOutputDirectory)'=='Always'" />
</Target>

This produces build output when violations are detected:

C:\path\Directory.Build.targets(382,5): error : Item 'AlwaysCopiedFile.Txt' set as CopyToOutputDirectory="Always", use CopyToOutputDirectory="PreserveNewest" instead. [C:\path\Test.csproj]

I think this would be very useful in the base targets, opted in via some new variable (adding a condition to my above example) - thoughts on this? I'd be happy to PR it if this is amenable we get a good variable and error message (I just tried to make an initial stab). Thanks!

/cc @baronfel @rainersigwald

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions