How to publish crates that are in the same workspace conveniently?

Since procedural macros are always a separate crate, here is the structure of my project:

crate
|-- macros
|--Cargo.toml

Cargo.toml:

[dependencies].
macros = {path = "macros"}

[workspace]
members = ["macros"]

But here's the problem: To publish the library on crates.io, I need to publish the crates separately, so before publishing I have to change crate cargo.toml and reference macros from crates.io:

[dependencies].
macros = "0.1.0"

This makes it much more complicated, as I don't want to publish it after every change to macros, only for the main crate to always reference macros c crates.io.

Is there not a more convenient strategy for this, given that I want macros to be part of the worspace of my crate.

Further, the library is stored in the github repository as follows:

crate
|-- macros
|--Cargo.toml

Before each publication on crates.io, I have to commit the changes as follows:
crate Cargo.toml always depends on crates.io macros, specifying a version that has not yet been published (first git commit then cargo publish).

And this seems strange, because on github my crate is the same as locally. So when cloning my crate repository (which will include macros), the user will see in crate Cargo.toml a link to crates.io macros, while he will have the local macros.

How to also change the strategy with respect to git?

You don't have to. Cargo has special support for multiple locations which makes this much more automatic. Write your dependencies like this:

[dependencies]
macros = { version = "0.1.0", path = "macros/" }

During development (or use through a git dependency), Cargo will always prefer the path. When you publish the library, Cargo will strip out the path from the published package, and so the dependency will be taken from crates.io instead.

2 Likes

Oh, thanks, turns out I should have specified the version, cargo doesn't detect it automatically for crates.io

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.