Skip to content

Use Ordinal string comparison for TargetAlias - #7224

Merged
nkolev92 merged 2 commits into
NuGet:devfrom
SimonCropp:Use-Ordinal-string-comparison-for-TargetAlias
Mar 24, 2026
Merged

nkolev92 merged 2 commits into
NuGet:devfrom
SimonCropp:Use-Ordinal-string-comparison-for-TargetAlias

Conversation

@SimonCropp

@SimonCropp SimonCropp commented Mar 23, 2026 •

Copy link
Copy Markdown
Contributor

Replace the instance Equals call with string.Equals(t.TargetAlias, frameworkAlias, StringComparison.Ordinal) to make the comparison explicit and culture-independent and to avoid potential NullReferenceException when TargetAlias is null.

 Error MSB4018 : The "ResolvePackageAssets" task failed unexpectedly.
System.NullReferenceException: Object reference not set to an instance of an object.
   at NuGet.ProjectModel.LockFile.<>c__DisplayClass39_0.<GetTarget>b__0(LockFileTarget t)
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at NuGet.ProjectModel.LockFile.GetTarget(String frameworkAlias, String runtimeIdentifier)
   at Microsoft.NET.Build.Tasks.LockFileExtensions.GetTargetAndReturnNullIfNotFound(LockFile lockFile, String
frameworkAlias, String runtimeIdentifier)
   at Microsoft.NET.Build.Tasks.LockFileExtensions.GetTargetAndThrowIfNotFound(LockFile lockFile, String
frameworkAlias, String runtimeIdentifier)
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheWriter..ctor(ResolvePackageAssets task)
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheReader.CreateReaderFromDisk(ResolvePackageAssets task, Byte[]
 settingsHash)
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheReader..ctor(ResolvePackageAssets task)
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.ReadItemGroups()
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.ExecuteCore()
   at Microsoft.NET.Build.Tasks.TaskBase.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(TaskExecutionHost taskExecutionHost,
TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)

Bug

Fixes:

Description

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc.

Replace the instance Equals call with string.Equals(t.TargetAlias, frameworkAlias, StringComparison.Ordinal) to make the comparison explicit and culture-independent and to avoid potential NullReferenceException when TargetAlias is null.
@SimonCropp
SimonCropp requested a review from a team as a code owner March 23, 2026 00:24
@SimonCropp
SimonCropp requested review from jeffkl and nkolev92 March 23, 2026 00:24
@dotnet-policy-service dotnet-policy-service Bot added the Community PRs created by someone not in the NuGet team label Mar 23, 2026
@SimonCropp

Copy link
Copy Markdown
Contributor Author

first commit added the test. second fixes it.

@nkolev92

Copy link
Copy Markdown
Member

@SimonCropp Thanks for creating this fix.

Do you have the real world scenario in which this happens?
I'm looking for the project file & toolset being used.

I think the alias should be hydrated by the reader, so it's making me thing I'm missing something.
This would imply

if (lockFile.Version == LockFileFormat.LegacyVersion)
{
// Populate the alias at read time. This allows readers to use the alias to find targets regardless of what the underlying assets file format is.
foreach (var target in lockFile.Targets.NoAllocEnumerate())
{
target.TargetAlias = lockFile.PackageSpec?.GetTargetFramework(target.TargetFramework)?.TargetAlias;
}
}
is basically returning null for some reason (I know there's null checks there, but PackageSpec is always a thing in actual assets files).

@SimonCropp

Copy link
Copy Markdown
Contributor Author

@nkolev92 sorry, i have not been able get a nice stand alone repro. it happens when moving between cli actions and Rider.

@SimonCropp

Copy link
Copy Markdown
Contributor Author
 if (lockFile.Version == LockFileFormat.LegacyVersion) 
 { 
     // Populate the alias at read time. This allows readers to use the alias to find targets regardless of what the underlying assets file format is. 
     foreach (var target in lockFile.Targets.NoAllocEnumerate()) 
     { 
         target.TargetAlias = lockFile.PackageSpec?.GetTargetFramework(target.TargetFramework)?.TargetAlias; 
     } 
 } 

if the assumption is TargetAlias should be not null, then should we add a throw when it is null?

@nkolev92

Copy link
Copy Markdown
Member

I've been annotating nullable and targetAlias should be empty now as far as I could tell, but it's very possible that it wasn't empty in previous versions.
Throw in rider in the mix, I'm not sure what version of the reader it carries.

Let's keep the simple fix for now, but I'll look into it more.

@nkolev92 nkolev92 self-assigned this Mar 23, 2026
@SimonCropp

Copy link
Copy Markdown
Contributor Author

looks similar to dotnet/sdk#39316 ?

@nkolev92

Copy link
Copy Markdown
Member

It doesn't look like it.
That issue is 2+ years old while this codepath is less than a year old.

@nkolev92
nkolev92 merged commit b5ca9db into NuGet:dev Mar 24, 2026
16 checks passed
@SimonCropp
SimonCropp deleted the Use-Ordinal-string-comparison-for-TargetAlias branch March 24, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community PRs created by someone not in the NuGet team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants