Visual Studio 2022 does not restore nuget packages for .NetFramework with PackageReference

441 views Asked by At

I used the tool in VS2022 to convert a .NetFramework project to use PackageReference instead of packages.config.

The upgrade worked fine but I can't get VS2022 to automatically restore the packages. Every time I do a clean build, i.e. delete the asset file in the obj/ folder, I need to manually run nuget restore or the project will not compile.

Once the asset file "project.asset.json" is present the project compiles and works fine.

Is there a way to troubleshoot this?

What msbuild task is supposed to create the asset file? In the detailed log I only see references to it in:

Target "ResolveNuGetPackageAssets" skipped, due to false condition; ('$(ResolveNuGetPackages)' == 'true' and exists('$(ProjectLockFile)')) was evaluated as ('true' == 'true' and exists('obj\project.assets.json')).

1

There are 1 answers

0
adrianm On BEST ANSWER

Found the problem in some confusing documentation.

There are two settings in NuGet.Config

<packageRestore>
  <add key="enabled" value="True" />
  <add key="automatic" value="False" />
</packageRestore>

The documentation says

enabled: A Boolean indicating whether NuGet can perform automatic restore. You can also set the EnableNuGetPackageRestore environment variable with a value of True instead of setting this key in the config file.

automatic: A Boolean indicating whether NuGet should check for missing packages during a build.

I interpreted that as enabled=true would automatically do a nuget restore but that is not how it works. Both settings must be true for .NetFramework projects. SDK projects work fine with automatic=false. Maybe dotnet restore ignores this nuget setting.

(I have no idea what "enable" actually does or what "missing packages" mean in this context)

My guess is I probably set automatic to false myself at one point when I was offline and assumed it would not try online sources