Canonical Error and Warnings Format
MSBuild recognizes the specially formatted errors and warnings that many command-line tools write to the console.
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
There are five main parts to these messages:
http://alexkipman.members.winisp.net/channel9wikiimages/wikicanonicalerrorwarnings.gif
The order of these parts is important and shouldn’t change based on locale.
Origin (Required, Not Localized)
Origin can be blank. If present, origin is a tool name—like ‘cl’ in the example or it is a file name, like ‘Main.cs’ in the example.
If it’s a filename, then it must be either absolute or relative to the current directory that the tool was executed from. Filenames can optionally be followed by parenthesized line and 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 are one-relative. The beginning line in a file is line one, and the leftmost column is column one. These numbers must use in Arabic digits.
If origin is a tool name, then it must not change based on locale. It should be a locale-neutral value.
Subcategory (Optional, Localized)
Subcategory is used to modify the category, it can be any string. Subcategory should be localized.
Category (Required, Not Localized)
Category must be either ‘error’ or ‘warning’. Case does not matter. Like origin, category must not be localized.
Code (Required, Not Localized)
Code identifies an application specific error or warning code for this message. Code must not be localized and it must not contain spaces.
Text (Optional, Localized)
Text is the explanation of the error. Text should be localized.
How does MSBuild Use Canonical Errors and Warnings?
When
MSBuild calls command-line tools—for example, CSC.EXE or RESGEN.EXE—it looks at the console output that is produced. Any lines that match the canonical format are treated specially.
Lines recognized as errors or warnings are turned into build errors or warnings. When
MSBuild is called from the console, these errors and warnings will be printed to the console.
When
MSBuild is used to build from within DEVENV, the errors and warnings will be logged to the tasklist. If origin refers to a file, the user can click on the task item to jump to that file in the editor. If there is position information, then the editor will jump to the position within the file that caused the error.
Finally, other future third-party loggers may want to make use of the broken out error and warning information.
Does MSBuild Produce Errors and Warnings in Canonical Format?
Yes. When called from the command-line,
MSBuild will output its own errors in canonical format.
This is important because canonical errors and warnings are also recognized and treated specially by BUILD.EXE in the build process used to build VS itself. Since we dogfood
MSBuild by calling it from BUILD.EXE, it’s very important that we produce errors in the canonical format.
How Does MSBuild Recognize Canonical Errors?
MSBuild uses a regular expression to recognize canonical errors and warnings. Here’s a snippet of code that shows the form of the regular expression.
// Defines the main pattern for matching messages.
static private Regex [originCategoryCodeTextExpression] = new Regex
(
// Beginning of line and any amount of whitespace.
@"^\s*"
// Match a [optional project number prefix 'ddd>'], single letter + colon + remaining filename, or
// string with no colon followed by a colon.
+@"(((?<ORIGIN>(((\d+>)?[a-zA-Z]?:[^:]*)|([^:]*))):)"
// Origin may also be empty. In this case there's no trailing colon.
+"|())"
// Match the empty string or a string without a colon that ends with a space
+"(?<SUBCATEGORY>(()|([^:]*? )))"
// Match 'error' or 'warning' followed by a space.
+"(?<CATEGORY>(error|warning)) "
// Match anything without a colon, followed by a colon
+"(?<CODE>[^:]*):"
// Whatever's left on this line, including colons.
+"(?<TEXT>.*)$",
[RegexOptions.IgnoreCase]
);
Is there a canonical format for warnings treated as errors?
Apparently not:
csc
Form1.cs(16,17): error CS0168: Warning as Error: The variable 'i' is declared but never used
cl
test.cpp(9) : error C2220: warning treated as error - no 'object' file generated