I am aware there are multiple questions on this topic already, but they all seem outdated. To clarify, I am using the "new" VSIX manifest format, and trying to follow the official instructions here: https://learn.microsoft.com/en-us/nuget/visual-studio-extensibility/visual-studio-templates

I have one project template and a couple of item templates that go with it. They all depend on deploying a NuGet package that should come bundled locally with the VSIX. I have examined the resulting VSIX file and all the files seem to be in the right place:

  1. The project template has the required XML for declaring which packages to install:
  <WizardExtension>
    <Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
    <FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName>
  </WizardExtension>
  <WizardData>
    <packages repository="extension" repositoryId="VsixID.etc.etc">
      <package id="Rx-Linq" version="2.2.5" />
    </packages>
  </WizardData>
  1. The repositoryID matches the ID attribute in the .vsixmanifest file.

  2. There is an individual Asset entry for each package, with the form:

    <Asset Type="Rx-Linq.2.2.5.nupkg" d:Source="File" Path="Packages\Rx-Linq.2.2.5.nupkg" d:VsixSubPath="Packages" />
  1. I have removed all packages.config and all the package references from the .csproj file installed by the VSIX (and even from the VSIX project itself just for good measure).

  2. I have inspected the output VSIX and there is indeed a Packages folder in the VSIX containing all the .nupkg files. This folder is indeed unpacked and copied into the Visual Studio Extensions folder.

Despite all this, when I create a new project with the template, VS displays an error message saying: Failed to restore package from C:\users\<pathtoextensions>\Packages.

The thing is, the .nupkg files are actually present in the exact folder that the error message refers to.

I have been searching this for days and I can't seem to find any reference to best practices that actually work. It seems like these VSIX manifests are geared towards the legacy packages.config way of doing things, and there are discussions about how to extend them to use PackageReference instead.

Can anyone give any advice at all at how we are supposed to proceed going forward? Are packages not supposed to be deployed with the VSIX anymore? Are we supposed to just fill in the project with PackageReference entries and just let the user resolve them manually?

I feel like I am missing something fundamental here and any insight would be extremely valuable.

Update: I have also opened an issue on the NuGet github repository, as this is clearly a problem with the PackageRestore feature when restoring packages stored in a VSIX installer. Everything else mentioned in this question is working as intended and expected, except the package restore.

1

There are 1 answers

1
Mr Qian On

How do you actually include NuGet packages in Visual Studio Project Templates VSIX targeting Visual Studio 2019?

Actually, there is no way to specify in a VS project template project that nuget packages can be used both using packages.config and PackageReference. Only two project templates of nuget management types can be created separately.

I have an easy way and since you have some issues with PackageReference format, you can try this funtion:

PackageReference

1) add these reference node in projecttemplate.csporj file:

enter image description here

   <ItemGroup>
    <PackageReference Include="Rx-Linq">
      <Version>2.2.5</Version>
    </PackageReference>
  </ItemGroup>

2) When you create a project by this project template, please check these two options and VS will automatically read xxx.csproj and then recover the corresponding nuget package based on the information in it during build process.

enter image description here

Note: also make sure that the nuget url is checked and can be access under Package Source.

packages.config

In additon, for packages.config, you can just create a file named packages.config and then add your nuget info into it:

1)

2) add these into projecttemplate.csproj file:

<ItemGroup>
  <Content Include="packages.config">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
 </ItemGroup>
<ItemGroup>
    <Reference Include="Rx-Linq, Version=2.2.5, Culture=neutral, PublicKeyToken=eb42632606e9261f, processorArchitecture=MSIL">
      <HintPath>..\packages\Rx-Linq.2.2.5\lib\net472\xxxxxxx.dll</HintPath>
    </Reference>
</ItemGroup>

Note: if this nuget package has dependencies, you should also add them(above steps) into packages.config and xxxx.csproj file. This funcution is a little more complicated than yours but it works. So, I recommend that you use PackageReference format.

More info you can refer to this similar issue.