MSBuild appears to only use old output files for custom build tools

て烟熏妆下的殇ゞ 提交于 2019-12-23 03:33:32

问题


I have an ANTLR grammar file as part of a C# project file and followed the steps outlined in the User Manual.

<Project ...>
    <PropertyGroup>
        <Antlr3ToolPath>$(ProjectDir)tools\antlr-3.1.3\lib</Antlr3ToolPath>
        <AntlrCleanupPath>$(ProjectDir)AntlrCleanup\$(OutputPath)</AntlrCleanupPath>
    </PropertyGroup>
    <ItemGroup>
        <Antlr3 Include="Grammar\Foo.g">
            <OutputFiles>FooLexer.cs;FooParser.cs</OutputFiles>
        </Antlr3>
        <Antlr3 Include="Grammar\Bar.g">
            <OutputFiles>BarLexer.cs;BarParser.cs</OutputFiles>
        </Antlr3>
    </ItemGroup>
    <Target Name="GenerateAntlrCode"
            Inputs="@(Antlr3)"
            Outputs="%(Antlr3.OutputFiles)">
        <Exec Command="java -cp %22$(Antlr3ToolPath)\antlr-3.1.3.jar%22 org.antlr.Tool -message-format vs2005 @(Antlr3Input)" Outputs="%(Antlr3Input.OutputFiles)" />
        <Exec Command="%22$(AntlrCleanupPath)\AntlrCleanup.exe%22 @(Antlr3Input) %(Antlr3Input.OutputFiles)" />
    </Target>
    <ItemGroup>
         <!-- ...other files here... -->
         <Compile Include="Grammar\FooLexer.cs">
             <AutoGen>True</AutoGen>
             <DesignTime>True</DesignTime>
             <DependentUpon>Foo.g</DependentUpon>
          </Compile>
          <Compile Include="Grammar\FooParser.cs">
              <AutoGen>True</AutoGen>
              <DesignTime>True</DesignTime>
              <DependentUpon>Foo.g</DependentUpon>
          </Compile>
          <!-- ... -->
    </ItemGroup>
</Project>

For whatever reason, the Compile steps only use old versions of the code, no amount of tweaking appears to help.

By "old versions," I mean that if I clean the solution, build the project, Foo.g will make FooLexer.cs and FooParser.cs. Should I then make an update to Foo.g and recompile, the new versions of the lexer and parser C# files are ignored and the old versions are used. I have to compile a second time...


回答1:


There seems to be a bug in the IDE: Visual Studio only monitors changes in C# files that it modifies itself (e.g. designer generated code). For code modified/generated outside of the IDE (e.g. external tool like ANTLR) it will use the in-memory version of the file, without refreshing it from the disk.

The workaround is to not use the "hosted" cache, and instead spawn an external CSC process to compile the project. You do this by setting the "UseHostCompilerIfAvailable" project property to false like so in your .csproj:

<UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable>

For more info, see this entry in the MS Connect website.

I had the exact same problem as you with ANTLR in Visual Studio, and this fixed it for me. However, some people report problems with project-to-project dependencies after setting that option to 'false' so watch out for side effects...



来源:https://stackoverflow.com/questions/2579605/msbuild-appears-to-only-use-old-output-files-for-custom-build-tools

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!