From ae61b2bc3502c8d5cf859d9c44110136ed4cbad5 Mon Sep 17 00:00:00 2001 From: filipw Date: Thu, 21 Jan 2021 20:11:16 +0100 Subject: [PATCH 1/3] use unix specific error message for MSB3644 --- src/OmniSharp.MSBuild/Logging/ErrorMessages.cs | 7 +++++++ .../Logging/MSBuildDiagnostic.cs | 18 +++++++++++++++--- src/OmniSharp.MSBuild/Logging/MSBuildLogger.cs | 5 +++-- 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 src/OmniSharp.MSBuild/Logging/ErrorMessages.cs diff --git a/src/OmniSharp.MSBuild/Logging/ErrorMessages.cs b/src/OmniSharp.MSBuild/Logging/ErrorMessages.cs new file mode 100644 index 0000000000..cbf61277d1 --- /dev/null +++ b/src/OmniSharp.MSBuild/Logging/ErrorMessages.cs @@ -0,0 +1,7 @@ +namespace OmniSharp.MSBuild.Logging +{ + internal class ErrorMessages + { + internal static string ReferenceAssembliesNotFoundUnix = "This project targets .NET version that requires reference assemblies that do not ship with OmniSharp out of the box (e.g. .NET Framework). The most common solution is to make sure Mono is installed on your machine (https://mono-project.com/download/) and that OmniSharp is started with that Mono installation (e.g. 'omnisharp.useGlobalMono':'always' in C# Extension for VS Code)."; + } +} diff --git a/src/OmniSharp.MSBuild/Logging/MSBuildDiagnostic.cs b/src/OmniSharp.MSBuild/Logging/MSBuildDiagnostic.cs index 8cc9cf1baf..92769ad7cf 100644 --- a/src/OmniSharp.MSBuild/Logging/MSBuildDiagnostic.cs +++ b/src/OmniSharp.MSBuild/Logging/MSBuildDiagnostic.cs @@ -1,5 +1,9 @@ -namespace OmniSharp.MSBuild.Logging +using System; +using OmniSharp.Utilities; + +namespace OmniSharp.MSBuild.Logging { + public class MSBuildDiagnostic { public MSBuildDiagnosticSeverity Severity { get; } @@ -31,9 +35,17 @@ private MSBuildDiagnostic( } public static MSBuildDiagnostic CreateFrom(Microsoft.Build.Framework.BuildErrorEventArgs args) - => new MSBuildDiagnostic(MSBuildDiagnosticSeverity.Error, - args.Message, args.File, args.ProjectFile, args.Subcategory, args.Code, + { + // https://github.com/dotnet/msbuild/blob/v16.8.3/src/Tasks/Resources/Strings.resx#L2155-L2158 + // for MSB3644, we should print a different message on Unix because the default one is Windows-specific + var diagnosticText = args.Code.Equals("MSB3644", StringComparison.OrdinalIgnoreCase) + && Platform.Current.OperatingSystem != Utilities.OperatingSystem.Windows + ? ErrorMessages.ReferenceAssembliesNotFoundUnix : args.Message; + + return new MSBuildDiagnostic(MSBuildDiagnosticSeverity.Error, + diagnosticText, args.File, args.ProjectFile, args.Subcategory, args.Code, args.LineNumber, args.ColumnNumber, args.EndLineNumber, args.EndColumnNumber); + } public static MSBuildDiagnostic CreateFrom(Microsoft.Build.Framework.BuildWarningEventArgs args) => new MSBuildDiagnostic(MSBuildDiagnosticSeverity.Error, diff --git a/src/OmniSharp.MSBuild/Logging/MSBuildLogger.cs b/src/OmniSharp.MSBuild/Logging/MSBuildLogger.cs index 12a652a731..3050a1e270 100644 --- a/src/OmniSharp.MSBuild/Logging/MSBuildLogger.cs +++ b/src/OmniSharp.MSBuild/Logging/MSBuildLogger.cs @@ -26,8 +26,9 @@ public ImmutableArray GetDiagnostics() => private void OnError(object sender, Microsoft.Build.Framework.BuildErrorEventArgs args) { - _logger.LogError(args.Message); - _diagnostics.Add(MSBuildDiagnostic.CreateFrom(args)); + var msBuildDiagnostic = MSBuildDiagnostic.CreateFrom(args); + _logger.LogError(msBuildDiagnostic.Message); + _diagnostics.Add(msBuildDiagnostic); } private void OnWarning(object sender, Microsoft.Build.Framework.BuildWarningEventArgs args) From d8d4ec329250c17134b83fd26c52ea1fb5858ef5 Mon Sep 17 00:00:00 2001 From: filipw Date: Fri, 22 Jan 2021 16:59:55 +0100 Subject: [PATCH 2/3] test for MSB3644 --- .../MSBuildDiagnosticTests.cs | 25 +++++++++++++++++++ .../OmniSharp.MSBuild.Tests.csproj | 1 + 2 files changed, 26 insertions(+) create mode 100644 tests/OmniSharp.MSBuild.Tests/MSBuildDiagnosticTests.cs diff --git a/tests/OmniSharp.MSBuild.Tests/MSBuildDiagnosticTests.cs b/tests/OmniSharp.MSBuild.Tests/MSBuildDiagnosticTests.cs new file mode 100644 index 0000000000..a590a51ac8 --- /dev/null +++ b/tests/OmniSharp.MSBuild.Tests/MSBuildDiagnosticTests.cs @@ -0,0 +1,25 @@ +using Microsoft.Build.Framework; +using OmniSharp.MSBuild.Logging; +using OmniSharp.Utilities; +using Xunit; +using Xunit.Abstractions; + +namespace OmniSharp.MSBuild.Tests +{ + public class MSBuildDiagnosticTests : AbstractMSBuildTestFixture + { + public MSBuildDiagnosticTests(ITestOutputHelper output) : base(output) + { + } + + [Fact] + public void MSB3644_CustomMessage() + { + var sourceDiagnostic = new BuildErrorEventArgs("test-subcategory", "MSB3644", "foo.cs", 1, 1, 1, 1, "Reference assemblies not found!", "help-keyword", "dummy-sender"); + var msbuildDiagnostic = MSBuildDiagnostic.CreateFrom(sourceDiagnostic); + + Assert.Equal(Platform.Current.OperatingSystem != OperatingSystem.Windows + ? ErrorMessages.ReferenceAssembliesNotFoundUnix : sourceDiagnostic.Message, msbuildDiagnostic.Message); + } + } +} diff --git a/tests/OmniSharp.MSBuild.Tests/OmniSharp.MSBuild.Tests.csproj b/tests/OmniSharp.MSBuild.Tests/OmniSharp.MSBuild.Tests.csproj index 7b3bf2de24..3bb59609d7 100644 --- a/tests/OmniSharp.MSBuild.Tests/OmniSharp.MSBuild.Tests.csproj +++ b/tests/OmniSharp.MSBuild.Tests/OmniSharp.MSBuild.Tests.csproj @@ -12,6 +12,7 @@ + From c011691ab34a5f3bbfdaf9db7b5ff408d4acb68e Mon Sep 17 00:00:00 2001 From: Filip W Date: Sat, 23 Jan 2021 09:52:46 +0100 Subject: [PATCH 3/3] static -> const --- src/OmniSharp.MSBuild/Logging/ErrorMessages.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OmniSharp.MSBuild/Logging/ErrorMessages.cs b/src/OmniSharp.MSBuild/Logging/ErrorMessages.cs index cbf61277d1..8e35c38a16 100644 --- a/src/OmniSharp.MSBuild/Logging/ErrorMessages.cs +++ b/src/OmniSharp.MSBuild/Logging/ErrorMessages.cs @@ -2,6 +2,6 @@ namespace OmniSharp.MSBuild.Logging { internal class ErrorMessages { - internal static string ReferenceAssembliesNotFoundUnix = "This project targets .NET version that requires reference assemblies that do not ship with OmniSharp out of the box (e.g. .NET Framework). The most common solution is to make sure Mono is installed on your machine (https://mono-project.com/download/) and that OmniSharp is started with that Mono installation (e.g. 'omnisharp.useGlobalMono':'always' in C# Extension for VS Code)."; + internal const string ReferenceAssembliesNotFoundUnix = "This project targets .NET version that requires reference assemblies that do not ship with OmniSharp out of the box (e.g. .NET Framework). The most common solution is to make sure Mono is installed on your machine (https://mono-project.com/download/) and that OmniSharp is started with that Mono installation (e.g. 'omnisharp.useGlobalMono':'always' in C# Extension for VS Code)."; } }