简体   繁体   中英

How to change the NuGet-package reference in a Visual Studio project to use Nuget.config

I have a Visual Studio project file with the extension .csproj. Inside it are references like this:

<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props"....

I have now made a NuGet.config file in the parent folder of from the solution folder. And I removed the local "packages" folder. In the new nuget.config I set up a common location for storing packages.

nuget.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <config>
        <add key="repositoryPath" value="D:\Data\NuGet" />
    </config>
    <packageRestore>
        <add key="enabled" value="True" />
    </packageRestore>
</configuration>

When I building I now get this error:

This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information... The missing file is ..\\packages\\Microsoft.Net.Compilers.1.0.0\\build\\Microsoft.Net.Compilers.props.

How can I solve this?

If I manually have to replace the (Import Project="..\\packages...) elements in the project file, what should I change it to, so that it follows the configuration from the Nuget.config?

If I manually have to replace the (Import Project="..\\packages...) elements in the project file, what should I change it to, so that it follows the configuration from the Nuget.config?

Since you use the new nuget.config file which changed the path of the local nuget reference(like this <add key="repositoryPath" value="xxxxxx" /> ).

And Restore will only restore the missing nuget packages but will not change to use the new nuget package location in xxx.csproj .

So you can follow my steps to resolve the issue:

Solution

1) Tools-->Nuget Package Manager-->Package Manager Console-->

type Update-Package -reinstall to reinstall these packages to reference the new right path.

2) enter the xxxx.csproj file, delete these duplicate, old import info like these:

 <Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\..\..\..\..\..\installed_packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />

 <Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props'))" />

3) Then rebuild your project and will solve this issue.

Update 1

The new Nuget.config file will specify that the newly installed nuget packages use the new reference address, but for previously installed nuget packages, the reference address in the xxx.csporj file will remain the old address. The Restore procedure only restores the nuget package under the new path, but it does not make any changes to the nuget reference in xxx.csproj file, so it can only be reinstalled.

Besides , the import node is created by Microsoft.Net.Compilers props file from the build folder in the microsoft.net.compilers nuget package. And it is a nuget mechanism which can do some operation in xxx.csproj file when you install the nuget package.

However , this file is also special and when you change the nuget reference path. Because nuget enabled the new address mechanism, during the uninstallation process, the old address of Microsoft.Net.Compilers.props is still not recognized, so it cannot be uninstalled. In fact , when you execute the reinstall nuget package, a new address has been created in the xxx.csproj file. See this:

在此处输入图片说明

So you should just delete duplicate files from the old address.

Visual Studio option to change the Nuget Package References

  • In Visual Studio Tools=> Nuget Package Manager => Package Sources.
  • You can change the package sources here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM