1

I've created my own targets file which during the build process runs a custom tool generates some C# code which are then included in the .csproj.

I have something very like this.

<Project Sdk="Microsoft.NET.Sdk" InitialTargets="MyTarget">
  .....

  <Import Project="MyTargetsFile.targets" />

  <ItemGroup>
    <XYZ Include="**\*.xyz" />
  </ItemGroup>

</Project>

The problem I encounter if that if I change the .xyz visual studio does trigger a rebuild because it considers it up to date. On the other hand if i run msbuild Systems.csproj /v:diag > Build.log it detects the changes and it rebuilds the project.

After a bit more research I've reached the conclusion that VS doesn't not even trigger MSBuild in this case. Is seems to me that visual studio just checks just .cs files for changes and ignores the rest.

Now my question is how do I make VS trigger msbuild if I've made a change in my .xyz file?

I'm using Visual Studio Enterprise 2017 Version 15.6.6

Update

I know I can set the build action for the files to Compile but they contain .xml and that causes an error.

I also know that I can clean the project and build, but I'm interested in an incremental build not a full one.

Bobby Tables
  • 2,953
  • 7
  • 29
  • 53

2 Answers2

2

After a lot of googling I've been able to find a solution, not necessarily the best, but it seems to do the job.

As stated in the question my problem was the vs was not triggering msbuild. Looks like in order to let msbuild always run (this does not mean that you get a full build eveytime, it just means you let msbuild decide what to do and not vs) you have to add

<PropertyGroup>
    <DisableFastUpToDateCheck>True</DisableFastUpToDateCheck>
</PropertyGroup>

MSDN says the following about DisableFastUpToDateCheck:

A boolean value that applies to Visual Studio only. The Visual Studio build manager uses a process called FastUpToDateCheck to determine whether a project must be rebuilt to be up to date. This process is faster than using MSBuild to determine this. Setting the DisableFastUpToDateCheck property to true lets you bypass the Visual Studio build manager and force it to use MSBuild to determine whether the project is up to date.

Another way to solve it although I'm not sure it's suitable for everyone, it to set the build action to Embedded resource.

Bobby Tables
  • 2,953
  • 7
  • 29
  • 53
1

Visual studio doesn't trigger MSBuild

Do you mean Visual studio doesn't trigger MSBuild if you change the .xyz?

It depends on the Build Action of your .xyz file. If you set the Build Action of that file to C# compiler, when you change the content of that file, VS/MSBuild will compile this file to check the content of this file. If it was changed, it will considers it not up to date, then trigger MSBuild.

The default build action of .cs file is C# compiler, most the default build action of the rest files should be None. When we add a file with build action None, VS/MSBuild will not check the content of this file, only check the input and the output of this file. That the reason why Visual studio doesn't trigger MSBuild if you change the .xyz.

For example, when we add a text file in the Visual Studio with build action None, build the project. Then I change some content in the text file, build again the project, it doesn't trigger MSBuild.

If we want to the MSBuild was triggered after we change the text file, we recommend to clean the build to remove the output, then when we build it again, MSBuild will be triggered.

Then I change the build action of the file to C# compiler(To avoid introducing compilation errors, I use space instead of code in this file.), I change the text file with a space after this project is up to date, build the project again, Visual studio trigger MSBuild.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Setting the build action to compile is something that crossed my mind, but the thing is that my contain xml and I would get an error. I would talke somehow to make vs take into consideration my custom files as well, without having to create an extension or something like that. And also clean would trigger a full rebuild which is a bit much from my point of view, I'm interested in a incremental build – Bobby Tables Apr 18 '18 at 06:29
  • @trebor, It will related to the another File Properties-Copy to Output Directory. If you want to VS trigger msbuild if you made a change in your `.xml` file, you can set the Copy to Output Directory to `copy always`. – Leo Liu Apr 18 '18 at 07:15
  • Can you also specify the build action, cause i tried `None` and `Content` and those had no effect, the only thing that seems to work is setting the build `Embedded resource` and than Copy to Ouput Directory doesn't matter – Bobby Tables Apr 18 '18 at 08:01
  • @trebor, What do you mean `VS trigger msbuild`? At first, I think you mean VS keep up to date after you made a change in your `.xyz` file, then you want to trigger a rebuild of that project when you have change the .xml file. But it seems I misunderstand you, could you please share some info about this issue? – Leo Liu Apr 18 '18 at 08:24
  • 1
    When building from visual studio it only checks for changes which have the build action embed resources or compile, my `.xyz` files were neither so when building from VS my `targets` didn't run, but when using msbuild everything was ok. Thank you for your time the solution that worked for me was adding `DisableFastUpToDateCheck` – Bobby Tables Apr 18 '18 at 08:58