You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was working with a Rust app and two libs that depend on each other, and I couldn't find a way to specify these dependencies other than explicitly specifying their relative paths on each app/lib's Cargo.toml file. I was under the impression that the Cargo.toml in the monorepo's parent directory would take care of all of this.
Here is an example:
apps/myapp/src/main.rs :
use lib1 ;
fn main() {
let result: String = lib1::lib1();
println!("{result}");
}
apps/myapp/Cargo.toml :
[package]
name = "myapp"
version = "0.1.0"
edition = "2021"
[dependencies]
lib1 = {path = "../../libs/lib1"}
libs/lib1/src/lib.rs :
use lib2;
pub fn lib1() -> String {
lib2::lib2()
}
libs/lib1/Cargo.toml :
[package]
name = "lib1"
version = "0.1.0"
edition = "2021"
[dependencies]
lib2 = {path = "../lib2"}
libs/lib2/src/lib.rs :
pub fn lib2() -> String {
"Hello".into()
}
libs/lib2/Cargo.toml :
[package]
name = "lib2"
version = "0.1.0"
edition = "2021"
[dependencies]
dep-graph
I would like to know if there is a better way to specify these dependencies especially that we intend to publish to crates.io which would not work if we have to specify a dependency by path.
Thanks.
The text was updated successfully, but these errors were encountered:
Specifying the deps by path in Cargo.toml is the canonical way to do it as far as I know, but it does leave something to be desired in terms of workflow. In my own project that's published to crates.io, what I've been doing is changing them to relative paths while I'm working, and then changing them back and incrementing the versions when I need to publish. It's pretty tedious. :)
I'll put it on my to-do list to add a "publishable" flag and try to come up with a way to automate this, but I'm honestly not super proficient with Rust devops, so I would love some suggestions if anyone has any ideas to share!
Edit to add: Currently the only thing the root-level Cargo.toml does is declare the workspace and its members (kind of like Nx's workspace.json). I'm aware of some projects with pretty sophisticated workspace layouts (e.g. Bevy Engine) with presumably automated build/publish pipelines, so that would probably be a good place to start investigating.
Update: I'm not sure if this is a relatively new Cargo feature or if I just hadn't been aware of it before, but you can now specify both a path and a version for workspace dependencies in your Cargo manifest, which makes them publishable without having to edit anything:
[package]
name = "myapp"# ...
[dependencies]
lib1 = { version = "1.2.3", path = "../../libs/lib1" }
# ...
Hey there,
I was working with a Rust app and two libs that depend on each other, and I couldn't find a way to specify these dependencies other than explicitly specifying their relative paths on each app/lib's Cargo.toml file. I was under the impression that the Cargo.toml in the monorepo's parent directory would take care of all of this.
Here is an example:
dep-graph
I would like to know if there is a better way to specify these dependencies especially that we intend to publish to crates.io which would not work if we have to specify a dependency by path.
Thanks.
The text was updated successfully, but these errors were encountered: