-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Cargo does not always search for .cargo/config file in project root #2930
Comments
I agree, the config resolution should not be based on the current working directory, but starting at the project (where Cargo.toml is, i.e. |
It was in the root before because .cargo/config would not be resolved: rust-lang/cargo#2930 Changing the working directory fixes it. Putting those files to src/rust is good to keep it close to the rust code. This also allows to add another Rust project in e.g. /tools, with a different config.
This behavior is described in this bug: rust-lang#2930 I think we should document limitations of the tool which exist at present if they do not bind our hands in the future. If this bug is fixed, this note should be removed.
Document cargo limitation w/ workspaces & configs This behavior is described in this bug: #2930 I think we should document limitations of the tool which exist at present if they do not bind our hands in the future. If this bug is fixed, this note should be removed.
Is there any progress? I wonder how can I organize more than one crate in a directory having same dependencies but different config.toml ? It seems that now I can only manage their cargo.toml and config.toml by hand. |
Changing this will break backwards-compatibility. I work with projects that rely on this behavior to easily inject configuration into Note that this use-case could become obsolete with a stable |
I have just encountered this issue when trying to change the output directory for one specific binary crate as a part of a large project. Telling users to specify |
The backwards-compatibility behavior is broken for every client of bitbake, preventing them from easily injecting configuration into cargo by modifying the source tree, a use case documented by cargo to work, but does not. Your ability to do what you're doing is actually contrary to the cargo config documentation. At least as documented, it looks more like you're dependent on a bug. The As a way forward, I can see adding support for the |
@compenguy would bitbake be able to take advantage of |
It's a really weird fit. The meta-build system (in this case meta-rust+bitbake) shouldn't need to know or care if the fetched source for build has a So if The '-C' flag solution proposed in #10098 is probably the best match for (meta)build systems' expectations, it eliminates all the surprises I mentioned, it's a common convention, and it's straightforward to document. |
If it doesn't work, thats ok. I wasn't sure how the system was put together, if |
(since I've not seen this mentioned in this issue) |
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
Add '-C' flag for changing current dir before build This implements the suggestion in #10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to `--manifest-path` that resolves the issue described in #2930. The behavior of this new flag makes cargo build function exactly the same when run at the root of the project as if run elsewhere outside of the project. This is in contrast to `--manifest-path`, which, for example, results in a different series of directories being searched for `.cargo/config.toml` between the two cases. Fixes #10098 Reduces impact of #2930 for many, possibly all impacted, by switching to this new cli argument. ### How should we test and review this PR? The easiest way to reproduce the issue described in #2930 is to create an invalid `.cargo/config.toml` file in the root of a cargo project, for example `!` as the contents of the file. Running cargo with the current working directory underneath the root of that project will quickly fail with an error, showing that the config file was processed. This is correct and expected behavior. Running the the same build with the current working directory set outside of the project, e.g. /tmp, and passing the `--manifest-path /path/to/project/Cargo.toml`. The build will proceed without erroring as a result of reading the project's `.cargo/config.toml`, because config file searching is done from cwd (e.g. in `/tmp` and `/`) without including the project root, which is a surprising result in the context of the [cargo config documentation](https://doc.rust-lang.org/cargo/reference/config.html), which suggests that a `.cargo/config.toml` file checked into the root of a project's revision control will be processed during the build of that project. Finally to demonstrate that this PR results in the expected behavior, run cargo similar to the previous run, from /tmp or similar, but instead of `--manifest-path /path/to/project/Cargo.toml`, pass `-C/path/to/project` to cargo (note the missing `Cargo.toml` at the end). The build will provide the exact same (expected error) behavior as when running it within the project root directory. ### Additional information ~Passing a path with a trailing '/' will result in failure. It is unclear whether this is a result of improper input sanitization, or whether the config.cwd value is being improperly handled on output. In either case this needs to be resolved before this PR is merge-ready.~ (the above issue appears to have been a side effect of local corruption of my rustup toolchain, and unrelated to this change) Because a `struct Config` gets created before command line arguments are processed, a config will exist with the actual cwd recorded, and it must then be replaced with the new value after command line arguments are processed but before anything tries to use the stored cwd value or any other value derived from it for anything. This change effectively creates a difficult-to-document requirement during cargo initialization regarding the order of events. For example, should a setting stored in a config file discovered via cwd+ancestors search be wanted before argument processing happens, this could result in unpleasant surprises in the exact use case this feature is being added to fix. A long flag was deferred out to not block this on deciding what to name it. A follow up issue will be created.
This implements the suggestion in rust-lang#10098 to make cargo change cwd before completing config processing and starting the build. It is also an alternative to --manifest-path that resolves the issue described in rust-lang#2930.
As an update, we now have #12738 for tracking finding ways of supporting config-based workflows with packages. Due to the compatibility concerns, I'm not sure what more there is we can do for this. I'm proposing to the cargo team that we close this. |
Currently, the search path for
.cargo/config
files is dependent on the current working directory at the time when cargo is executed. I find this behavior very surprising: I would expect that cargo at least always reads the.cargo/config
file at the root of the project it builds.It is important to note though that for some applications, this is probably the right thing to do (for example, cargo new does not have any associated project root to work with).
Examples
Test case demonstrating the problem
The text was updated successfully, but these errors were encountered: