nanoframework / Home

:house: The landing page for .NET nanoFramework repositories.
https://www.nanoframework.net
MIT License
861 stars 79 forks source link

MSBuild props/targets from NuGet package are not imported into a nanoFramework project #1544

Open frobijn opened 2 days ago

frobijn commented 2 days ago

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:

The zip file contains two projects:

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:

Expected behaviour

The expected situation can be viewed:

Other project types behave as expected, e.g.,:

As a demonstration of the workaround:

Screenshots

No response

Aditional context

All latest versions: Visual Studio 2022, nanoFramework extension.

josesimoes commented 1 day ago

I haven't tried it myself but I'm suspecting this is because our project extension (nfproj) differs from the standard one (csproj) and the Visual Studio tools can't handle it when processing the ad slash removal off the props and targets.

Our project system doesn't support PackageReference, correct.

As you've mentioned, the work around is to use the infamous import PowerShell script. I personally don't like that approach very much because that's a security risk (that's one of the reasons for this being removed/replaced in the current .NET project system).

I suggest that we wait for the reply on the question you've raised in Developer community portal before making a decision. Because, if this is a bug in the tools, we have nothing fix at our end (as I suspect it's the case).

frobijn commented 1 day ago

Yes, let's hope that I get a reaction from Microsoft. I hope they can tell what aspect of the nanoFramework project Visual Studio is using to prevent the addition/removal of imports. The NuGet code seems to work fine - I repurposed one of their unit tests and debugged it, and that code adds the imports conform spec. So I suspect that Visual Studio post-processes the XML produced by the NuGet code and then ignores the added imports.

The reason I investigated a workaround is that this type of package is really very useful to add tools to a project, e.g., a test adapter, MSBuild task for version checking, debugging unit tests and more. There may be PRs in the near future that refer to this issue and propose to use the workaround while waiting two years (2022 to 2024 for icons, isn't it? ;-)) for Microsoft to implement a fix.

josesimoes commented 1 day ago

It really depends on the traction it gets from the developer base. And also if it's something that's open source or not. If that's something in our project system, we probably can fix it. If it's with nuget also fixable (I've done the implementation of the update for nfproj myself and worked with them to get a TFM for nanoFramework). Now if it's with VS, we'll probably be seeing a long turn around, like the icons. If any at all, in case this is with the old project system which isn't getting any improvements now... Let's patiently wait for developments.