Installed project-specific nuget packages not being recognized

匿名 (未验证) 提交于 2019-12-03 08:50:26

问题:

I have a web project that won't build because of a nuget error.

We have many websites that all use a web CMS called Sitecore. Our different websites work under different versions. Thus, we have a common library that targets many versions as described here.

So, I have a project structure like seen below. Please keep in mind this is a logical representation. Consider all these files being at the root of the solution.

  • Common.sln
    • Common.SC65.csproj
      • MyClass.cs [shared]
      • MyClass.SC65.cs
      • packages.Common.SC65.config
    • Common.SC70.csproj
      • MyClass.cs [shared]
      • MyClass.SC70.cs
      • packages.Common.SC70.config
    • Common.SC72.csproj
      • MyClass.cs [shared]
      • MyClass.SC72.cs
      • packages.Common.SC72.config

This is the error I'm having now:

C:\Path\Website.ProjectSC65\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets(225,5): error : Could not locate C:\Path\Common\packages.config. Ensure that this project has Microsoft.Bcl.Build installed and packages.config is located next to the project file.

That leads me to believe that the problem lies in that Bcl targets file, and it seems to be yelling about this line:

<ValidatePackageReferences Packages="@(ValidatePackages)"                            ReferencingProject="$(BclBuildReferencingProject)"                            ReferencingProjectPackagesConfig="$(BclBuildReferencingProjectConfig)"                            ReferencedProject="$(MSBuildProjectFullPath)"                            ReferencedProjectPackagesConfig="$(MSBuildProjectDirectory)\packages.config"                            TreatWarningsAsErrors="$(TreatWarningsAsErrors)" /> 

"Website.ProjectSC65" has a reference to Common.SC65.

I have occasionally had weird problems with nuget packages acting funny, but I can't seem to shake this issue no matter what I do. Mostly some Update-Package -reinstall combination solves my issues. I need these different packages per project, as they support different versions of the framework.

I am fully aware that it is looking for a packages file that doesn't exist technically, as it should be referencing 'packages.Common.SC65.config'.

What's weirder still, I can't find anything on the internet now that references the above technique to reference project level dependencies. I know I found this at one point, but I have no idea if this is how it should be done.

So, the main things I need to know:

  1. What could be causing the error above in my project build, and how would I fix it?
  2. What is the proper way of setting up nuget packages on a project level? (not a solution level)

回答1:

Alright, so it seems like that technique of including project level configuration isn't supported out of the box on everything.

In my web project, I took the /packages/Microsoft.Bcl.Build.1.0.14/tools/Microsoft.Bcl.Build.targets file and modified the line it was complaining about.

<ValidatePackageReferences Packages="@(ValidatePackages)"                            ReferencingProject="$(BclBuildReferencingProject)"                            ReferencingProjectPackagesConfig="$(BclBuildReferencingProjectConfig)"                            ReferencedProject="$(MSBuildProjectFullPath)"                            ReferencedProjectPackagesConfig="$(MSBuildProjectDirectory)\packages.config"                            TreatWarningsAsErrors="$(TreatWarningsAsErrors)" /> 

I did so by replacing this line: ReferencedProjectPackagesConfig="$(MSBuildProjectDirectory)\packages.config"

With this line:

ReferencedProjectPackagesConfig="$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config" 

Injecting the project name into the property does work well. This may cause issues later if I have other nuget packages I'm referencing from other libraries higher up the chain that are simply "packages.config". For now though, this is a decent solution.

This feels really hacky though. I'd love to hear other answers, especially ones that don't involve modifying the internals of a package.



回答2:

I was getting same error on UWP project. And following below worked for me.

I did check and see the package.config in the right directory and I did have Microsoft.BCL.Build. The only way to get this to work is to close visual studio delete the obj folder in the solutions directory. Once the obj folder is deleted and I reloaded the project in visual studio everything worked again.

Source: http://onteorablog.azurewebsites.net/ensure-that-this-project-has-microsoft-bcl-build-installed-and-packages-config-is-located-next-to-the-project-file/



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!