Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
iankronquist opened this issue Jul 9, 2021 · 3 comments
Closed

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

iankronquist opened this issue Jul 9, 2021 · 3 comments
Labels
C-bug Category: bug

Comments

@iankronquist
Copy link
Contributor

iankronquist commented Jul 9, 2021

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
@ehuss
Copy link
Contributor

ehuss commented Jul 9, 2021

Thanks for the report! Yea, this can be confusing, since cargo always uses the current working directory to find configuration files. This is mostly the same as issue #2930, so I'm going to close in favor of that. Unfortunately it is not so simple to change the behavior since it could break existing workflows, so the path forward is not exactly clear.

@ehuss ehuss closed this as completed Jul 9, 2021
@iankronquist
Copy link
Contributor Author

Sorry for the duplicate issue. Is this behavior explicitly documented? If not, I think it should be documented as part of the cargo reference: https://doc.rust-lang.org/cargo/reference/config.html

@ehuss
Copy link
Contributor

ehuss commented Jul 9, 2021

I believe it is documented at the top of the page:

It looks for configuration files in the current directory and all parent directories.

Perhaps it could be worded a little more clearly? I'm not sure how, though. ("current working directory"?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

2 participants