Search code examples
c#ubuntu-18.04.net-core-2.2

Why would `dotnet msbuild` fail with warnings on the first run and then run successfully the second time?


I am joining a new team at work and have never worked with .NET before, but I am having an issue that I don't even know how to begin to diagnose.

Basically, I needed to build the project and I used the following command on the VisualStudio Code terminal to do so:

dotnet msbuild -restore -target:build -p:VisualStudioVersion=16

On the first run, the compiler complained about errors such as:

  • trailing commas
  • closing braces preceded by blank lines
  • type name conflicts
  • Literal string as a parameter
  • Class owning disposable field but type of field is not actually disposable

I did not try to run the application at this point because I wanted to resolve the warnings first.

I ran the command (dotnet msbuild -restore -target:build -p:VisualStudioVersion=16) again and it did the compilation without producing any warnings.

Would anyone who knows more about more about .NET be able to give me some insight as to why this would happen?


Solution

  • Why would dotnet msbuild fail with warnings on the first run and then run successfully the second time?

    The fundamental problem is that you've characterized the situation as the opposite of the real situation. MSBuild did not "fail with warnings". It succeeded with warnings. The project was successfully built, so it succeeded.

    Once your mental model of how the tool works is correct, it will be much easier to diagnose problems.

    I ran the command again and it did the compilation without producing any warnings.

    No, it did not do the compilation again. Rather, it did nothing. Building an already-successfully-built project does nothing; the project is already built.

    You said you wanted it built, it already is built, so we're done. MSBuild is supposed to be smart enough to not re-do work that has already been done; it is not perfect, but it does a pretty good job of not wasting your time re-doing work.

    If you want to force a project to rebuild from scratch, including doing all the analysis, typically you would use

    /target:Rebuild
    

    not

    /target:Build
    

    If you want to get the project back into the pre-built state without doing a rebuild, you'd typically do

    /target:Clean
    

    I am having an issue that I don't even know how to begin to diagnose.

    Then today would be a great day to read your project file and learn how it works, and also to read the msbuild documentation:

    https://learn.microsoft.com/en-us/visualstudio/msbuild/walkthrough-using-msbuild?view=vs-2019

    Now you know how to begin to diagnose it: read the project file and your tool documentation before you use the tool.