0

I wanted to add some logging to a lib used both direcly in my app and also from another lib as transitive dependency. So i've cloned the repo, added logging and then added it as

somelib = {path= "../../somelib"}

to my Cargo. The build then failed with "Only one package in the dependency graph may specify the same links value." which seems to be a known issue (or few related).

Do I understand correctly that it can't be solved now? Is using "path" the only way to force cargo to take local lib? Maybe locally modified version can be copied into some cache or alike? Or maybe tweak something in Cargo.lock? Fork it and try to add as github dep or there will be same problem as with local path?

user656449
  • 2,950
  • 2
  • 30
  • 43

1 Answers1

1

I wanted to add some logging to a lib used both direcly in my app and also from another lib as transitive dependency.

When you want to replace a transitive dependency with modified code, that is a job for the [patch] section, documented in the “Overriding Dependencies” chapter of the Cargo reference.

You would write, in your project's root Cargo.toml (whether that is a package or a workspace):

[patch.crates-io]
somelib = { path = "../../somelib" }

Then, wherever there is a dependency for somelib in crates.io, with the same major version as your patched version declares, it will be replaced with your patched version.

Note that [patch] only applies to cargo builds done in your package/workspace. [patch] has no effect if you publish your package; you are not allowed to mess with the dependency graph when some other project depends on your library. If this is your situation, then you must contribute the changes to the upstream library, or fork and publish your own version if necessary.

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108