Skip to content
Open
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
61 changes: 38 additions & 23 deletions src/code/PSResourceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -736,15 +736,25 @@ public static bool TryConvertFromJson(
foreach (JsonElement dependencyGroup in dependencyGroupsElement.EnumerateArray())
{
NuGetFramework groupFramework = NuGetFramework.AnyFramework;
if (dependencyGroup.TryGetProperty("targetFramework", out JsonElement tfmElement))
if (dependencyGroup.TryGetProperty("targetFramework", out JsonElement tfmElement) &&
tfmElement.ValueKind == JsonValueKind.String)
{
string tfmString = tfmElement.GetString();
if (!string.IsNullOrWhiteSpace(tfmString))
{
NuGetFramework parsed = NuGetFramework.Parse(tfmString);
if (parsed != null && !parsed.IsUnsupported)
try
{
groupFramework = parsed;
NuGetFramework parsed = NuGetFramework.Parse(tfmString);
if (parsed != null && !parsed.IsUnsupported)
{
groupFramework = parsed;
}
}
catch
{
// Unparseable TFM strings are treated as framework-agnostic so
// the dependency group remains eligible for fallback selection.
// groupFramework will just be NuGetFramework.AnyFramework;
}
}
}
Expand All @@ -753,39 +763,44 @@ public static bool TryConvertFromJson(

// Select the best matching group using FrameworkReducer
JsonElement? selectedGroupElement = null;
try
if (groupMap.Count > 0)
{
if (groupMap.Count > 0)
try
{
NuGetFramework currentFramework = GetCurrentFrameworkForDeps();
var reducer = new FrameworkReducer();
NuGetFramework bestMatch = reducer.GetNearest(currentFramework, groupMap.Select(g => g.framework));

if (bestMatch != null)
{
selectedGroupElement = groupMap.FirstOrDefault(g => g.framework.Equals(bestMatch)).groupElement;
var match = groupMap.FirstOrDefault(g => g.framework.Equals(bestMatch));
if (match.groupElement.ValueKind != JsonValueKind.Undefined)
{
selectedGroupElement = match.groupElement;
}
}
}
}
catch
{
// If TFM selection fails, fall through to fallback
}
catch
{
// If TFM selection fails, fall through to fallback
}

// Fallback: use the "any" / no-TFM group, or the first group
if (selectedGroupElement == null)
{
var fallback = groupMap.FirstOrDefault(g =>
g.framework == null ||
g.framework.Equals(NuGetFramework.AnyFramework) ||
g.framework.IsUnsupported);
selectedGroupElement = fallback.groupElement.ValueKind != JsonValueKind.Undefined
? fallback.groupElement
: groupMap.FirstOrDefault().groupElement;
// Fallback: use the "any" / no-TFM group, or the first group
if (selectedGroupElement == null)
{
var fallback = groupMap.FirstOrDefault(g =>
g.framework == null ||
g.framework.Equals(NuGetFramework.AnyFramework) ||
g.framework.IsUnsupported);
selectedGroupElement = fallback.groupElement.ValueKind != JsonValueKind.Undefined
? fallback.groupElement
: groupMap[0].groupElement;
}
}

// Parse dependencies from the selected group
if (selectedGroupElement.HasValue &&
selectedGroupElement.Value.ValueKind == JsonValueKind.Object &&
selectedGroupElement.Value.TryGetProperty("dependencies", out JsonElement dependenciesElement) &&
dependenciesElement.ValueKind == JsonValueKind.Array)
{
Expand Down
Loading