Skip to content

[Bug]: Static Graph Restore failed with a NullReferenceException when a non-SDK-style project refs an SDK-style project with SetTargetFramework #11680

Open
@dfederm

Description

@dfederm

NuGet Product Used

MSBuild.exe

Product Version

msbuild 17

Worked before?

No

Impact

It's more difficult to complete my work

Repro Steps & Context

Dep\Dep.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net472;net5.0</TargetFrameworks>
  </PropertyGroup>
</Project>

Broken\Broken.csproj:

<Project>
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
  <PropertyGroup>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\Dep\Dep.csproj">
      <SetTargetFramework>TargetFramework=net472</SetTargetFramework>
    </ProjectReference>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Do a static graph restore on Broken\Broken.csproj. The output looks like:

Microsoft (R) Build Engine version 17.2.0-dev-22166-01+719247ede for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
C:\Program Files\Microsoft Visual Studio\2022\IntPreview\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.RestoreEx.targets(19,5): error : Object reference not set to an instance of an object. [C:\Code\tmp\GraphRestoreRepro\Broken\Broken.csproj]

Build FAILED.

C:\Program Files\Microsoft Visual Studio\2022\IntPreview\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.RestoreEx.targets(19,5): error : Object reference not set to an instance of an object. [C:\Code\tmp\GraphRestoreRepro\Broken\Broken.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:06.70

The NRE specifically is in MSBuildStaticGraphRestore.GetPackageSpec. The caller iterates each ProjectWithInnerNodes and calls GetPackageSpec(project.OuterProject), but project.OuterProject is null for the Dep\Dep.csproj entry.

If any of the following is changed, it works:

  1. Non-static graph restore
  2. Remove the SetTargetFramework metadata from Broken\Broken.csproj
  3. Migrate Broken\Broken.csproj to SDK-style

Verbose Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions