Failed to add NuGet package
Asked Answered
A

3

6

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>
Athalia answered 17/5, 2016 at 10:42 Comment(1)
please provide your vs version and what is the service pack?Bridie
S
6

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";

Sternum answered 26/5, 2016 at 14:54 Comment(12)
Thank you for this very comprehensive answer. Will give it a try.Athalia
We have found that it is copying "libtidy.dll" to the root directory rather than to the "bin" folder. Any ideas?Athalia
The install.ps1 file that goes in the package under the tools folder should have set out to to copy the file to the bin on each build. Can you check the properties for libtidy.dll and let me know what the value is for 'copy to output directory'Sternum
It is set to "Do not copy". Thanks for help thus far.Athalia
The above may have confused. The above relates to the original dll is the original solution where it worked. I have added my install.ps1 code in EDIT2 aboveAthalia
Your install.ps1 looks good. There are a couple of things that I can think of that would be preventing it from setting the 'copy to output directory' to 'Copy Always'. 1) the manifest file is missing the line to copy the install.ps1 to the tools folder. 2) install.ps1 is looking for libtidy.dll in the root directory, but it is somewhere else.Sternum
I have added EDIT3 which provides more information as we are still get the error. Thank you again for your help.Athalia
Couple of things. I was getting the same error you are describing in EDIT3, but fixed it. I had a typo, in the install.ps1 file. Funny thing is, your install.ps1 file that you show in EDIT2, seems to already have the fix that I had. But, please double-check to make sure your install.ps1 file looks exactly like my EDIT. Regarding your note in EDIT3, the libtidy.dll should be installed to the root directory. It will be copied to bin when you build your project. An edit you should make too, is to remove the `<dependencies>' tag in your manifest file.Sternum
I'll post a github of an example I know works and provide you with the link for you to review.Sternum
Thanks, I had spotted the typo in install.ps1. I have now removed the dependency element from EDIT 3. I will wait for your example. I really appreciate your help.Athalia
Link to github: github.com/bgriffin4750/unipay-sdk-nuget. Hope this helps, I tried this out on a few PC's to make sure it was doing everything.Sternum
We did the steps after step 6 with a .targets file rather than PowerShell, which I think is simpler. Put this in a .targets file: <?xml version="1.0" encoding="utf-8"?> <Project ToolVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <Content Include="libtidy.dll"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content> </ItemGroup> </Project> And then add this line to your .nuspec file: <file src="bin\Release\MyTargetsFile.targets" target="build"/>Veratridine
I
0

I once had an issue similar where I had to be running VS with elevated permission to install a nuget package. Something to do with our corporate security settings.

Inexpressive answered 25/5, 2016 at 23:20 Comment(0)
N
0

I just had this issue but with the message "Failed to add reference to cmclientlib" and changing the default package management format worked for me.

Go to Tools > Options > NuGet Package Manager > General > Package Management. Then change the default package management format to Package Reference and that cleared up all the issues.

Newsmagazine answered 6/6 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.