How can i create a (design time only) nuget package that doesn't add an assembly reference when installed?

774 views Asked by At

I want to build a nuget package for a design time only package. This package contains resources (a ruleset file, a build props file and an assembly) for using a custom code analysis rule set.

My nuspec file (inside my CodeAnalysis assembly project) looks like this:

<package>
    <metadata>
        <id>$id$</id>
        <version>$version$</version>
        <authors>$author$</authors>
        <description>$description$</description>
        <references>
        </references>
        <developmentDependency>true</developmentDependency>
    </metadata>
    <files>
        <!-- copy rules assembly to tools folder -->
        <file src="bin\Release\CodeAnalysis.dll" target="tools\CodeAnalysis.dll" />
        <file src="tools\CodeAnalysis.ruleset" target="tools\CodeAnalysis.ruleset" />
        <file src="build\CodeAnalysis.props" target="build\CodeAnalysis.props" />
    </files>
</package>

When in build this package, the CodeAnalysis.dll assembly is present in \lib\net40\CodeAnalysis.dll, and a copy is added in \tools\CodeAnalysis.dll.

When i use this package in another project, the default behavior is that a reference to the assembly is added to the project. Since this a a design / build time only assembly (only used when executing code analysis on the project), i don't want nuget to add this reference.

I've looked into the <references> section of the nuspec file, but that doesn't give me a clue how to prevent a reference from being made.

How do i build a nuspec file so that nuget does not add a project reference when installing the nuget package?

2

There are 2 answers

3
Alex Wiese On

Pass the -tool command line switch to nuget. This will not include the DLL as a referenced assembly.

nuget pack MyProject.csproj -tool
0
Owain Williams On

Take a look at the ConditionalAttribute class in the System.Diagnostics namespace. With this you can specify a conditional compilation symbol that if isn't defined the target of the [Conditional] attribute isn't compiled into the resulting assembly.

This way you could define a CODE_ANALYSIS symbol (or whatever name you choose) and decorate all the types, methods, properties, etc. in your project with the [Conditional("CODE_ANALYSIS")] attribute. If people using your package haven't also defined this symbol then their resulting compiled assembly will not include a reference to your project, but they will be able to benefit from it at design time.

This is what JetBrains use for their Annotations NuGet package, there's a blog post about it here:

http://blog.jetbrains.com/dotnet/2015/08/12/how-to-use-jetbrains-annotations-to-improve-resharper-inspections/