0

I have a library project used in multiple applications. Let's call this Library1. Library1 has its ownNuGet package dependencies. When I setup a project, ProjectA, using Library1, it specified a HintPath for each .dll that Library1 depended on. Let's say this HintPath was "..\etc.dll".

I now have Library2, which depends on Library1, and ProjectB that depends on Library2 (and Library1). I have a hierarchy like this:

ProjectB
--Library2
----Library1

But the other project uses Library1 directly, like so:

ProjectA
--Library1

When opening either ProjectB, the packages are being resolved in "ProjectB\packages", which is two folders up from the library depending on it, meaning it should be looking in "....\etc.dll" rather than "..\etc.dll" specified in the HintPath.

Is there a way I can specify a HintPath depending on the parent project, or any kind of workaround that will allow this to work without me having to manually change each of my .dll reference paths? Perhaps a file I could add to the parent project?

snotyak
  • 3,709
  • 6
  • 35
  • 52
  • In my point of view, I only differentiate all external library in a single "lib" folder for the project. I may not try to recreate the dependency of related libraries. For example, ProjectA have a reference of "Library1.dll" in "lib" folder and ProjectB have a reference to "Library2.dll" but also have "Library1.dll" in the "lib" folder for the dependency. – Zeal Jul 02 '15 at 15:37
  • @Zeal My "Library2" and "Library1" are git submodules. They're projects and while I could use .dll's we would need to update them frequently. – snotyak Jul 02 '15 at 16:42

1 Answers1

1

You can create a NuGet.Config file can specify the repositoryPath which allows you to override the default location where NuGet packages are downloaded to for the project.

<configuration>
  <config>
    <add key="repositorypath" value="Packages" />
  </config>
</configuration>

The repositoryPath is relative to the NuGet.Config file's directory.

NuGet will look for this file based on the solution you open so you would need to place it so that either one or all of your solution's use this file. A simple way is to put it in a directory that is a parent of both solutions but you can add it for each solution or just the one.

If the solution is in c:\a\b\c then NuGet will look for the file in these locations in the order specified, finally falling back to the one in your user profile.

  1. c:\a\b\c.nuget\nuget.config
  2. c:\a\b\c\nuget.config
  3. c:\a\b\nuget.config
  4. c:\a\nuget.config
  5. c:\nuget.config

After setting this up you may need to reinstall the NuGet packages so the correct hintPaths are added to your project.

Matt Ward
  • 47,057
  • 5
  • 93
  • 94
  • While this does work for the parent project (where the .sln is located), the inner library project dependencies won't create their own `Packages` folders with this nuget.config. What I'm looking for is a way to use my library projects in different projects, with different structures and still have their package references work correctly. Ideally, I'd be able to set a separate location for a library's .csproj within that library's folder and have its packages downloaded there so the hintpath would work regardless of the project using it. Does that make sense? – snotyak Jul 02 '15 at 16:48
  • You cannot have a separate packages directory for each project. NuGet's packages directory is based on the solution's directory. Unless you switch to the new .xproj and project.json style projects, which do not store hint paths at all, but are only supported in Visual Studio. To keep the original hint paths you would have to have some tool or script that restores the packages correctly for each project that has its own unique packages directory and you would also need to only update the NuGet packages for a project in the correct parent solution. – Matt Ward Jul 02 '15 at 16:54
  • That makes sense. Is there a way I could make it so all projects use a folder in a user's home directory rather than making it relative? I couldn't get environment variables to work in the nuget.config file. – snotyak Jul 02 '15 at 17:40
  • You could try a global repositoryPath in the NuGet.Config file in your user profile or a full path with the drive letter in the NuGet.Config placed in the solution directory. The problem is that Visual Studio will always try to use a relative path for the hint path unless the directory is on a different drive. – Matt Ward Jul 02 '15 at 18:51
  • Ah. I think I could solve the problem if I could specify a fixed path (that worked for all users), and do the same as my HintPath. Using the user's home directory would be most universal in this case due to permissions but I haven't had success using %SOME_VARIABLE% in my path. – snotyak Jul 02 '15 at 21:08
  • My solution was to add a nuget.config in my project directory, making my packages download to my home directory then referencing that directory using the `$(HOME)` environment variable in my .csproj files. Thanks! – snotyak Jul 03 '15 at 00:58