18

When developing a library in node, if you wish to develop against a library that only exists locally, before you npm publish, you can use npm link /path/to/other/node_library.

What is the equivalent of this for Rust? How do you create another a foo executable that links to bar library, without pushing bar library to a git remote first?

The official rust tutorial shows how to do this using raw rustc, how can this be done in Cargo.toml?

(The cargo documentation shows you how to build a lib, but now how to link to one that doesn't have a remote repository.)

bguiz
  • 27,371
  • 47
  • 154
  • 243

3 Answers3

6

It is also possible to use git file: URL if your dependency is in a local git repo:

[dependencies.local_dep]
git = "file:/some/local/path"

There is also a very useful feature when you want to use your own local copy of some package. You can specify a path to such package in ~/.cargo/config file:

package-name = "/path/to/package"

With this configuration when some other package (let's name it a) requires package-name, regardless of what is declared in a manifest about package-name location, package-name will be build from the source tree specified in this config file. This is useful when you need to test your changes in a library which other projects depend on.

Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • Thanks for your answer, but both of these suggestions do not work when the local dependency and the executable that link to it link against the same 3rd party dependencies, I get an error when running `cargo update`: `At the moment, Cargo only supports a single source for a particular package name (Dependency { name: nickel, source_id: https://github.com/nickel-org/nickel.rs.git, req: *, transitive: true, only_match_name: false }).` – bguiz Aug 05 '14 at 00:29
  • 2
    I believe `package-name` is now `paths` and is an array as per the docs at http://doc.crates.io/config.html#configuration-keys – Fraser Jun 19 '15 at 15:56
3

You can do:

[dependencies.local_dep]
path = "some/local/path"

Check out https://github.com/gfx-rs/gfx-rs/blob/master/Cargo.toml for an example.

abject_error
  • 2,858
  • 1
  • 19
  • 23
  • Thanks for your answer, but this suggestion does not work when the local dependency and the executable that link to it link against the same 3rd party dependencies, I get an error when running `cargo update`: `At the moment, Cargo only supports a single source for a particular package name (Dependency { name: nickel, source_id: https://github.com/nickel-org/nickel.rs.git, req: *, transitive: true, only_match_name: false }).` – bguiz Aug 05 '14 at 00:29
  • [Here](http://doc.crates.io/guide.html#path-dependencies)'s the official Cargo doc about `path`. – Emil Laine Aug 16 '15 at 17:09
0

http://doc.crates.io/manifest.html#the-dependencies-section contains an example of doing this with the path field (the geometry crate):

[package]
# ...

[dependencies]
hammer = { version = "0.5.0", git = "https://github.com/wycats/hammer.rs" }
color = { git = "https://github.com/bjz/color-rs" }
geometry = { path = "crates/geometry" }
Jack O'Connor
  • 10,068
  • 4
  • 48
  • 53