Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to add NuGet package

I have created a NuGet package libtidy which is pushed to a Team Services feed.

When I try installing via the NuGet console I get the error

Failed to add reference to 'libtidy'

I have read this Stack Overflow post where the OP has a similar problem but I have been unable to resolve the issue - I've tried:

  • Deleting all the folders inside of the packages folder and updating
  • Executing regsvr32 "C:\Program Files (x86)\Common Files\microsoft shared\MSEnv\VsLangproj.olb" from an elevated command prompt
  • Doing Uninstall-Package libtidy -force from the package manager console (this doesn't work as the package isn't installed
  • git clean -dfx
  • clearing nuget cache
  • manually removing the libtidy directory in the .nuget directory

EDIT

Having done a little research, could this be to do with the fact that libtidy.dll is not managed code? Infact it is ANSI-C. In our application we are using TidyManaged, as a wrapper, which is managed, and is successfully installed via nuget. Currently if we manually copy in libtidy.dll into bin, it works fine, but it would be better if the build process pulled in libtidy.dll, perhaps as part of the Tidymanaged install, which it does not at present.

EDIT2

param($installPath, $toolsPath, $package, $project)

$file = $project.ProjectItems.Item("libtidy.dll");

If ($file -eq $null)
{
     $project.ProjectItems.AddFromFile("libtidy.dll");
     $file = $project.ProjectItems.Item("libtidy.dll");
}

$file.Properties.Item("CopyToOutputDirectory").Value = [int]1;

EDIT3

Edit 3:

I am

a) placing libtidy.dll and Install.ps1 in a directory called nuget-libtidy in the manifest generated by nuget spec

I have:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>nuget-libtidy</id>
    <version>1.0.0</version>
    <authors>Name</authors>
    <owners>Name</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>nuget-libtidy</description>
    <copyright>Copyright 2016</copyright>
    <tags>nuget-libtidy</tags>
  </metadata>
  <files>
      <file src="libtidy.dll" target="content" />
      <file src="Install.ps1" target="tools" />
  </files>
</package>

When I run nuget pack I get the following warning:

WARNING: 1 issue(s) found with package 'nuget-libtidy2'.

Issue: Assembly outside lib folder.
Description: The assembly 'content\libtidy.dll' is not inside the 'lib' folder and hence it won't be added as reference when the package is installed into a project.
Solution: Move it into the 'lib' folder if it should be referenced.

b) When we build the application libtidy.dll is placed in the root of the project (not the bin) & get the following error in the output window:

Added package 'nuget-libtidy' to 'packages.config'
Executing script file <path>\nuget-libtidy\tools\Install.ps1'...
Cannot add a link to the file libtidy.dll. There is already a file of the same name in this folder.
At <path>\nuget-libtidy\tools\Install.ps1:7 char:5
+     $project.ProjectItems.AddFromFile("libtidy.dll");
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Successfully installed 'nuget-libtidy' to <Namepace>
like image 317
SamJolly Avatar asked May 17 '16 10:05

SamJolly


1 Answers

You are probably getting this

Failed to add reference to 'libtidy'

because you have libtidy somewhere inside the lib folder. Installing a packag in this folder will automatically have a reference added to it, and you can't add a reference to an unmanaged library directly.

If you are not already, you should look into manually creating your nuget package rather than doing it by project or assembly. To do this:

  1. Create a folder called nuget-libtidy
  2. Add libtidy.dll, TidyManaged.dll and anything else you need into this folder
  3. Open cmd and navigate to the folder you just created
  4. Run nuget spec to create the default manifest Package.nuspec (you will need to have nuget added to your PATH environment variable
  5. Add the following xml to the nuspec file just created right after the </metadata> closing tag

    <files>
        <file src="libtidy.dll" target="content" />
        <file src="TidyManaged.dll" target="lib" />
    </files>
    
  6. Adjust any of the other values you need.

You could call it done and pack and deploy the nuget package. You need libtidy.dll to be copied to the output directory. This means after you install the package, you would have to navigate to right-click on dependencies\libtidy.dll in visual studio, select properties and set Copy to Output Directory to Copy Always.

If you don't want everyone to have to do this you could make a few more adjustments to you nuget-libtidy folder and the manifest file. Basically you need to do is create an Install.ps1 file that adds

<ItemGroup>
    <Content Include="libtidy.dll">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>

to the project file of the installed project.

Here is an example of Install.ps1 that should do what you want:

param($installPath, $toolsPath, $package, $project)

$file = $project.ProjectItems.Item("libtidy.dll");

If ($file -eq $null)
{
    $project.ProjectItems.AddFromFile("libtidy.dll");
    $file = $project.ProjectItems.Item("libtidy.dll");
}

$file.Properties.Item("CopyToOutputDirectory").Value = [int]1;

Once your PowerShell script is done add another line to you manifest file:

<file src="Install.ps1" target="tools" />

Don't forget running scripts on Windows 10 requires you to set the execution policy. I would suggest running Set-ExecutionPolicy RemoteSigned. After you run this in PowerShell, you will have to reboot your system.

At this point, you should be able to pack and deploy.

EDIT

Typo found in Install.ps1 file. Line 3, $file1 = $project.ProjectItems.Item("libtidy.dll"); should be $file = $project.ProjectItems.Item("libtidy.dll";

like image 176
Brandon Griffin Avatar answered Oct 10 '22 13:10

Brandon Griffin