Description
Tool
Visual Studio extension
Description
If a NuGet package contains files build\<package_id>.props
and/or build\<package_id>.targets
, the Visual Studio 2022 package manager is supposed to update the project file. If the type of project supports PackageReference
, a package reference is added. If the project file is a .NET Framework-type MSBuild file (like the nanoFramework projects), an Import
element is added to import the .props and/or .targets file that points to the files in the location of the (unpacked) NuGet package.
... but not for a nanoFramework project! All other files in a NuGet package are handled as expected, even other special file types (e.g., a NuGet package with only content files). It gets even weirder. The code of the NuGet client used by Visual Studio package managers can be found on github, and nothing in the code suggests that the imports are not supported for nanoFramework project files. That suggests that when Visual Studio saves the project file's XML as created by the package manager code (that seems to contain the imports), the imports are not properly saved but other changes are. I've asked Microsoft whether that is true and what the cause is, but that is still under investigation and may be a hard problem.
One possible cause is that one of the project (type) capabilities is required for the imports to work, and that capability is missing. I couldn't find one in the documentation, and without access to the Visual Studio source code it is hard to tell.
Even if this is fixed, it is not clear whether the fix will be applied to both Visual Studio 2019 and 2022.
There is a workaround: an install.ps1 can be added that add the imports to the nanoFramework project. The reverse (uninstall removes the imports) does not seem to work properly. I'm curious whether the core team would accept this workaround in future contributions.
How to reproduce
Required files are in this file: NuGetBuildpackage.zip
The zip file contains three packages:
- A
BuildPackage.1.0.0.nupkg
that contains a .props and .targets file. - A
BuildPackage_workaround.1.0.0.nupkg
that contains a .props and .targets file and workaround (install.ps1 and uninstall.ps1). - A
ContentPackage.1.0.0.nupkg
that contains only content files.
The zip file contains two projects:
RegularProject.Template
is a .NET Framework class library project with solutionTestProject.Template
is a nanoFramework class library project with solution
The templates are configured that the three packages can easily been installed via the NuGet package manager in Visual Studio, without affecting any other NuGet package storage locations.
The issue can be demonstrated:
- Copy/rename the
TestProject.Template
directory, open the solution in that directory and install theBuildPackage.1.0.0.nupkg
- The nanoFramework project does not have imports for the .props and .targets file.
Expected behaviour
The expected situation can be viewed:
- Copy/rename the
RegularProject.Template
directory, open the solution in that directory and install theBuildPackage.1.0.0.nupkg
- The .NET Framework project does have imports for the .props and .targets file.
Other project types behave as expected, e.g.,:
- Copy/rename the
TestProject.Template
directory, open the solution in that directory and install theContentPackage.1.0.0.nupkg
- Some content files are added to the nanoFramework project conform the NuGet specs.
As a demonstration of the workaround:
- Copy/rename the
TestProject.Template
directory, open the solution in that directory and install theBuildPackage_workaround.1.0.0.nupkg
- The nanoFramework project now does have imports for the .props and .targets file.
- Uninstall the package
- The imports are not removed, not even by uninstall.ps1, but there is a correct project file available in the directory. That file is removed if ( newer version of) the package is installed.
Screenshots
No response
Aditional context
All latest versions: Visual Studio 2022, nanoFramework extension.
Reported here NuGet/Home#1850.