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 or 3.6. Configuration 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, should said dependency be used in their code.

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

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



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

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.

Leave a Reply