Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

Commit 876442f

Browse files
authored
[generator] Fix MSBuild warning/error format for Visual Studio (#765)
In b858dc5 we updated `generator` warnings/errors to give line & column information in more places. However, the existing method for formatting the line & column information was wrong: // Correct C:\code\Metadata.xml(2, 6): warning BG8A04: <attr path="/api/package[@name='androidx.appcompat.widget']/class[@name='RoundRectDrawableWithShadow']"/> matched no nodes. // Incorrect C:\code\Metadata.xml(2, 6) warning BG8A04: <attr path="/api/package[@name='androidx.appcompat.widget']/class[@name='RoundRectDrawableWithShadow']"/> matched no nodes. By omitting the colon after the line & column information, Visual Studio parses the colon within `C:\` instead, resulting in: ![image](https://user-images.githubusercontent.com/179295/102400384-7500b100-3fa7-11eb-8f35-aae2a04f3a58.png) * Filename: `C` * Line number: 1 This is actually worse than what we previously had, as double-clicking it does nothing, as `C` is not a valid file on disk. Rewrite the `Report.Format()` method to be a little clearer to read and add the required colon.
1 parent 3f6cf72 commit 876442f

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
usingSystem;
2+
usingSystem.Collections.Generic;
3+
usingSystem.Linq;
4+
usingSystem.Text;
5+
usingSystem.Threading.Tasks;
6+
usingMonoDroid.Generation;
7+
usingNUnit.Framework;
8+
9+
namespacegeneratortests
10+
{
11+
publicclassReportTests
12+
{
13+
[Test]
14+
publicvoidFormatTests()
15+
{
16+
varcode=0x37;
17+
varmsg="There was a {0} error";
18+
varargs="bad";
19+
varsourcefile=@"C:\code\test.cs";
20+
varline=32;
21+
varcol=12;
22+
23+
Assert.AreEqual("error BG0037: There was a bad error",Report.Format(true,code,null,0,0,msg,args));
24+
Assert.AreEqual(@"C:\code\test.cs: error BG0037: There was a bad error",Report.Format(true,code,sourcefile,0,0,msg,args));
25+
Assert.AreEqual(@"C:\code\test.cs(32): error BG0037: There was a bad error",Report.Format(true,code,sourcefile,line,0,msg,args));
26+
Assert.AreEqual(@"C:\code\test.cs(32, 12): error BG0037: There was a bad error",Report.Format(true,code,sourcefile,line,col,msg,args));
27+
Assert.AreEqual(@"C:\code\test.cs(32, 12): warning BG0037: There was a bad error",Report.Format(false,code,sourcefile,line,col,msg,args));
28+
}
29+
}
30+
}

‎tools/generator/Utilities/Report.cs‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,25 @@ public static string FormatCodedMessage (bool error, LocalizedMessage message, p
129129

130130
publicstaticstringFormat(boolerror,interrorCode,stringsourceFile,intline,intcolumn,stringformat,paramsobject[]args)
131131
{
132-
varorigin=sourceFile!=null?sourceFile+(line>0?column>0?$"({line}, {column})":$"({line})":null)+' ':null;
133-
returnstring.Format("{0}{1} BG{2:X04}: ",origin,error?"error":"warning",errorCode)+
134-
string.Format(format,args);
132+
varorigin=FormatOrigin(sourceFile,line,column);
133+
134+
return$"{origin}{(error?"error":"warning")} BG{errorCode:X04}: "+string.Format(format,args);
135+
}
136+
137+
staticstringFormatOrigin(stringsourceFile,intline,intcolumn)
138+
{
139+
if(string.IsNullOrWhiteSpace(sourceFile))
140+
returnnull;
141+
142+
varret=sourceFile;
143+
144+
if(line==0)
145+
returnret+": ";
146+
147+
if(column>0)
148+
returnret+$"({line}, {column}): ";
149+
150+
returnret+$"({line}): ";
135151
}
136152
}
137153

0 commit comments

Comments
 (0)