Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ private async Task<PackageSpec> GetPackageSpecAsync(ISettings settings)
FrameworkName = targetFramework,
Warn = warn,
PackagesToPrune = packagesToPrune,
TargetAlias = targetFramework.ToString(),
};

// Build up runtime information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ private AssetsFileDependenciesSnapshot(LockFile? lockFile, AssetsFileDependencie
continue;
}

string targetAlias = GetTargetAlias(lockFileTarget.Name);
string targetAlias = lockFileTarget.TargetAlias;

previous.DataByTarget.TryGetValue(targetAlias, out AssetsFileTarget? previousTarget);

ImmutableArray<AssetsFileLogMessage> logMessages = ParseLogMessages(lockFile, previousTarget, lockFileTarget.Name);
ImmutableArray<AssetsFileLogMessage> logMessages = ParseLogMessages(lockFile, previousTarget, targetAlias);

dataByTarget.Add(
targetAlias,
Expand All @@ -114,34 +114,6 @@ private AssetsFileDependenciesSnapshot(LockFile? lockFile, AssetsFileDependencie
DataByTarget = dataByTarget.ToImmutable();
return;

string GetTargetAlias(string lockFileTargetName)
{
// In some places, the target alias specified in the project file (e.g. "net472") will not
// match the target name used throughout the lock file (e.g. ".NETFramework,Version=v4.7.2").
// The dependencies tree only uses the target alias (what's in the project file) so we need
// to map back to that. See https://github.com/dotnet/project-system/issues/6832.

if (lockFile.PackageSpec.TargetFrameworks.Any(t => t.TargetAlias == lockFileTargetName))
{
// The target name used in the assets file matches the target alias in the project file.
return lockFileTargetName;
}

// The target name used in the assets file does NOT match any target alias in the project.
// Attempt to find the name used in the project.
foreach (TargetFrameworkInformation targetInfo in lockFile.PackageSpec.TargetFrameworks)
{
if (targetInfo.FrameworkName.DotNetFrameworkName == lockFileTargetName)
{
// We found a match, so return the alias.
return targetInfo.TargetAlias;
}
}

// No match was found. Not ideal. Nothing to do but return the original value.
return lockFileTargetName;
}

static ImmutableArray<AssetsFileLogMessage> ParseLogMessages(LockFile lockFile, AssetsFileTarget? previousTarget, string target)
{
if (lockFile.LogMessages.Count == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ internal static List<TargetFrameworkInformation> GetTargetFrameworkInfos(IReadOn
FrameworkReferences = GetFrameworkReferences(msBuildProjectInstance),
PackagesToPrune = prunedReferences,
RuntimeIdentifierGraphPath = msBuildProjectInstance.GetProperty(nameof(TargetFrameworkInformation.RuntimeIdentifierGraphPath)),
TargetAlias = targetAlias,
TargetAlias = string.IsNullOrEmpty(targetAlias) ? targetFramework.ToString() : targetAlias,
Warn = warn
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable
~NuGet.Commands.IRestoreTargetGraph.TargetAlias.get -> string
~NuGet.Commands.IRestoreTargetGraph.TargetGraphNameWithAlias.get -> string
~NuGet.Commands.RestoreTargetGraph.TargetAlias.get -> string
~NuGet.Commands.RestoreTargetGraph.TargetGraphNameWithAlias.get -> string
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable
~NuGet.Commands.IRestoreTargetGraph.TargetAlias.get -> string
~NuGet.Commands.IRestoreTargetGraph.TargetGraphNameWithAlias.get -> string
~NuGet.Commands.RestoreTargetGraph.TargetAlias.get -> string
~NuGet.Commands.RestoreTargetGraph.TargetGraphNameWithAlias.get -> string
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ internal async Task<CompatibilityCheckResult> CheckAsync(
/// </summary>
private static RestoreLogMessage GetErrorMessage(NuGetLogCode logCode, CompatibilityIssue issue, RestoreTargetGraph graph)
{
return RestoreLogMessage.CreateError(logCode, issue.Format(), issue.Package.Id, graph.TargetGraphName);
return RestoreLogMessage.CreateError(logCode, issue.Format(), issue.Package.Id, graph.TargetGraphNameWithAlias);
}

private static IEnumerable<NuGetFramework> GetPackageFrameworks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static IEnumerable<RestoreLogMessage> GetMissingLowerBounds(IEnumerable<I
.OrderBy(e => e.Child.Name, StringComparer.OrdinalIgnoreCase)
.ThenBy(e => e.Child.Version)
.ThenBy(e => e.Parent.Name, StringComparer.OrdinalIgnoreCase)
.Select(e => GetMissingLowerBoundMessage(e, graph.TargetGraphName)));
.Select(e => GetMissingLowerBoundMessage(e, graph.TargetGraphNameWithAlias)));
}

return messages;
Expand Down Expand Up @@ -159,7 +159,7 @@ public static IEnumerable<RestoreLogMessage> GetBumpedUpDependencies(
match.Key.Name,
match.Key.Version);

var graphName = indexedGraph.Graph.TargetGraphName;
var graphName = indexedGraph.Graph.TargetGraphNameWithAlias;

messages.Add(RestoreLogMessage.CreateWarning(NuGetLogCode.NU1601, message, match.Key.Name, graphName));
}
Expand Down Expand Up @@ -290,7 +290,7 @@ public static IEnumerable<RestoreLogMessage> GetDependenciesAboveUpperBounds(Lis
child,
actual);

messages.Add(RestoreLogMessage.CreateWarning(NuGetLogCode.NU1608, message, dependencyId, graph.TargetGraphName));
messages.Add(RestoreLogMessage.CreateWarning(NuGetLogCode.NU1608, message, dependencyId, graph.TargetGraphNameWithAlias));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal static async Task LogAsync(IEnumerable<IRestoreTargetGraph> graphs, Rem
{
var tasks = graphs.SelectMany(graph => graph.Unresolved.Select(e =>
GetMessageAsync(
graph.TargetGraphName,
graph.TargetGraphNameWithAlias,
e,
context.FilterDependencyProvidersForLibrary(e),
context.PackageSourceMapping.IsEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace NuGet.Commands
{
public interface IRestoreTargetGraph
{
string TargetGraphNameWithAlias { get; }

string TargetGraphName { get; }

/// <summary>
Expand Down
68 changes: 28 additions & 40 deletions src/NuGet.Core/NuGet.Commands/RestoreCommand/LockFileBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ public LockFile CreateLockFile(LockFile previousLockFile,

if (project.RestoreMetadata?.ProjectStyle == ProjectStyle.PackageReference)
{
AddProjectFileDependenciesForPackageReference(project, lockFile, targetGraphs);
}
else
{
AddProjectFileDependenciesForSpec(project, lockFile);
AddProjectFileDependenciesForPackageReference(project, lockFile, targetGraphs.AsList());
}

// Record all libraries used
Expand Down Expand Up @@ -164,12 +160,20 @@ public LockFile CreateLockFile(LockFile previousLockFile,
.OrderBy(graph => graph.Framework.ToString(), StringComparer.Ordinal)
.ThenBy(graph => graph.RuntimeIdentifier, StringComparer.Ordinal))
{
var target = new LockFileTarget
{
TargetFramework = targetGraph.Framework,
RuntimeIdentifier = targetGraph.RuntimeIdentifier,
TargetAlias = targetGraph.TargetAlias,
};
var target = lockFile.Version >= 4 ?
new LockFileTarget
{
TargetFramework = targetGraph.Framework,
RuntimeIdentifier = targetGraph.RuntimeIdentifier,
TargetAlias = targetGraph.TargetAlias,
Name = targetGraph.TargetGraphNameWithAlias
} :
new LockFileTarget
{
TargetFramework = targetGraph.Framework,
RuntimeIdentifier = targetGraph.RuntimeIdentifier,
TargetAlias = targetGraph.TargetAlias,
};

var flattenedFlags = IncludeFlagUtils.FlattenDependencyTypes(_includeFlagGraphs, project, targetGraph);

Expand Down Expand Up @@ -266,7 +270,7 @@ public LockFile CreateLockFile(LockFile previousLockFile,
NuGetLogCode.NU1701,
message,
library.Name,
targetGraph.TargetGraphName);
targetGraph.TargetGraphNameWithAlias);

_logger.Log(logMessage);

Expand Down Expand Up @@ -391,42 +395,25 @@ private static string GetFallbackFrameworkString(NuGetFramework framework)
return string.Join(", ", frameworks);
}

private static void AddProjectFileDependenciesForSpec(PackageSpec project, LockFile lockFile)
private static void AddProjectFileDependenciesForPackageReference(PackageSpec project, LockFile lockFile, List<RestoreTargetGraph> targetGraphs)
{
// Use empty string as the key of dependencies shared by all frameworks
lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
string.Empty,
project.Dependencies
.Select(group => group.LibraryRange.ToLockFileDependencyGroupString())
.OrderBy(group => group, StringComparer.Ordinal)));
bool isV4LockFile = lockFile.Version >= 4;

foreach (var frameworkInfo in project.TargetFrameworks
.OrderBy(framework => framework.FrameworkName.ToString(),
StringComparer.Ordinal))
{
lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
frameworkInfo.FrameworkName.ToString(),
frameworkInfo.Dependencies
.Select(x => x.LibraryRange.ToLockFileDependencyGroupString())
.OrderBy(dependency => dependency, StringComparer.Ordinal)));
}
}

private static void AddProjectFileDependenciesForPackageReference(PackageSpec project, LockFile lockFile, IEnumerable<RestoreTargetGraph> targetGraphs)
{
// For NETCore put everything under a TFM section
// Projects are included for NETCore
foreach (var frameworkInfo in project.TargetFrameworks
.OrderBy(framework => framework.FrameworkName.ToString(),
.OrderBy(framework => framework.TargetAlias,
StringComparer.Ordinal))
{
var dependencies = new List<LibraryRange>();
dependencies.AddRange(project.Dependencies.Select(e => e.LibraryRange));
dependencies.AddRange(frameworkInfo.Dependencies.Select(e => e.LibraryRange));

var targetGraph = targetGraphs.SingleOrDefault(graph =>
graph.Framework.Equals(frameworkInfo.FrameworkName)
&& string.IsNullOrEmpty(graph.RuntimeIdentifier));
RestoreTargetGraph targetGraph = !string.IsNullOrEmpty(frameworkInfo.TargetAlias) ?
targetGraphs.SingleOrDefault(graph =>
graph.TargetAlias.Equals(frameworkInfo.TargetAlias)
&& string.IsNullOrEmpty(graph.RuntimeIdentifier)) :
targetGraphs.SingleOrDefault(graph =>
graph.Framework.Equals(frameworkInfo.FrameworkName)
&& string.IsNullOrEmpty(graph.RuntimeIdentifier));

var resolvedEntry = targetGraph?
.Flattened
Expand All @@ -452,8 +439,9 @@ private static void AddProjectFileDependenciesForPackageReference(PackageSpec pr
}

// Add entry
string framework = isV4LockFile && string.IsNullOrEmpty(frameworkInfo.TargetAlias) ? frameworkInfo.FrameworkName.ToString() : frameworkInfo.TargetAlias;
var dependencyGroup = new ProjectFileDependencyGroup(
frameworkInfo.FrameworkName.ToString(),
framework,
uniqueDependencies.Select(x => x.ToLockFileDependencyGroupString())
.OrderBy(dependency => dependency, StringComparer.Ordinal));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public async Task<Tuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> TryRestor
TelemetryActivity telemetryActivity,
string telemetryPrefix)
{
// Aliasing should only be supported in new world.
// TODO NK - this is where we do the splitting
var allRuntimes = RuntimeGraph.Empty;
var frameworkTasks = new List<Task<RestoreTargetGraph>>();
var graphs = new List<RestoreTargetGraph>();
Expand Down Expand Up @@ -315,7 +317,7 @@ internal async Task<bool> ResolutionSucceeded(IEnumerable<RestoreTargetGraph> gr
string.Join(", ", conflict.Requests),
graphName);

_logger.Log(RestoreLogMessage.CreateError(NuGetLogCode.NU1106, message, conflict.Name, graph.TargetGraphName));
_logger.Log(RestoreLogMessage.CreateError(NuGetLogCode.NU1106, message, conflict.Name, graph.TargetGraphNameWithAlias));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ public void ApplyStandardProperties(RestoreRequest request)

request.RequestedRuntimes.UnionWith(Runtimes);
request.FallbackRuntimes.UnionWith(FallbackRuntimes);

// TODO NK - set-up the lock file version here.
request.LockFileVersion = LockFileFormat.Version;

// Run runtime asset checks for project.json, and for other types if enabled.
Expand Down
Loading
Loading