Skip to content

Commit

Permalink
Fix building projects restored with NuGet 5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
dsplaisted committed Nov 12, 2020
1 parent c427c6f commit e3b75e1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 15 deletions.
26 changes: 20 additions & 6 deletions src/Tasks/Microsoft.NET.Build.Tasks/LockFileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,39 @@ namespace Microsoft.NET.Build.Tasks
{
internal static class LockFileExtensions
{
public static LockFileTarget GetTargetAndThrowIfNotFound(this LockFile lockFile, string frameworkAlias, string runtime)
public static LockFileTarget GetTargetAndReturnNullIfNotFound(this LockFile lockFile, string frameworkAlias, string runtimeIdentifier)
{
LockFileTarget lockFileTarget = lockFile.GetTarget(frameworkAlias, runtime);
LockFileTarget lockFileTarget = lockFile.GetTarget(frameworkAlias, runtimeIdentifier);

if (lockFileTarget == null &&
lockFile.PackageSpec.TargetFrameworks.All(tfi => string.IsNullOrEmpty(tfi.TargetAlias)))
{
var nuGetFramework = NuGetUtils.ParseFrameworkName(frameworkAlias);
lockFileTarget = lockFile.GetTarget(nuGetFramework, runtimeIdentifier);
}

return lockFileTarget;
}

public static LockFileTarget GetTargetAndThrowIfNotFound(this LockFile lockFile, string frameworkAlias, string runtimeIdentifier)
{
LockFileTarget lockFileTarget = lockFile.GetTargetAndReturnNullIfNotFound(frameworkAlias, runtimeIdentifier);

if (lockFileTarget == null)
{
string frameworkString = frameworkAlias;
string targetMoniker = string.IsNullOrEmpty(runtime) ?
string targetMoniker = string.IsNullOrEmpty(runtimeIdentifier) ?
frameworkString :
$"{frameworkString}/{runtime}";
$"{frameworkString}/{runtimeIdentifier}";

string message;
if (string.IsNullOrEmpty(runtime))
if (string.IsNullOrEmpty(runtimeIdentifier))
{
message = string.Format(Strings.AssetsFileMissingTarget, lockFile.Path, targetMoniker, frameworkString);
}
else
{
message = string.Format(Strings.AssetsFileMissingRuntimeIdentifier, lockFile.Path, targetMoniker, frameworkString, runtime);
message = string.Format(Strings.AssetsFileMissingRuntimeIdentifier, lockFile.Path, targetMoniker, frameworkString, runtimeIdentifier);
}

throw new BuildErrorException(message);
Expand Down
10 changes: 5 additions & 5 deletions src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,8 @@ public CacheWriter(ResolvePackageAssets task)
CanWriteToCacheFile = true;
if (task.DesignTimeBuild)
{
_compileTimeTarget = _lockFile.GetTarget(_targetFramework, runtimeIdentifier: null);
_runtimeTarget = _lockFile.GetTarget(_targetFramework, _task.RuntimeIdentifier);
_compileTimeTarget = _lockFile.GetTargetAndReturnNullIfNotFound(_targetFramework, runtimeIdentifier: null);
_runtimeTarget = _lockFile.GetTargetAndReturnNullIfNotFound(_targetFramework, _task.RuntimeIdentifier);
if (_compileTimeTarget == null)
{
_compileTimeTarget = new LockFileTarget();
Expand All @@ -678,7 +678,7 @@ public CacheWriter(ResolvePackageAssets task)
}
else
{
_compileTimeTarget = _lockFile.GetTargetAndThrowIfNotFound(_targetFramework, runtime: null);
_compileTimeTarget = _lockFile.GetTargetAndThrowIfNotFound(_targetFramework, runtimeIdentifier: null);
_runtimeTarget = _lockFile.GetTargetAndThrowIfNotFound(_targetFramework, _task.RuntimeIdentifier);
}

Expand Down Expand Up @@ -1065,7 +1065,7 @@ private void WriteApphostsForShimRuntimeIdentifiers()
LockFileTarget runtimeTarget;
if (_task.DesignTimeBuild)
{
runtimeTarget = _lockFile.GetTarget(_targetFramework, runtimeIdentifier) ?? new LockFileTarget();
runtimeTarget = _lockFile.GetTargetAndReturnNullIfNotFound(_targetFramework, runtimeIdentifier) ?? new LockFileTarget();
}
else
{
Expand Down Expand Up @@ -1095,7 +1095,7 @@ private bool CanResolveApphostFromFrameworkReference()
}
else
{
var targetFramework = _lockFile.GetTarget(_targetFramework, null).TargetFramework;
var targetFramework = _lockFile.GetTargetAndThrowIfNotFound(_targetFramework, null).TargetFramework;

if (targetFramework.Version.Major >= 3
&& targetFramework.Framework.Equals(".NETCoreApp", StringComparison.OrdinalIgnoreCase))
Expand Down
50 changes: 50 additions & 0 deletions src/Tests/Microsoft.NET.Restore.Tests/RestoreWithOlderNuGet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using FluentAssertions;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using NuGet.Common;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using System.IO;
using System.Threading;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.NET.Restore.Tests
{
public class RestoreWithOlderNuGet : SdkTest
{
public RestoreWithOlderNuGet(ITestOutputHelper log) : base(log)
{
}

[Fact]
public void ItCanBuildProjectRestoredWithNuGet5_7()
{
var testProject = new TestProject()
{
TargetFrameworks = "netcoreapp3.1",
IsSdkProject = true
};
testProject.PackageReferences.Add(new TestPackageReference("Humanizer.Core", "2.8.26"));

var testAsset = _testAssetsManager.CreateTestProject(testProject);

var restoreCommand = new NuGetExeRestoreCommand(Log, testAsset.Path, testProject.Name);
restoreCommand.NuGetExeVersion = "5.7.0";
restoreCommand.Execute()
.Should()
.Pass();

new BuildCommand(testAsset)
.ExecuteWithoutRestore()
.Should()
.Pass();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class NuGetExeRestoreCommand : TestCommand

public string ProjectFile { get; }

public string NuGetExeVersion { get; set; }

public string FullPathProjectFile => Path.Combine(ProjectRootPath, ProjectFile);

public NuGetExeRestoreCommand(ITestOutputHelper log, string projectRootPath, string relativePathToProject = null) : base(log)
Expand All @@ -43,19 +45,36 @@ protected override SdkCommandSpec CreateCommand(IEnumerable<string> args)
{
throw new InvalidOperationException("Path to nuget.exe not set");
}
else if (!File.Exists(TestContext.Current.NuGetExePath))

var nugetExePath = TestContext.Current.NuGetExePath;
if (!string.IsNullOrEmpty(NuGetExeVersion))
{
// https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
nugetExePath = Path.Combine(Path.GetDirectoryName(nugetExePath), NuGetExeVersion, "nuget.exe");
}

if (!File.Exists(nugetExePath))
{
string directory = Path.GetDirectoryName(nugetExePath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

string url = string.IsNullOrEmpty(NuGetExeVersion) ?
"https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" :
$"https://dist.nuget.org/win-x86-commandline/v{NuGetExeVersion}/nuget.exe";
var client = new System.Net.WebClient();
client.DownloadFile("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", TestContext.Current.NuGetExePath);
client.DownloadFile(url, nugetExePath);
}

var ret = new SdkCommandSpec()
{
FileName = TestContext.Current.NuGetExePath,
FileName = nugetExePath,
Arguments = newArgs
};

TestContext.Current.AddTestEnvironmentVariables(ret);

return ret;
}
}
Expand Down

0 comments on commit e3b75e1

Please sign in to comment.