0

I would like to restore my nugets one level up. My repo directory points to say for ex. c:\repo\s and my solutin is in c:\repo\s\src, when I am restoring packages with nuget restore , its restoring the packages at C:\repo\s\src\packages and I would like that to be C:\repo\s\packages. Appreciate your help.

I have the below nuget.config file in C:\repo\s\src directory.

$<configuration>
  <config>
    <add key="repositoryPath" value="..\..\packages" />
  </config>
</configuration>

My Yaml job looks like this

$steps:
- task: NuGetToolInstaller@0
  displayName: 'Use NuGet 4.3.0'

- task: NuGetCommand@2
  displayName: 'NuGet restore'
  inputs:
    restoreSolution: src/myproject.sln
    vstsFeed: '4448b1e2-8ac8-45ef-870c-1ebab90f3348'
    restoreDirectory: '$(Build.SourcesDirectory)'


    - task: VSBuild@1
      displayName: 'Build solution src/myproject.sln'
      inputs:
    solution: src/myproject.sln
    vsVersion: 15.0
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

1 Answers1

1

msbuild not picking packages from restored location

Whether you use the nuget.config file or you directly specify restoreDirectory: '$(Build.SourcesDirectory)' in the nuget restore task, nuget will restore the package to the folder C:\repo\s\packages.

However, NuGet Restore only restores packages to the restore directory, but does not modify your project file.

When we add the nuget packages to the project, it will add following code in the project file to specify the dll location:

  <ItemGroup>
    <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\packages\Newtonsoft.Json.12.0.3-beta1\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>
  </ItemGroup>

There is node HintPath to specify the location of the dll file.

When we use nuget.config or restoreDirectory: '$(Build.SourcesDirectory)' to change the location of the package restore, MSBuild will not pick up the package/dll based on the HintPath. The correct HintPath should be:

<HintPath>..\..\packages\Newtonsoft.Json.12.0.3-beta1\lib\net45\Newtonsoft.Json.dll</HintPath>

That the reason why the msbuild not picking packages from restored location.

To resolve this issue, you need use the NuGet command line in the Package Manager Console (On local VS):

Update-Package -reinstall

to force reinstall the package references into project, it will update the HintPath. Upload the changes file to the Azure devops and build it.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135