How to made crate library on live project?

I develop some GUI project and thoughts to separate one feature to the library, then publish it on crates as the separated product.

In PHP, I've work directly, inside the vendor directory, then copied files to the library project manually, on ready and tested :slight_smile:

But with Rust compiler, not sure how can I make changes directly on live project in development, before commit or publish new release then download it through cargo.

Maybe I can work with initial release somewhere in target dir? How do you work with libraries on-the-fly?

Thanks

If you want to use a crate from your local filesystem you can add it as a path dependency.

[dependencies]
my-lib   = { path = "../my-lib" }

From there, cargo will build the dependency and link it into your program, and automatically recompile if my-lib changes.

3 Likes

If you're modifying a library which is already in crates.io, perhaps preparing the next version and testing it in some programs using it, you can add a patch section:

[dependencies]
lazy-regex = "3.3"

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

Bonus: when it's ready you just have to comment out the line in the patch section. This helps not losing version or features information in the dependencies part.

2 Likes

Thanks to all for replies!

Variant with patch looks useful, just interesting how to ignore it for production environment because it works like internal / local fork and replaces original dependency.

Found the answer:

Note: The [patch] table can also be specified as a configuration option, such as in a .cargo/config.toml file or a CLI option like --config 'patch.crates-io.rand.path="rand"'. This can be useful for local-only changes that you don’t want to commit, or temporarily testing a patch.

https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html#the-patch-section

2 Likes

The .cargo/config.toml solution looks dangerous when testing with a library you're locally developing: what are the odds you'll test then forget your Cargo.toml doesn't represent what you tested and you'll commit ? If you modify the Cargo.toml file directly, this can't happen.

1 Like

Finally, created symlink from [patch.crates-io] path to src dir of repository location and that's enough for me, thanks

The normal practice for parallel developed dependencies of crates published to crates.io is to specify both the version and the local path: when you publish cargo strips out the local path and uses the version (or git repository etc)

It's not clear that it matters for applications that aren't published: you generally want whatever code you were locally developing against, even if they aren't published.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.