MSBuild / Visual Studio aware error messages and message formats


MSBuild recognizes error messages and warnings that have been specially formatted by many command line tools that typically write to the console. For instance, take a look at the following error messages – they are all properly formatted to be MSBuild and Visual Studio friendly.


    Main.cs(17,20): warning CS0168: The variable ‘foo’ is declared but never used


   


    C:\dir1\foo.resx(2) : error BC30188: Declaration expected.


   


    cl : Command line warning D4024 : unrecognized source file type ‘foo.cs’, object file assumed


   


    error CS0006: Metadata file ‘System.dll’ could not be found.


 


These messages confirm to special format that is shown below, and comprise 5 parts – the order of these parts are important and should not change: 


(See image here)


 



Origin (Required)


Origin can be blank. If present, the origin is usually a tool name, like ‘cl’ in one of the examples. But it could also be a filename, like ‘Main.cs’ shown in another example. If it is a filename, then it must be an absolute or a relative filename, followed by an optional paranthesized line/column information in one of the following forms:


 


        (line) or (line-line) or (line-col) or (line,col-col) or (line,col,line,col)


 


Lines and columns start at 1 in a file – i.e. the beginning of a file is 1, and the leftmost column is 1. If the Origin is a tool name, then it must not change based on local – i.e. it needs to be locale neutral.


 


Subcategory (Optional)


Subcategory is used to classify the category itself further, and should not be localized.


 


Category (Required)


Category must be either ‘error’ or ‘warning’. Case does not matter. Like origin, category must not be localized.


 


Code (Required)


Code identifies an application specific error code / warning code. Code must not be localized and it must not contain spaces.


 


Text (Optional)


User friendly text that explains the error, and *must* be localized if you cater to multiple locales.


 


So, how does MSBuild use Errors and Warnings emitted by external tools?


When MSBuild calls command line tools (for instance, csc.exe or vbc.exe), it looks at the output emitted by the tool to standard out / standard error streams. Any lines that match the error format that I just described will be treated specially – i.e. lines that are recognized as errors or warnings will be turned into build errors and warnings respectively.


 


So, what’s the big deal about this anyway? (Here comes the really cool part) 🙂


To see the real benefit of this, you have to be building from within Visual Studio. Because MSBuild treats these messages specially, they get logged as first class warnings and errors in the Visual Studio task list. If the Origin specifies line/column information, then double clicking on the message will take you to the source of the error in the offending file.


 


[ Author : Faisal Mohamood ]

Comments (23)

  1. Keith Hill says:

    This is really cool.  One minor problem is that I get two errors in the VS task list.  One for the error picked up by parsing the output (awesome – thanks) and the other because the tool returned an error code.  I’m invoking the tool using the error task.  If I set IgnoreExitCode to false, the second error caused the by exit code doesn’t show up but then again MSBUILD thinks the target succeeded when it really failed.  Is there a way that I can have my cake and eat it too?

  2. Keith Hill says:

    Doh that should read that I’m invoking the command line exe using the "Exec" task.

  3. Britney says:

    Hello, nice site look this:

    <a href="http://urlzip.de/5m9">chanel handbags</a>

    End ^) See you

  4. ... says:

    mmm.. nice design, I must say..

  5. ... says:

    Du musst ein Fachmann sein – wirklich guter Aufstellungsort, den du hast!

  6. ... says:

    luogo grande:) nessun osservazioni!

  7. ... says:

    Chi ha fatto questo? E un buon posto per trovare le informazioni importanti!:)

  8. hdhucs says:

    Hi. Cool design. Keep up the good.

    Good luck.

  9. hdhucs says:

    Hi. Cool design. Keep up the good.

    Good luck.

  10. Matt says:

    Is-it possible to change to language of the errors messages text ?

    Thanks

  11. turha says:

    Here is the GNU specification for formatting error messages so that line and column numbers can be automatically recognized:

    http://www.gnu.org/prep/standards/standards.html#Errors

    It says tab stops should be assumed every 8 columns.  Your blog post did not go into such detail.  How should column numbers logged via MSBuild count tabs and CJK fullwidth characters?

  12. Mike says:

    This is really nice for Errors and Warnings, but how do you get VS to log a simple informational message.  Category only contains error and warning and by experimentation I see that VS ignores attempts at "message", "informational", etc…  Is there a magic incantation?

  13. Chris Kline says:

    It’s worth noting that "warning" and "error" MUST be lower case in order to be recognized. I.e., "Warning" will not be recognized.

  14. fabiola-wy says:

    <a href= http://puhlov.com >mixolgy measurments</a>

  15. Greg Popovitch says:

    I have a problem. When compiling c++ with cl the file names in error messages are lowercased. This is not the case when building from inside Visual Studio 2005. How can I prevent this case change when using cl?

    Thanks,

    greg

  16. baztheman says:

    Hi,

    I posted in msdn with a msbuild error code return question but have not been answered hence I am giving this another try…

    I am using msbuild from framework version 2.0.50727.1433. I need to be able to capture msbuild error correctly on command line or via batch files. When I type "msbuild sdsd", it does not return non zero. Why?

    Here is my msdn forum post if you want more details: http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/82615310-3ac2-4265-87fb-c3d7a4e652fe/

    Thank you.

    B.

  17. Hoop Somuah says:

    " (line) or (line-line) or (line-col) or (line,col-col) or (line,col,line,col)"

    (line-col) should read (line,col)

  18. Hoop Somuah says:

    also, shouldn’t "(line,col,line,col)" be "(line,col-line,col)" for consistency?

    where comma separated values in a cordinate (point) and hyphen separates the values in a span

  19. Joe Chavez says:

    With regard to:

    Messages

    Indicates the number of Messages in the list. Click to toggle whether Messages are displayed.

    What is the syntax to add "message" to the "Messages" list in the build output.

    So far I have:

    printf("UnitTest++: info UT100: %d tests passed.n", totalTestCount);

    printf("UnitTest++: message UT100: %d tests passed.n", totalTestCount);

    printf("UnitTest++: warning UT100: %d tests passed.n", totalTestCount);

    printf("UnitTest++: error UT100: %d tests passed.n", totalTestCount);

    The first 2 "info" and "message" do not show up in the "Messages" list. The "warning" and "error" work as expected.

  20. AstaDan says:

    Like Joe Chavez, I too really want to know how I can get a message in to the 'Messages' list in the Error List pane of VS2010 via a #pragma message in C++.

    Please can you help?

  21. James F says:

    This is the appropriate MSDN page which describes the format/syntax quite well.

    msdn.microsoft.com/…/yxkt8b26.aspx

  22. prinkel says:

    hi

    i am using visual studio 2010 and sql 2008, while showing the grid after writing in the text and clicking the search button i am getting the required result thats fine and when i write "good' OR 1 = 1 –" in the text box the whole table is been shown in the grid view can someone please tell me why this is happening and how to over come this error