Repository navigation
Output Mono.Linker warnings through MSBuild - #968
Conversation
|
Please take a look @vitek-karas @marek-safar I've added a simple test to give an example of how this would be used and how it'd look like and created issues #986 #987 related to this. |
| public enum MessageCode { | ||
| L100, // Error | ||
| L400, // Warning | ||
| L900, // Info. |
There was a problem hiding this comment.
Did you talk to the Roslyn about the diagnostic IDs? We need to make sure that the codes we're about to add are unique and ideally that they follow some kind of pattern.
There was a problem hiding this comment.
They are still debating which way to go on this. For now, I simply used IL as a prefix.
| #if !NETCOREAPP | ||
| [IgnoreTestCase ("Can be enabled once MonoBuild produces a dll from which we can grab the types in the Mono.Linker namespace.")] | ||
| #else | ||
| [SetupCompileBefore ("LogStep.dll", new [] { "Dependencies/LogStep.cs" }, new [] { "illink.dll" })] |
There was a problem hiding this comment.
I do not believe that referencing the linker from a test case is a good path to go down. The test cases were intended to test controlled use cases. linking the linker itself is not a controlled test case and is bound to result in tests that are high maintenance.
If you want to write a test like this I think it should live in Mono.Linker.Tests as a normal unit test. Maybe something similar to IndividualTests if you still want to use a test case and the testing infrastructure to drive running of the linker.
There was a problem hiding this comment.
Thinking about it more...I don't think using the test case infrastructure in any way is the way to go here.
It looks more like you want to write a unit test that sets up a LinkContext and then calls
MessageOrigin msgOrigin = new MessageOrigin ("logtest");
Message msgError = new Message (msgOrigin, MessageCategory.Error, MessageCode.L100, text: "Error");
Message msgWarning = new Message (msgOrigin, MessageCategory.Warning, MessageCode.L400, text: "Warning");
Message msgInfo = new Message (new MessageOrigin(), MessageCategory.Info, MessageCode.L900, text: "Info");
context.LogMessage (msgError);
context.LogMessage (msgWarning);
context.LogMessage (msgInfo);
And then you assert what is in the ILogger class that you setup with the LinkContext.
If you wanted to test logging end to end via a test case then I would setup a scenario that causes a message to be logged and then assert that message is in the log. If you don't have messages hooked up yet, then I would hold off on a test case until you do.
There was a problem hiding this comment.
I'll move the test to Mono.Linker.Tests for now. I agree that a better way to test this would be to have a scenario that triggers a warning/error to be logged, but in this PR I'm just adding the basic infrastructure for logging support through MSBuild. I'll do as you advice and write tests that trigger the warnings once I update all the places where we log warnings/errors in the linker to use this.
Remove Position and move line and column to MessageOrigin. Remove MessageCode and use integer instead, check that code value falls into one of the known ranges and that it matches with the received category.
| public string Text { get; } | ||
|
|
||
| public MSBuildMessageContainer ( | ||
| int code, |
There was a problem hiding this comment.
Please fix the order, text is the most important and should never be empty and not last in argument
| Logger.LogMessage (importance, "{0}", message); | ||
| } | ||
|
|
||
| public void LogMessage (MSBuildMessageContainer msBuildMessage) |
There was a problem hiding this comment.
| public void LogMessage (MSBuildMessageContainer msBuildMessage) | |
| public void LogMessage (MSBuildMessageContainer msbuildMessage) |
| /// </summary> | ||
| public string Text { get; } | ||
|
|
||
| public MSBuildMessageContainer ( |
There was a problem hiding this comment.
| public MSBuildMessageContainer ( | |
| public MSBuildMessageContainer (int code, MessageCategory category, string text, string subCategory = null, MessageOrigin origin = null) |
| public class MessageOrigin { | ||
|
|
||
| #if NETCOREAPP | ||
| private const string ToolName = "illinker"; |
There was a problem hiding this comment.
Move that to MSBuildMessageContainer and use it only when MessageOrigin is null
Remove toolname from MessageOrigin
| Category.ToString().ToLowerInvariant(), | ||
| "IL" + Code.ToString("D4"), | ||
| !String.IsNullOrEmpty(Text) ? ": " + Text : Text); | ||
| Origin.ToString() == null ? |
There was a problem hiding this comment.
This will NRE when Origin is null. Please move the logic out of the expression
9e0eb23 to
a6ce195
Compare
8d73061 to
4f56ffb
Compare
| break; | ||
|
|
||
| // Info. | ||
| case int _code when (_code > 6000 && _code <= 8000): |
There was a problem hiding this comment.
Should we make code optional for info and diagnostic messages? It looks like they are never displayed on UI so the code is not really useful.
There was a problem hiding this comment.
Yes, there's no need for the code parameter for this category (If we are to keep the info category).
There was a problem hiding this comment.
Please change Code property to be nullable then. I am also thinking of having factory methods instead of this constructor if only some parameters are required. Something like
static MessageContainer CreateErrorMessage (string text, int code, string subcategory = MessageSubCategory.None, MessageOrigin? origin = null) { .. }
static MessageContainer CreateWarningMessage (string text, int code, string subcategory = MessageSubCategory.None, MessageOrigin? origin = null) { .. }
static MessageContainer CreateInfoMessage (string text) { .. }
static MessageContainer CreateDiagnostingMessage (string text) { .. }| cat = "warning"; | ||
| break; | ||
| default: | ||
| cat = ""; |
There was a problem hiding this comment.
My understanding is that only error or warning are recognized. Should we use different text output pattern for info and diagnostics?
There was a problem hiding this comment.
Yes, MSBuild only recognizes (and prints) warnings and errors through the format that we're using. I added the info category, so that we could still log text to console even if there isn't an error or warning on sight. I don't know how useful this would be in general, if it doesn't bring any value or there isn't any scenario where this is needed, I think I should simply remove it.
There was a problem hiding this comment.
This makes sense as I'd prefer to see this used for any linker logging. To make this effect we need to hook up the filtering to MSBuild properties. Made issue #1030 about that.
Update tests
Co-Authored-By: Marek Safar <[email protected]>
* Add Message class to output errors and warnings to the stdout. * Add simple test case. * Log messages through LinkerTestLogger * Address feedback * Add parameter to LogContainsAttribute that checks for regex match. Remove Position and move line and column to MessageOrigin. Remove MessageCode and use integer instead, check that code value falls into one of the known ranges and that it matches with the received category. * Order msbuildMC params based on their importance Remove toolname from MessageOrigin * Remove unnecessary constructor and comparisons * Clean up msbuild container to be universaly usable * Update tests * Tweak one more category * Add more tests * Use factory methods for MessageContainer Update tests * Update MessageContainerTests.cs * Use Append instead of string concatenation Co-Authored-By: Marek Safar <[email protected]> Co-authored-by: Marek Safar <[email protected]> Commit migrated from dotnet/linker@7b141d7
Related to #769