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

Output Mono.Linker warnings through MSBuild - #968

Merged
marek-safar merged 15 commits into
dotnet:masterfrom
mateoatr:msbuild_output
Mar 31, 2020
Merged

marek-safar merged 15 commits into
dotnet:masterfrom
mateoatr:msbuild_output

Conversation

@mateoatr

Copy link
Copy Markdown
Contributor

Related to #769

@mateoatr

Copy link
Copy Markdown
Contributor Author

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.

@mateoatr mateoatr changed the title [WIP] Output Mono.Linker warnings through MSBuild Output Mono.Linker warnings through MSBuild Mar 10, 2020
@mateoatr
mateoatr requested a review from vitek-karas March 10, 2020 18:40
Comment thread src/linker/Linker/LinkContext.cs Resolved
Comment thread src/linker/Linker/MessageCode.cs Outdated
Comment on lines +2 to +5
public enum MessageCode {
L100, // Error
L400, // Warning
L900, // Info.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are still debating which way to go on this. For now, I simply used IL as a prefix.

Comment thread src/linker/Linker/Message.cs Resolved
Comment thread src/linker/Linker/Message.cs Resolved
Comment thread src/linker/Linker/Message.cs Resolved
Comment thread src/linker/Linker/Message.cs Resolved
Comment thread src/linker/Linker/Message.cs Resolved
Comment thread src/linker/Linker/Message.cs Resolved
Comment thread src/linker/Linker/MessageOrigin.cs Resolved
Comment thread src/linker/Linker/MessageOrigin.cs Resolved
#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" })]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mrvoorhe mrvoorhe Mar 11, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/linker/Linker/ConsoleLogger.cs Resolved
Comment thread src/linker/Linker/Position.cs Resolved
Comment thread src/linker/Linker/MessageCode.cs Resolved
Comment thread src/linker/Linker/Position.cs Resolved
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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the order, text is the most important and should never be empty and not last in argument

Comment thread src/linker/Linker/LinkContext.cs Outdated
Logger.LogMessage (importance, "{0}", message);
}

public void LogMessage (MSBuildMessageContainer msBuildMessage)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void LogMessage (MSBuildMessageContainer msBuildMessage)
public void LogMessage (MSBuildMessageContainer msbuildMessage)

/// </summary>
public string Text { get; }

public MSBuildMessageContainer (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public MSBuildMessageContainer (
public MSBuildMessageContainer (int code, MessageCategory category, string text, string subCategory = null, MessageOrigin origin = null)

Comment thread src/linker/Linker/MessageOrigin.cs Outdated
public class MessageOrigin {

#if NETCOREAPP
private const string ToolName = "illinker";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will NRE when Origin is null. Please move the logic out of the expression

@marek-safar

Copy link
Copy Markdown
Contributor

@mateoatr I went ahead and clean it up so there could be only one way to log from illinker (made few more comments where it still needs some investigation). The tracking issue for remaining work is #1022

Comment thread src/linker/Linker/MessageContainer.cs Outdated
break;

// Info.
case int _code when (_code > 6000 && _code <= 8000):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there's no need for the code parameter for this category (If we are to keep the info category).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 = "";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that only error or warning are recognized. Should we use different text output pattern for info and diagnostics?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/linker/Linker/MessageContainer.cs Resolved
@marek-safar
marek-safar merged commit 7b141d7 into dotnet:master Mar 31, 2020
tkapin pushed a commit to tkapin/runtime that referenced this pull request Jan 31, 2023
* 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants