Skip to content

Commit

Permalink
Merge pull request #2069 from OmniSharp/feature/better-mono-error-mes…
Browse files Browse the repository at this point in the history
…sages

use an OmniSharp specific message for MSB3644
filipw authored Jan 24, 2021
2 parents ff7bdca + c011691 commit aaf1fef
Showing 5 changed files with 51 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/OmniSharp.MSBuild/Logging/ErrorMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OmniSharp.MSBuild.Logging
{
internal class ErrorMessages
{
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).";
}
}
18 changes: 15 additions & 3 deletions src/OmniSharp.MSBuild/Logging/MSBuildDiagnostic.cs
Original file line number Diff line number Diff line change
@@ -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,
5 changes: 3 additions & 2 deletions src/OmniSharp.MSBuild/Logging/MSBuildLogger.cs
Original file line number Diff line number Diff line change
@@ -26,8 +26,9 @@ public ImmutableArray<MSBuildDiagnostic> 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)
25 changes: 25 additions & 0 deletions tests/OmniSharp.MSBuild.Tests/MSBuildDiagnosticTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\OmniSharp.MSBuild\OmniSharp.MSBuild.csproj" />
<ProjectReference Include="..\..\src\OmniSharp.Shared\OmniSharp.Shared.csproj" />
<ProjectReference Include="..\TestUtility\TestUtility.csproj" />
</ItemGroup>

0 comments on commit aaf1fef

Please sign in to comment.