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

NullReferenceException in PackagesLockFileUtilities.HasDirectPackageDependencyChanged (NuGet.ProjectModel) #14164

Open
fowl2 opened this issue Mar 6, 2025 · 2 comments
Labels
Area:RestoreCPM Central package management Area:RestoreRepeatableBuild The lock file features Functionality:Restore Type:Bug WaitingForClientTeam Customer replied, needs attention from client team. Do not apply this label manually.

Comments

@fowl2
Copy link

fowl2 commented Mar 6, 2025

NuGet Product Used

Package restore as part of build in Visual Studio.

Product Version

Visual Studio 2022 Version 17.13.2

Repro Steps & Context

Used the .Net Upgrade Assistant to convert my solution to centralised package management.

Now I get this error in the Output when building:

NuGet package restore failed. Please see Error List window for detailed warnings and errors.
Error occurred while restoring NuGet packages: Object reference not set to an instance of an object.

The only errors are

NU1008: Projects that use central package version management should not define the version on the PackageReference items but on the PackageVersion items.

(There are no versions on <PackageReference>s)

I debugged the relevant msbuild and found the exception was coming from:

https://github.com/NuGet/NuGet.Client/blob/4fd7b8a94c44e72ba3ad9918e36bcb75d2de4f51/src/NuGet.Core/NuGet.ProjectModel/ProjectLockFile/PackagesLockFileUtilities.cs#L413C119-L413C195

It seems like e.LibraryRange must be null somehow.

The packages.lock.json for the project is empty:

{
  "version": 2,
  "dependencies": {
    ".NETFramework,Version=v4.6.2": {},
    "net8.0": {}
  }
}

See stack trace:

Verbose Logs

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=NuGet.ProjectModel
  StackTrace:

NuGet.ProjectModel.dll!NuGet.ProjectModel.PackagesLockFileUtilities.HasDirectPackageDependencyChanged.AnonymousMethod__6_3(NuGet.LibraryModel.LibraryDependency e) Line 298
	at NuGet.ProjectModel\PackagesLockFileUtilities.cs(298)
System.Core.dll!System.Linq.Enumerable.WhereSelectEnumerableIterator<NuGet.LibraryModel.LibraryDependency, string>.MoveNext()
System.Core.dll!System.Linq.Buffer<string>.Buffer(System.Collections.Generic.IEnumerable<string> source)
System.Core.dll!System.Linq.OrderedEnumerable<string>.GetEnumerator()
mscorlib.dll!string.Join(string separator, System.Collections.Generic.IEnumerable<string> values) Line 194
	at System\String.cs(194)
NuGet.ProjectModel.dll!NuGet.ProjectModel.PackagesLockFileUtilities.HasDirectPackageDependencyChanged(System.Collections.Generic.IEnumerable<NuGet.LibraryModel.LibraryDependency> newDependencies, System.Collections.Generic.IEnumerable<NuGet.ProjectModel.LockFileDependency> lockFileDependencies, NuGet.Frameworks.NuGetFramework nuGetFramework) Line 302
	at NuGet.ProjectModel\PackagesLockFileUtilities.cs(302)
NuGet.ProjectModel.dll!NuGet.ProjectModel.PackagesLockFileUtilities.IsLockFileValid(NuGet.ProjectModel.DependencyGraphSpec dgSpec, NuGet.ProjectModel.PackagesLockFile nuGetLockFile) Line 129
	at NuGet.ProjectModel\PackagesLockFileUtilities.cs(129)
NuGet.Commands.dll!NuGet.Commands.RestoreCommand.EvaluatePackagesLockFileAsync(string packagesLockFilePath, NuGet.DependencyResolver.RemoteWalkContext contextForProject, NuGet.Common.TelemetryActivity lockFileTelemetry) Line 903
	at D:\a\_work\1\s\src\NuGet.Core\NuGet.Commands\RestoreCommand\RestoreCommand.cs(903)
NuGet.Commands.dll!NuGet.Commands.RestoreCommand.EvaluateLockFile(NuGet.Common.TelemetryActivity telemetry, NuGet.DependencyResolver.RemoteWalkContext contextForProject, string packagesLockFilePath, NuGet.ProjectModel.PackagesLockFile packagesLockFile, System.Threading.CancellationToken token) Line 450
	at D:\a\_work\1\s\src\NuGet.Core\NuGet.Commands\RestoreCommand\RestoreCommand.cs(450)
NuGet.Commands.dll!NuGet.Commands.RestoreCommand.ExecuteAsync(System.Threading.CancellationToken token) Line 226
	at D:\a\_work\1\s\src\NuGet.Core\NuGet.Commands\RestoreCommand\RestoreCommand.cs(226)
NuGet.Commands.dll!NuGet.Commands.RestoreRunner.ExecuteAsync(NuGet.Commands.RestoreSummaryRequest summaryRequest, System.Threading.CancellationToken token) Line 249
	at D:\a\_work\1\s\src\NuGet.Core\NuGet.Commands\RestoreCommand\RestoreRunner.cs(249)
NuGet.Commands.dll!NuGet.Commands.RestoreRunner.ExecuteAndCommitAsync(NuGet.Commands.RestoreSummaryRequest summaryRequest, NuGet.Commands.IRestoreProgressReporter progressReporter, System.Threading.CancellationToken token) Line 232
	at D:\a\_work\1\s\src\NuGet.Core\NuGet.Commands\RestoreCommand\RestoreRunner.cs(232)
mscorlib.dll!System.Threading.Tasks.Task<System.Threading.Tasks.Task<NuGet.Commands.RestoreSummary>>.InnerInvoke() Line 373
	at System.Threading.Tasks\Task.cs(373)
mscorlib.dll!System.Threading.Tasks.Task.Execute() Line 2514
	at System.Threading.Tasks\Task.cs(2514)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 515
	at System.Threading\ExecutionContext.cs(515)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 486
	at System.Threading\ExecutionContext.cs(486)
mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot) Line 2684
	at System.Threading.Tasks\Task.cs(2684)
mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution) Line 2630
	at System.Threading.Tasks\Task.cs(2630)
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() Line 569
	at System.Threading\ThreadPoolWorkQueue.cs(569)
@kartheekp-ms
Copy link
Contributor

@fowl2 Would it be possible to upload a sample solution or provide a GitHub link to a sample project so that we can reproduce the issue?

@kartheekp-ms kartheekp-ms added Functionality:Restore Area:RestoreRepeatableBuild The lock file features Area:RestoreCPM Central package management WaitingForCustomer Applied when a NuGet triage person needs more info from the OP and removed Triage:Untriaged labels Mar 6, 2025
@fowl2
Copy link
Author

fowl2 commented Mar 7, 2025

ok I've narrowed it down to the combination of

  1. MultiTargeting (even with only 1 target)
  2. RestorePackagesWithLockFile (even if no lock file is present
  3. CPM enabled
  4. A PackageReference with a missing PackageVersion

With only 1 project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net8.0</TargetFrameworks>
    <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.CrmSdk.CoreAssemblies" />
  </ItemGroup>
</Project>

Build output:

Build started at 11:35 AM...
1>------ Build started: Project: NuGetRepro, Configuration: Debug Any CPU ------
NuGet package restore failed. Please see Error List window for detailed warnings and errors.
Error occurred while restoring NuGet packages: Object reference not set to an instance of an object.
1>C:\Program Files\dotnet\sdk\9.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(266,5): error NETSDK1004: Assets file 'REDACTED\nugetRepro\obj\project.assets.json' not found. Run a NuGet package restore to generate this file.
1>Done building project "NuGetRepro.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 11:35 AM and took 00.477 seconds ==========

edit: I guess this means desired solution here would be the correct "NU1010 The PackageReference items PackageId do not have corresponding PackageVersion.". error. I should make a bug on the .net upgrade assistant about the missing PackageVersion.

@microsoft-github-policy-service microsoft-github-policy-service bot added WaitingForClientTeam Customer replied, needs attention from client team. Do not apply this label manually. and removed WaitingForCustomer Applied when a NuGet triage person needs more info from the OP labels Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area:RestoreCPM Central package management Area:RestoreRepeatableBuild The lock file features Functionality:Restore Type:Bug WaitingForClientTeam Customer replied, needs attention from client team. Do not apply this label manually.
Projects
None yet
Development

No branches or pull requests

2 participants