Search code examples
rustrust-cargo

Where to put the .cargo/config.toml file in a cargo workspace?


It's stated in the Cargo Book (on page 3.6. Configuration) that when cargo is invoked from the workspace root, the .cargo/config.toml files specified for individual packages within the workspace do not get read.

At present, when being invoked from a workspace, Cargo does not read config files from crates within the workspace. i.e. if a workspace has two crates in it, named /projects/foo/bar/baz/mylib and /projects/foo/bar/baz/mybin, and there are Cargo configs at /projects/foo/bar/baz/mylib/.cargo/config.toml and /projects/foo/bar/baz/mybin/.cargo/config.toml, Cargo does not read those configuration files if it is invoked from the workspace root (/projects/foo/bar/baz/).

Also, it's not exactly specified either in 14.3. Cargo Workspaces - The Rust Programming Language or 3.6. Configuration - The Cargo Book where to put a .cargo/config.toml that would be common to all the packages in the workspace.

My objective here is to specify a single folder where all packages in the workspace should look for the vendored code of external dependencies.

Like if path/to/workspace/root is the the workspace root folder, the packages in the workspace should look for vendored external dependencies in path/to/workspace/root/vendoredcode.

For a single package outside of a workspace, I'd put it .cargo/config.toml in the package root and put the following in it:

[source.crates-io]
replace-with="vendored-sources"

[source.vendored-sources]
directory="path/to/package/root/vendoredcode"

How to do the same for all packages in a workspace with a single .cargo/config.toml file?

PS: There might be some confusion due to my consistent use of the term "package" where some people might commonly use the term "crate".

I read that a single package (a folder with Cargo.toml and an src subfolder which contains main.rs and/or lib.rs) can actually contain multiple binary crates and a single library crate at the same time, so I preferred to use the term "package" instead of "crate" for it.

This choice was to avoid confusion though I recognise that while it might avoid confusion with one set of people, it might create confusion with another set of people. Hence this clarification.


Solution

  • Just put the .cargo/config.toml at the root of the workspace.