Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't reference an executable in the "tools" folder of a Nuget package from MSBuild

I have a C# console app project which I am trying to turn into a Nuget package so that multiple projects within my team can call the executable from this project in an AfterResolveReferences step within the .csproj file.

I've created the Nuget package just fine with a .nuspec file that looks something like this:

<package ...>
  <metadata>
    ...
  </metadata>
  <files>
    <file src="bin\Release\*.*" target="tools" />
  </files>
</package>

This works and creates a Nuget package which contains my executable in the "tools" directory. The problem is the the containing folder includes the version number of the Nuget package which will change frequently. I'm trying to reference it like this in the .csproj file:

<Target Name="AfterResolveReferences">
  <Exec Command="$(SolutionDir)packages\PackageName.1.2.3\tools\AssemblyName.exe" />
</Target>

When I include PackageName.1.2.3 in the path, it works as expected but this is obviously a very brittle solution. When I just use "AssemblyName.exe" I get "The command AssemblyName.exe exited with code 9009".

There's obviously a simple standard for doing this kind of thing which I'm not familiar with - MSBuild and Nuget aren't my strongest suits, so I'd be very grateful for any advice.

What I'm actually trying to achieve here is to create a TypeScript file containing interfaces derived from C# classes defined in my model project using the TypeLite.Lib package. The TypeScript file must be created before the web project is built, as TypeScript code in the web project depends on the interfaces contained in this output. I'm open to suggestions of more elegant ways to solve this problem but I would still like to know how to solve the referencing problem anyway.

like image 788
wwarby Avatar asked Aug 09 '17 07:08

wwarby


People also ask

How do I add a reference to a NuGet package?

After you install a NuGet package, you can then make a reference to it in your code with the using <namespace> statement, where <namespace> is the name of package you're using. After you've made a reference, you can then call the package through its API.

Where is NuGet EXE located?

The latest recommended NuGet CLI is always available at https://dist.nuget.org/win-x86-commandline/latest/nuget.exe .

How do I add a NuGet reference in Visual Studio?

Install packages from NuGet.org Select Package Manager, and then copy the Install-Package command. In Visual Studio, select Tools > NuGet Package Manager > Package Manager Console to open the package manager console. Paste the command into the Package Manager Console and then press Enter.

How do I install a NuGet package .nupkg file locally?

Menu Tools → Options → Package Manager Click OK. Drop your NuGet package files in that folder. Go to your Project in Solution Explorer, right click and select "Manage NuGet Packages". Select your new package source.


1 Answers

The idea is that the NuGet package should be self-contained. That is, users should not need to add anything to the project file when using a nuget package.

NuGet packages can also contain build logic - if you put a PackageName.targets file into the a build directory, it will be automatically included into the project referencing the NuGet package. From there, you can define targets and would typically reference a tool by using $(MSBuildThisFileDirectory)..\tools\MyTool.exe.

This is important because the packages directory is only used for "classic" NuGet references via packages.config and not for the new-style PackageReference way of referencing NuGet packages, where all projects/solution share a user-level global package cache (no solution-local copies).

like image 100
Martin Ullrich Avatar answered Oct 02 '22 03:10

Martin Ullrich