visual-studionugetnuget-packagenuget-spec

How to stop NuGet packaging from including unwanted files


I'm building a NuGet package with a folder hierarchy that looks like this:

ProjectDir
    Infrastructure
        class1.cs
        class2.cs
    Task
        class3.cs
        class3.cs
    StuffToInclude
        SampleCode
            sampleclass1.cs.pp
            sampleclass2.cs.pp
        Images
            image1.gif
            image2.gif
        Doc
            text1.txt
            text2.txt
    Project.nuspec

Note the nuspec file is generated simply with the "nuget spec" command, and then the following file section is added:

<files>
    <file src="StuffToInclude\**\*.*" target="content" />
</files>

What I would expect (and want) is a content folder in the package that looks like this:

content
    SampleCode
        sampleclass1.cs.pp
        sampleclass2.cs.pp
    Images
        image1.gif
        image2.gif
    Doc
        text1.txt
        text2.txt

What I get however is a slightly larger content folder that includes a second copy of the "StuffToInclude" folder, looking like this:

content
    StuffToInclude
        Images
            image1.gif
            image2.gif
        Doc
            text1.txt
            text2.txt
    SampleCode
        sampleclass1.cs.pp
        sampleclass2.cs.pp
    Images
        image1.gif
        image2.gif
    Doc
        text1.txt
        text2.txt

Notice that the undesired StuffToInclude folder does not have the SampleCode subfolder in it -- somehow the nuget packer figured out that ".pp" files should not be placed in the content unless explicitly asked for. But all those other files (in all the other folders) are unnecessarily duplicated and it is not desirable to have them there - because when the package is consumed, that folder is also duplicated in the target project.

I thought perhaps something like this in the nuspec would help:

<files>
    <file src="StuffToInclude\**\*.*" target="content" exclude="StuffToInclude\**\*.*"/>
</files>

but all variants I have tried for the "exclude" attribute don't seem to help.

How do I exclude the "StuffToInclude" file from being included in the content folder?

Many thanks.


Solution

  • How to stop NuGet packaging from including unwanted files

    I assume the command you use when creating the package is something like nuget pack xx.csproj.

    If so, what's the special reason you created a package based on project file after creating the .nuspec file?

    According to this document, we can use nuget pack command based on the specified .nuspec or project file.

    So usually, if we create a package based on .csproj, there's no need to create a .nuspec file. Also, if we want to create a package based on .nuspec, I suggest use command nuget pack xx.nuspec instead of nuget pack xx.csproj.

    Cause of the issue:

    I've done some tests and found when we put Project.nuspec file in project dir, and use a command like nuget pack xx.csproj,then the nuget.exe will read data from both xx.nuspec and xx.csproj, that's why you have a second copy.

    1# Structure for nuget pack xx.csproj if no xx.nuspec in same directory:

    content
        StuffToInclude
            Images
                image1.gif
                image2.gif
            Doc
                text1.txt
                text2.txt
    

    2# Structure for nuget pack xx.nuspec:

    content
        SampleCode
            sampleclass1.cs.pp
            sampleclass2.cs.pp
        Images
            image1.gif
            image2.gif
        Doc
            text1.txt
            text2.txt
    

    And when you use nuget pack command based on project file(.csproj) where exists a ProjectName.nuspec, it reads data from both .csproj and .nuspec. so it results in the second copy like what you got.

    To resolve it:

    1. Keep the xx.nuspec file and use command like nuget pack xx.nuspec.(suggested)

    2. Delete the xx.nuspec file(Or delete the <Files> section) and use command like nuget pack xx.csproj, to include those xx.cs.pp files into content folder, you may need to set their build action to content.

    3. Keep both the xx.nuspec file and the xx.csproj file (see below) and everything you want in the xx.nuspec file, but to avoid the unwanted second copies of things, make sure that every single item you don't want copied twice is set to a Build Action of None.