Skip to content

In a workspace, Cargo should respect per-crate .cargo/configs #9670

@iankronquist

Description

@iankronquist

Problem

Thanks for creating cargo, it's such a useful build tool.
Unfortunately, I've run into an issue with Cargo workspaces and .cargo/config.toml files.

In a workspace, Cargo does not respect any per-crate .cargo/config.tomls

Steps

Given a workspace like this, with two default crates generated by cargo:

ian@iankhome:~/cargo-bug$ tree -a
.
├── Cargo.lock
├── Cargo.toml
├── testbin
│   ├── .cargo
│   │   └── config.toml
│   ├── .gitignore
│   ├── Cargo.toml
│   └── src
│       └── main.rs
└── testlib
    ├── .cargo
    │   └── config.toml
    ├── .gitignore
    ├── Cargo.toml
    └── src
        └── lib.rs
ian@iankhome:~/cargo-bug$ cat Cargo.toml
[workspace]

members = [
    "testlib",
    "testbin",
]

I created a per-crate .cargo/config.toml like so:

ian@iankhome:~/cargo-bug$ cat testbin/.cargo/config.toml
[build]
rustflags = ["-C", "link-args=-fsome-artisanal-option"]
ian@iankhome:~/cargo-bug$ cat testlib/.cargo/config.toml
[build]
rustflags = ["-C", "link-args=-fsome-artisanal-option"]

When I build the project I do not see the link argument -fsome-artisanal-option passed to the linker. I have confirmed using strace that cargo never opens those config files.

If I run cargo build within one of the crates in the workspace, cargo does parse the .cargo/config.toml. e.g.:

~/cargo-bug$ cd testbin/
~/cargo-bug/testbin$ cargo build
   Compiling testbin v0.1.0 (/home/ian/cargo-bug/testbin)
error: linking with `cc` failed: exit status: 1
....
  = note: cc: error: unrecognized command line option '-fsome-artisanal-option'

In this example the link error is expected because the -fsome-artisanal-option is not a real linker flag but an example pulled from some rust documentation. The point is that we can see that it is being passed to the linker when we run cargo build from within testbin.

Possible Solution(s)

When building each sub-crate in a workspace, Cargo should check for a .cargo/config.toml within the crate root, and parse and load it when building that specific crate. Options within one sub-crate's .cargo/config.toml should not be passed to another sub-crate.

Notes

Output of cargo version:

$ cargo --version
cargo 1.54.0-nightly (0cecbd673 2021-06-01)
$ rustup --version
rustup 1.24.2 (755e2b07e 2021-05-12)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.54.0-nightly (cc77ba46f 2021-06-03)`
$ rustc --version
rustc 1.54.0-nightly (cc77ba46f 2021-06-03)
$ cat /etc/issue
Ubuntu 20.04.2 LTS \n \l

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions