Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix building projects restored with NuGet 5.7 #14517

Merged
merged 2 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good.


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
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ public void It_resolves_conflicts(bool isSdk, bool usePackagesConfig)

if (usePackagesConfig)
{
testAsset.NuGetRestore(Log, relativePath: AppName);
new NuGetExeRestoreCommand(Log, testAsset.TestRoot, AppName)
.Execute()
.Should()
.Pass();
}
else
{
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)
{
}

[WindowsOnlyFact]
public void ItCanBuildProjectRestoredWithNuGet5_7()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

{
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 @@ -11,54 +11,29 @@

namespace Microsoft.NET.TestFramework.Commands
{
public class NuGetRestoreCommand : TestCommand
public class NuGetExeRestoreCommand : TestCommand
{
private List<string> _sources = new List<string>();

private readonly string _projectRootPath;
public string ProjectRootPath => _projectRootPath;

public string ProjectFile { get; }

public string NuGetExeVersion { get; set; }

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

public NuGetRestoreCommand(ITestOutputHelper log, string projectRootPath, string relativePathToProject = null) : base(log)
public NuGetExeRestoreCommand(ITestOutputHelper log, string projectRootPath, string relativePathToProject = null) : base(log)
{
_projectRootPath = projectRootPath;
ProjectFile = MSBuildCommand.FindProjectFile(ref _projectRootPath, relativePathToProject);
}

public NuGetRestoreCommand AddSource(string source)
{
_sources.Add(source);
return this;
}

public NuGetRestoreCommand AddSourcesFromCurrentConfig()
{
var settings = Settings.LoadDefaultSettings(Directory.GetCurrentDirectory(), null, null);
var packageSourceProvider = new PackageSourceProvider(settings);

foreach (var packageSource in packageSourceProvider.LoadPackageSources())
{
_sources.Add(packageSource.Source);
}

return this;
}

protected override SdkCommandSpec CreateCommand(IEnumerable<string> args)
{
var newArgs = new List<string>();

newArgs.Add("restore");

if (_sources.Any())
{
newArgs.Add("-Source");
newArgs.Add(string.Join(";", _sources));
}

newArgs.Add(FullPathProjectFile);

newArgs.Add("-PackagesDirectory");
Expand All @@ -70,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
16 changes: 0 additions & 16 deletions src/Tests/Microsoft.NET.TestFramework/TestAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,6 @@ public RestoreCommand GetRestoreCommand(ITestOutputHelper log, string relativePa
return new RestoreCommand(log, System.IO.Path.Combine(TestRoot, relativePath));
}

public NuGetRestoreCommand GetNuGetRestoreCommand(ITestOutputHelper log, string relativePath = "")
{
return new NuGetRestoreCommand(log, System.IO.Path.Combine(TestRoot, relativePath))
.AddSourcesFromCurrentConfig();
}

public TestAsset Restore(ITestOutputHelper log, string relativePath = "", params string[] args)
{
var commandResult = GetRestoreCommand(log, relativePath)
Expand All @@ -197,16 +191,6 @@ public TestAsset Restore(ITestOutputHelper log, string relativePath = "", params
return this;
}

public TestAsset NuGetRestore(ITestOutputHelper log, string relativePath = "", params string[] args)
{
var commandResult = GetNuGetRestoreCommand(log, relativePath)
.Execute(args);

commandResult.Should().Pass();

return this;
}

private bool IsBinOrObjFolder(string directory)
{
var binFolder = $"{System.IO.Path.DirectorySeparatorChar}bin";
Expand Down