Publish libraries with internal dependencies

I have a workspace with 2 libraries

➜  cargo-workspaces tree
.
├── Cargo.lock
├── Cargo.toml
├── lib-one
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
└── lib-two
    ├── Cargo.toml
    └── src
        └── lib.rs

I have lib-one with "log" dependency and lib-two with dependency on "lib-one'.

lib-one/Cargo.toml

[package]
name = "lib-one"
version = "0.1.0"
authors = ["Ivan Skiridomov <ivan.skiridomov@gmail.com>"]
edition = "2018"

[dependencies]
log = "0.4"

lib-two/Cargo.toml

[package]
name = "lib-two"
version = "0.1.0"
authors = ["Ivan Skiridomov <ivan.skiridomov@gmail.com>"]
edition = "2018"

[dependencies]
lib-one = "^0.1"

I expect that when i run cargo publish from lib-one directory, it will be published. But it throw an error instead:

➜  lib-one git:(master) ✗ cargo publish 
    Updating crates.io index
error: no matching package named `lib-one` found
location searched: registry `https://github.com/rust-lang/crates.io-index`
required by package `lib-two v0.1.0 (/Users/i_skiridomov/Projects/tmp/cargo-workspaces/lib-two)

How i can publish workspace libraries with dependencies to crate registry ?

Answering myself: in lib-two/Cargo.toml should be a path attribute.

[dependencies]
lib-one = {path = "../lib-one", version="^0.1", registry="local-registry"}

Is it correct way ?

Yep, you need both a path and a version.

For local development, cargo will use the path dependency. When publishing, however, Cargo will rewrite the dependency in Cargo.toml to mention only the version.

Unrelated, but in Cargo version = "^0.1" is equivalent to version = "0.1", that is, circumflex is optional.

1 Like

This question should be asked on the https://users.rust-lang.org forum. This forum (https://internals.rust-lang.org) is dedicated to the development of Rust itself.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.