3

I am trying to generate NuGet packages for a pair of internal C# projects (eventually more, but starting with these two) that are used in other solutions/projects/etc. Both of these internal C# projects have references to other NuGet packages (Enterprise Library Logging in particular). I am using the feature of Nuget that allows it to build the packages from the CSPROJ file to simplify my life a little bit. The problem is that the Nuget.Exe tool does not identify the packages that the project references as dependencies. To give a full layout of the solution:

Solution:
   Project 1
       Refs to some NuGet packages off the global repository (Enterprise Library, etc etc).
       (has a packages.config file that explicity lists them all)
   Project 2
       Refs to some NuGet packages off the global repository (Enterprise Library, etc. etc).
       Project ref to Project 1
       (has a packages.config file that explicity lists them all)

The command line I use is:

   nuget pack Project2.csproj -IncludeReferencedProjects -verbosity detailed

The relevent parts of the output of the command line are:

Found packages.config. Using packages listed as dependencies

Id: Project2
Version: 1.0.0.0
Authors: abc
Description: abc
Dependencies: Project1 (= 1.0.0.0)

Added file 'lib\net35\project2.dll'.

I would have expected "Dependencies: Project1, Enterprise Libray Logging, etc etc etc"

*NOTE: that I had to add a nuspec file to both Project1 and Project2 to get the interproject dependency part working correctly. The nuspec file is essentially blank though, save for the placeholder values:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>abc</authors>
    <owners>abc</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>defr</description>
    <copyright>Copyright 2013</copyright>
  </metadata>
</package>

The closest thing I found on the NuGet issue tracker is http://nuget.codeplex.com/workitem/3097, but that is slightly different and neither of the solutions put forward in there seemed to work.

Anybody have a similar situation working correctly?

I am using NuGet 2.7 with Visual Studio 2012.

syazdani
  • 4,760
  • 1
  • 26
  • 35

3 Answers3

8

I ended up looking through the NuGet source code and found out what the problem we had was.

In our case, it ended up being two separate issues:

  • The projects themselves had .sln files in their folders
  • We were incorrectly emptying out the solution level packages folder before doing the pack command in our build script.

The reason why these were causing problems is because NuGet looks for the solution level packages folder to decide which package dependencies to pull in (not quite sure how this determination is made). If the path to that packages folder is incorrect (as it would be if NuGet uses the wrong solution file), then it can't resolve the dependencies correctly. In addition if the packages folder is empty, it also cannot resolve the dependencies correctly.

syazdani
  • 4,760
  • 1
  • 26
  • 35
3

My fix I did to resolve that, I runned this script in my postbuild:

param([string]$projectNuspec, [string]$projectName, [string]$projectPath)

Function DisplayInfo([string] $text)
{
        Write-Host ""
        Write-Host "****************************"
        Write-Host $text
        Write-Host "****************************"
        Write-Host ""
}

$csprojMustBeSaved = $false
$packageFileName = [System.IO.Path]::Combine($projectPath, "packages.config")

If(Test-Path  $packageFileName)
{
    $csprojMustBeSaved = $false
    Write-host "Project is:" $projectName
    Write-Host "Nuspec file is:" $packageFileName   
    $projectFullName = [System.IO.Path]::Combine($projectPath, $projectName)
    $nuspecFullName = [System.IO.Path]::Combine($projectPath, $projectNuspec)

    Write-Host "Open Nuspec File: " $packageFileName
    $nuspecStream =  [System.IO.File]::OpenText($nuspecFullName)
    $nuspecDoc = New-Object System.Xml.XmlDocument  
    $nuspecDoc.Load($nuspecStream)
    $nuspecStream.Dispose()

    Write-Host "Open project File: " $projectFullName
    $csprojStream =  [System.IO.File]::OpenText($projectFullName)
    $csprojDoc = New-Object System.Xml.XmlDocument
    $csprojDoc.Load($csprojStream)  
    $csprojStream.Dispose()

    $xnm = New-Object System.Xml.XmlNamespaceManager(New-Object System.Xml.NameTable);
    $xnm.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");

    [System.Xml.XmlNode]$metaData = $nuspecDoc.package.metadata

    if($metaData.dependencies -ne $null)
    {
        DisplayInfo("Clear dependencies in nuspec")
        foreach($dependencies in $metaData.dependencies)
        {
            $metaData.RemoveChild($dependencies)
        }       
        DisplayInfo("Dependencies Cleared")
    }

    DisplayInfo("Open packages.config File: $packageFileName")
    $packageFile = New-Object System.Xml.XmlDocument    
    $packageFile.Load($packageFileName)

    $FirstTime = $true
    foreach($package in $packageFile.packages.ChildNodes)
    {
        if(!$package.id.Equals(""))
        {
            if($FirstTime -eq $true)
            {
                $dependencies = $null
                $dependencies = $nuspecDoc.CreateElement('dependencies', "http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd")   
                $nuspecDoc.package.metadata.AppendChild($dependencies)
                $FirstTime = $false
            }

            Write-Host "Create dependency for: "$package.id"version: "$package.version
            $dependency = $nuspecDoc.CreateElement('dependency', "http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd")   
            $dependency.SetAttribute('id',$package.id)
            $packageVersion = [string]::Format("[{0}]",$package.version)
            $dependency.SetAttribute('version',$packageVersion)             
            $dependencies.AppendChild($dependency)          
        }   
    }

    if($csprojMustBeSaved -eq $true)
    {
        Write-Host "Save Csproj File: "$projectFullName
        $csprojDoc.Save($projectFullName)

        Write-host "File Saved successfuly" 
    }

    Write-Host "Save Nuspec File: " $projectPath$projectNuspec
    $nuspecDoc.Save($nuspecFullName);   
    Write-host "File Saved successfuly" 
}
else
{
    Write-Host "There is no " $packageFileName " skip this step"  
}
David C
  • 678
  • 6
  • 11
0

I faced the exact same problem few weeks ago, prior to version 2.7 it was working. All my dependencies will have been added to the package manifest. When I updated from version 2.6 to 2.7 it wasn't working anymore so I created a powershell task to copy the content of package.config to the nuspec before the build.

David C
  • 678
  • 6
  • 11