Search code examples
c#.netnugetpackage-management

Is there a .NET (Core) equivalent of npm link?


This essentially amounts to, "how do I clone a .NET project, make modifications, and use it rather than the published one?"

In NodeJS, we have npm link, which lets you link a local package (module) in your node_modules/ directory, to your current project. So for example, instead of using Express in your package.json, you can

  1. Clone Express
  2. Make modifications to express
  3. Compile (transpile, if necessary) and/or build
  4. Run npm link in Express repo to create a globally available local package
  5. Run npm link express in your current project to use your local express, rather that the one you would get if you npm install it.

With .NET, the closest solution I have seen so far includes creating a local feed, but in my experimentation this doesn't seem to work. Other questions on stack overflow like how to use local packages in .net seem to offer solutions using RestoreSources, which is virtually undocumented across the entire web. When attempting to change RestoreSources to use a LocalPackages directory, it is not clear to me the local packages are being used or not (source in obj/ directory still seems to come from nuget package rather than local).


Solution

  • For anyone in the future wondering this, the correct answer is to use the local feed. My problem was that I had cached nuget packages which were being resolved during dotnet restore.

    The following command before restore solved my issues:

    dotnet nuget locals all --clear
    dotnet restore
    

    Essentially, you need to have a nuget config (NuGet.config) in your solution that sets up your local packages directory:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <packageSources>
        <clear />
        <add key="LocalDev" value="./my-project/artifacts" />
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    </packageSources>
    </configuration>
    

    Where artifacts/ directory contains all of the .nupkg packages you want to use during restore. It is obviously also essential that you must make sure those packages are built/compiled before you dotnet restore in your primary solution:

    dotnet pack /p:Version=5.22.12 --configuration Release --force --output "%~dp0artifacts";