Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local dependencies for publishable crates #17

Closed
ychiguer opened this issue Jun 16, 2022 · 2 comments
Closed

Local dependencies for publishable crates #17

ychiguer opened this issue Jun 16, 2022 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@ychiguer
Copy link

ychiguer commented Jun 16, 2022

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:

2022-06-16_12-53

  • 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

2022-06-16_12-52

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.

@dannymcgee
Copy link
Contributor

dannymcgee commented Jun 21, 2022

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.

@dannymcgee dannymcgee added enhancement New feature or request help wanted Extra attention is needed labels Jun 21, 2022
@dannymcgee dannymcgee changed the title Apps and Libs dependencies Local dependencies for publishable crates Jun 21, 2022
@dannymcgee
Copy link
Contributor

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" }
# ...

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants