Merge pull request #6689 from livarcocc/resolver_min_msbuild

Adding a validation for the minimum MSBuild version.
This commit is contained in:
Livar 2017-05-24 21:16:47 -07:00 committed by GitHub
commit b42ac76787
4 changed files with 108 additions and 24 deletions

View file

@ -50,22 +50,36 @@ namespace Microsoft.DotNet.MSBuildSdkResolver
}
msbuildSdksDir = Path.Combine(netcoreSdkDir, "Sdks");
netcoreSdkVersion = new DirectoryInfo(netcoreSdkDir).Name;;
}
netcoreSdkVersion = new DirectoryInfo(netcoreSdkDir).Name;
if (!IsNetCoreSDKOveridden(netcoreSdkVersion) &&
IsNetCoreSDKSmallerThanTheMinimumVersion(netcoreSdkVersion, sdkReference.MinimumVersion))
if (IsNetCoreSDKSmallerThanTheMinimumVersion(netcoreSdkVersion, sdkReference.MinimumVersion))
{
return factory.IndicateFailure(
new[]
{
$"Version {netcoreSdkVersion} of the SDK is smaller than the minimum version"
$"Version {netcoreSdkVersion} of the .NET Core SDK is smaller than the minimum version"
+ $" {sdkReference.MinimumVersion} requested. Check that a recent enough .NET Core SDK is"
+ " installed, increase the minimum version specified in the project, or increase"
+ " the version specified in global.json."
});
}
var minimumMSBuildVersionString =
File.ReadAllLines(Path.Combine(netcoreSdkDir, "minimumMSBuildVersion"))[0];
var minimumMSBuildVersion = Version.Parse(minimumMSBuildVersionString);
if (context.MSBuildVersion < minimumMSBuildVersion)
{
return factory.IndicateFailure(
new[]
{
$"Version {netcoreSdkVersion} of the .NET Core SDK requires at least version {minimumMSBuildVersionString}"
+ $" of MSBuild. The current available version of MSBuild is {context.MSBuildVersion.ToString()}."
+ " Change the .NET Core SDK specified in global.json to an older version that requires the MSBuild"
+ " version currently available."
});
}
}
string msbuildSdkDir = Path.Combine(msbuildSdksDir, sdkReference.Name, "Sdk");
if (!Directory.Exists(msbuildSdkDir))
{
@ -80,11 +94,6 @@ namespace Microsoft.DotNet.MSBuildSdkResolver
return factory.IndicateSuccess(msbuildSdkDir, netcoreSdkVersion);
}
private bool IsNetCoreSDKOveridden(string netcoreSdkVersion)
{
return netcoreSdkVersion == null;
}
private bool IsNetCoreSDKSmallerThanTheMinimumVersion(string netcoreSdkVersion, string minimumVersion)
{
FXVersion netCoreSdkFXVersion;
@ -101,7 +110,7 @@ namespace Microsoft.DotNet.MSBuildSdkResolver
return true;
}
return FXVersion.Compare(netCoreSdkFXVersion, minimumFXVersion) == -1;
return FXVersion.Compare(netCoreSdkFXVersion, minimumFXVersion) < 0;
}
private string ResolveNetcoreSdkDirectory(SdkResolverContext context)

View file

@ -0,0 +1 @@
15.3.0

View file

@ -36,6 +36,12 @@
<ProjectReference Include="..\tool_nuget\tool_nuget.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="minimumMSBuildVersion">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<Target Name="PublishAspNetRuntimePackageStore"
AfterTargets="CrossgenPublishDir">
<ItemGroup>

View file

@ -73,11 +73,37 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
result.Path.Should().BeNull();
result.Version.Should().BeNull();
result.Warnings.Should().BeNullOrEmpty();
result.Errors.Should().Contain("Version 99.99.99 of the SDK is smaller than the minimum version 999.99.99"
result.Errors.Should().Contain("Version 99.99.99 of the .NET Core SDK is smaller than the minimum version 999.99.99"
+ " requested. Check that a recent enough .NET Core SDK is installed, increase the minimum version"
+ " specified in the project, or increase the version specified in global.json.");
}
[Fact]
public void ItReturnsNullWhenTheSDKRequiresAHigherVersionOfMSBuildThanTheOneAvailable()
{
var environment = new TestEnvironment();
var expected =
environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "99.99.99", new Version(2, 0));
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
var resolver = environment.CreateResolver();
var result = (MockResult)resolver.Resolve(
new SdkReference("Some.Test.Sdk", null, "99.99.99"),
new MockContext
{
ProjectFilePath = environment.TestDirectory.FullName
},
new MockFactory());
result.Success.Should().BeFalse();
result.Path.Should().BeNull();
result.Version.Should().BeNull();
result.Warnings.Should().BeNullOrEmpty();
result.Errors.Should().Contain("Version 99.99.99 of the .NET Core SDK requires at least version 2.0 of MSBuild."
+ " The current available version of MSBuild is 1.0. Change the .NET Core SDK specified in global.json to an older"
+ " version that requires the MSBuild version currently available.");
}
[Fact]
public void ItReturnsTheVersionIfItIsEqualToTheMinVersion()
{
@ -147,27 +173,63 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
=> new DotNetMSBuildSdkResolver(GetEnvironmentVariable);
public DirectoryInfo GetSdkDirectory(ProgramFiles programFiles, string sdkName, string sdkVersion)
=> TestDirectory.GetDirectory(GetProgramFilesDirectory(programFiles).FullName, "dotnet", "sdk", sdkVersion, "Sdks", sdkName, "Sdk");
=> TestDirectory.GetDirectory(
GetProgramFilesDirectory(programFiles).FullName,
"dotnet",
"sdk",
sdkVersion,
"Sdks",
sdkName,
"Sdk");
public DirectoryInfo GetProgramFilesDirectory(ProgramFiles programFiles)
=> TestDirectory.GetDirectory($"ProgramFiles{programFiles}");
public DirectoryInfo CreateSdkDirectory(ProgramFiles programFiles, string sdkVersion, string sdkName)
public DirectoryInfo CreateSdkDirectory(
ProgramFiles programFiles,
string sdkName,
string sdkVersion,
Version minimumMSBuildVersion = null)
{
var dir = GetSdkDirectory(programFiles, sdkVersion, sdkName);
var dir = GetSdkDirectory(programFiles, sdkName, sdkVersion);
dir.Create();
CreateMSBuildRequiredVersionFile(programFiles, sdkVersion, minimumMSBuildVersion);
return dir;
}
public void CreateMuxerAndAddToPath(ProgramFiles programFiles)
{
var muxerDirectory = TestDirectory.GetDirectory(GetProgramFilesDirectory(programFiles).FullName, "dotnet");
var muxerDirectory =
TestDirectory.GetDirectory(GetProgramFilesDirectory(programFiles).FullName, "dotnet");
new FileInfo(Path.Combine(muxerDirectory.FullName, Muxer)).Create();
PathEnvironmentVariable = $"{muxerDirectory}{Path.PathSeparator}{PathEnvironmentVariable}";
}
private void CreateMSBuildRequiredVersionFile(
ProgramFiles programFiles,
string sdkVersion,
Version minimumMSBuildVersion)
{
if (minimumMSBuildVersion == null)
{
minimumMSBuildVersion = new Version(1, 0);
}
var cliDirectory = TestDirectory.GetDirectory(
GetProgramFilesDirectory(programFiles).FullName,
"dotnet",
"sdk",
sdkVersion);
File.WriteAllText(
Path.Combine(cliDirectory.FullName, "minimumMSBuildVersion"),
minimumMSBuildVersion.ToString());
}
public void CreateGlobalJson(DirectoryInfo directory, string version)
=> File.WriteAllText(directory.GetFile("global.json").FullName,
$@"{{ ""sdk"": {{ ""version"": ""{version}"" }} }}");
@ -188,6 +250,12 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
{
public new string ProjectFilePath { get => base.ProjectFilePath; set => base.ProjectFilePath = value; }
public new string SolutionFilePath { get => base.SolutionFilePath; set => base.SolutionFilePath = value; }
public new Version MSBuildVersion { get => base.MSBuildVersion; set => base.MSBuildVersion = value; }
public MockContext()
{
MSBuildVersion = new Version(1, 0);
}
}
private sealed class MockFactory : SdkResultFactory