-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2403 from JoeRobich/return-meaningful-error
Return meaningful error when pinned SDK version is not found.
- Loading branch information
Showing
6 changed files
with
211 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace OmniSharp.Services | ||
{ | ||
public class DotNetVersion | ||
{ | ||
public static DotNetVersion FailedToStartError { get; } = new DotNetVersion("`dotnet --version` failed to start."); | ||
|
||
public bool HasError { get; } | ||
public string ErrorMessage { get; } | ||
|
||
public SemanticVersion Version { get; } | ||
|
||
private DotNetVersion(SemanticVersion version) | ||
{ | ||
Version = version; | ||
} | ||
|
||
private DotNetVersion(string errorMessage) | ||
{ | ||
HasError = true; | ||
ErrorMessage = errorMessage; | ||
} | ||
|
||
public static DotNetVersion Parse(List<string> lines) | ||
{ | ||
if (lines == null || lines.Count == 0) | ||
{ | ||
return new DotNetVersion("`dotnet --version` produced no output."); | ||
} | ||
|
||
if (SemanticVersion.TryParse(lines[0], out var version)) | ||
{ | ||
return new DotNetVersion(version); | ||
} | ||
|
||
var requestedSdkVersion = string.Empty; | ||
var globalJsonFile = string.Empty; | ||
|
||
foreach (var line in lines) | ||
{ | ||
var colonIndex = line.IndexOf(':'); | ||
if (colonIndex >= 0) | ||
{ | ||
var name = line.Substring(0, colonIndex).Trim(); | ||
var value = line.Substring(colonIndex + 1).Trim(); | ||
|
||
if (string.IsNullOrEmpty(requestedSdkVersion) && name.Equals("Requested SDK version", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
requestedSdkVersion = value; | ||
} | ||
else if (string.IsNullOrEmpty(globalJsonFile) && name.Equals("global.json file", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
globalJsonFile = value; | ||
} | ||
} | ||
} | ||
|
||
return requestedSdkVersion.Length > 0 && globalJsonFile.Length > 0 | ||
? new DotNetVersion($"Install the [{requestedSdkVersion}] .NET SDK or update [{globalJsonFile}] to match an installed SDK.") | ||
: new DotNetVersion($"Unexpected output from `dotnet --version`: {string.Join(Environment.NewLine, lines)}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Collections.Generic; | ||
using OmniSharp.Services; | ||
using TestUtility; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace OmniSharp.Tests | ||
{ | ||
public class DotNetVersionFacts : AbstractTestFixture | ||
{ | ||
public DotNetVersionFacts(ITestOutputHelper output) | ||
: base(output) | ||
{ | ||
} | ||
|
||
[Theory] | ||
[InlineData("6.0.201")] | ||
[InlineData("7.0.100-preview.2.22153.17")] | ||
public void ParseVersion(string versionString) | ||
{ | ||
var cliVersion = DotNetVersion.Parse(new() { versionString }); | ||
|
||
Assert.False(cliVersion.HasError, $"{versionString} did not successfully parse."); | ||
|
||
Assert.Equal(versionString, cliVersion.Version.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void ParseErrorMessage() | ||
{ | ||
const string RequestedSdkVersion = "6.0.301-rtm.22263.15"; | ||
const string GlobalJsonFile = "/Users/username/Source/format/global.json"; | ||
const string ExpectedErrorMessage = $"Install the [{RequestedSdkVersion}] .NET SDK or update [{GlobalJsonFile}] to match an installed SDK."; | ||
|
||
var lines = new List<string>() { | ||
"The command could not be loaded, possibly because:", | ||
" * You intended to execute a .NET application:", | ||
" The application '--version' does not exist.", | ||
" * You intended to execute a .NET SDK command:", | ||
" A compatible .NET SDK was not found.", | ||
"", | ||
$"Requested SDK version: {RequestedSdkVersion}", | ||
$"global.json file: {GlobalJsonFile}", | ||
"", | ||
"Installed SDKs:", | ||
"6.0.105 [/usr/local/share/dotnet/sdk]", | ||
"6.0.202 [/usr/local/share/dotnet/sdk]", | ||
"6.0.300 [/usr/local/share/dotnet/sdk]", | ||
"7.0.100-preview.4.22252.9 [/usr/local/share/dotnet/sdk]", | ||
"", | ||
$"Install the [{RequestedSdkVersion}] .NET SDK or update [{GlobalJsonFile}] to match an installed SDK.", | ||
"", | ||
"Learn about SDK resolution:", | ||
"https://aka.ms/dotnet/sdk-not-found" | ||
}; | ||
|
||
var cliVersion = DotNetVersion.Parse(lines); | ||
|
||
Assert.True(cliVersion.HasError); | ||
|
||
Assert.Equal(ExpectedErrorMessage, cliVersion.ErrorMessage); | ||
} | ||
} | ||
} |