project-system icon indicating copy to clipboard operation
project-system copied to clipboard

Incorrect restore in Visual Studio when a dependency to a C# project is flowed through a C++/CLI project

Open joeltankam opened this issue 2 years ago • 1 comments

Visual Studio 2022 17.3 Preview

Summary

The project.assets.json file is incomplete for a C# project when it transitively depends on another C# project through a C++/CLI project. This problem seems to only occur in Visual Studio 2022 17.3 (tested with Preview 4.0 to 6.0) and not previous versions.

Lets say we have three projects: ProjectA (C#), ProjectB (C++/CLI) and ProjectC (C#); so that ProjectA references ProjectB which references ProjectC:

ProjectA (C#) --(depends on)--> ProjectB (C++/CLI) --> ProjectC (C#)

If the ProjectReference from ProjectB to ProjectC does not contain the metadata Project (e.g. <Project>{e5599aee-f39e-4613-b48d-7824eca526e5}</Project>), MSBuild (through command line) and Visual Studio restore won't behave the same way. Typically, the transitive reference to ProjectC is missing from ProjectA's asset file when performing NuGet restore in Visual Studio:

  (...)
  "targets": {
    ".NETFramework,Version=v4.8": {
      "ProjectB/1.0.0": {
        "type": "project",
-       "dependencies": {
-         "ProjectC": "1.0.0"
-       },
        "compile": {
          "bin/placeholder/ProjectB.dll": {}
        },
        "runtime": {
          "bin/placeholder/ProjectB.dll": {}
        }
      },
-     "ProjectC/1.0.0": {
-       "type": "project",
-       "framework": ".NETFramework,Version=v4.8",
-       "compile": {
-         "bin/placeholder/ProjectC.dll": {}
-       },
-       "runtime": {
-         "bin/placeholder/ProjectC.dll": {}
-       }
-     }
    }
  }
  (...)

Steps to Reproduce

  1. Create three projects as described in the summary

  2. Remove <Project> metadata in the reference from ProjectB to ProjectC:

    -    <ProjectReference Include="..\ProjectC\ProjectC.csproj">
    -      <Project>{e5599aee-f39e-4613-b48d-7824eca526e5}</Project>
    -    </ProjectReference>
    +    <ProjectReference Include="..\ProjectC\ProjectC.csproj" />
    
  3. Perform a restore on ProjectA using MSBuild and check the contain of .\ProjectA\obj\project.assets.json: msbuild -v:m -t:Restore .\ProjectA\ProjectA.csproj

  4. Open the solution in Visual Studio 2022 17.3 Preview, force a restore if needed, and check the content of .\ProjectA\obj\project.assets.json again; you will notice the changes mentioned in the summary

See this repository: joeltankam/restore-cli-sample

Expected Behavior

MSBuild CLI and Visual Studio should have the same behavior, which means the restore should not be performed when opening the solution in Visual Studio. Package Manage log should be:

All packages are already installed and there is nothing to restore.

Actual Behavior

The restore is performed again in Visual Studio for ProjectA:

Restored (...)\ProjectA\ProjectA.csproj (in 0.9 ms). NuGet package restore finished.

Changing .\ProjectA\obj\project.assets.json as described in the summary; creating incremental compilation issues when switching from command-line to VS (e.g. some projects are unnecessarily re-restored and recompiled).

User Impact

We have a 950+ projects repository (~85% C#, ~10% C++, ~5% C++/CLI), with an average solution size of around 280 projects.

When switching the .NET SDK project file format, we removed <Project> metadata on all project references, including C++ projects. This used to work perfectly until Visual Studio 2022 17.3. Now that it does not, it has some significant impacts considering the size of our repository and solutions (it's standard practice for us first to compile the whole repository through command-line, and then open a specific solution in Visual Studio, thus the issue).

On a side note, the Project metadata is supposed to be the ProjectGuid property of the referenced project; where does that value come from in this scenario since C# projects don't have that property anymore?

joeltankam avatar Aug 04 '22 16:08 joeltankam

Any update on this please ?

joeltankam avatar Aug 29 '22 14:08 joeltankam

Any update on this please ?

joeltankam avatar Oct 10 '22 13:10 joeltankam

On a side note, the Project metadata is supposed to be the ProjectGuid property of the referenced project; where does that value come from in this scenario since C# projects don't have that property anymore?

Can you please give an answer to this ? @kvenkatrajan @drewnoakes

joeltankam avatar Oct 18 '22 13:10 joeltankam

cc: @nkolev92 It seems that this belong to the NuGet team

ocallesp avatar Oct 18 '22 16:10 ocallesp

Moved to https://github.com/NuGet/Home/issues/12170

ocallesp avatar Oct 18 '22 16:10 ocallesp