msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

How can we clear the caches of last build

Open jianyexi opened this issue 2 years ago • 7 comments

I have a custom msbuild target which can generate some cs files from .cadl files and the generated files will be added to the compiler so that it can be compiled. But every time, the code generation will clear the last generated files, due to the compiler caches , if some generated files in last build were renamed or deleted in current build, the compiler will report "some files not found", how can we solve this issue ? I try to exclude the generated file before compiling , like <Compile Remove="**/generated/**/*.cs" />, but seems does not work

here is my custom build target : https://github.com/jianyexi/adl/blob/f74e9be533bf08a36b0fd221f755343cccd64a36/packages/cadl-msbuild-target/build/Microsoft.Cadl.MSBuild.targets#L63

jianyexi avatar Jun 06 '22 03:06 jianyexi

In addition, we are using <Project Sdk="Microsoft.NET.Sdk.Web"> and custom build target

jianyexi avatar Jun 06 '22 03:06 jianyexi

I try to exclude the generated file before compiling , like <Compile Remove="/generated//*.cs" />

What target (if any) are you hooking this Remove into?

Also, the link to your custom build target doesn't work.

benvillalobos avatar Jun 09 '22 16:06 benvillalobos

I try to exclude the generated file before compiling , like

What target (if any) are you hooking this Remove into?

Also, the link to your custom build target doesn't work.

the target is before 'CoreCompile' I put it here:

<Target Name="CadlCompileCore">
    <ItemGroup>
      <CadlCompile Remove="@(CadlCompile)" Condition=" '%(CadlCompile.Compile)' != 'true' " />
    </ItemGroup>

    <CadlCompiler Condition=" '$(Language)' == 'C#' and '@(CadlCompile)' != '' "
                  Imports="%(CadlCompile.Imports)"
                  CadlInputPaths="@(CadlCompile)"
                  CadlCompilerPath="%(CadlCompile.CadlCompilerPath)"
                  ToolPath="%(CadlCompile.CommandPath)"
                  OutputDir="%(CadlCompile.OutputDir)"
                  OS="$(OS)"
                  Options="%(CadlCompile.Options)"
                  Emitters="%(CadlCompile.Emitters)" >
      <Output TaskParameter="GeneratedFiles" ItemName="_Cadl_GeneratedFiles"/>
    </CadlCompiler>
    
    <Message Text="cadl gernerated @(_Cadl_GeneratedFiles)" Importance="high"/>
    <ItemGroup>
      <Compile Remove="generated/**/*.cs" />
      <Compile Remove="@(_Cadl_GeneratedFiles)" MatchOnMetadata="Identity" MatchOnMetadataOptions="PathLike" />
      <Compile Include="@(_Cadl_GeneratedFiles)"  Watch="false" />
    </ItemGroup>
  </Target>

  <!-- Do cadl compilation by default in a C# project. In other types, the user invoke
       CadlCompile directly where required. -->
  <Target Name="_CadlCompile_BeforeCsCompile" BeforeTargets="CoreCompile" DependsOnTargets="CadlCompileCore" Condition=" '$(Language)' == 'C#' " />

jianyexi avatar Jun 10 '22 01:06 jianyexi

@jianyexi I think several of the problems you're encountering could be resolved by generating the files into a folder in the obj ($(IntermediateOutputPath)) directory, instead of next to the checked-in source. That is the standard approach for generated files.

What's happening now is that at evaluation time, before your target runs, the SDK globs all **/*.cs files that exist on disk, including the ones from the last run. Then you generate more, but the <Compile Remove in your target globs only the ones that exist on disk at that time, so any generated files that you deleted in your code generator would not be on the list to be removed.

rainersigwald avatar Aug 04 '22 17:08 rainersigwald

Thanks for the response, is there any way to just skip or change the step of 'including the ones from the last run' when evaluating

jianyexi avatar Aug 05 '22 09:08 jianyexi

This issue is marked as stale because feedback has been requested for 30 days with no response. Please respond within 14 days or this issue will be closed due to inactivity.

ghost avatar Sep 05 '22 00:09 ghost

@jianyexi yes, via SDK properties you can change the files that are automatically discovered under the project; see DefaultItemExcludes for example.

rainersigwald avatar Sep 08 '22 16:09 rainersigwald

Closing as resolved.

benvillalobos avatar Oct 27 '22 16:10 benvillalobos