Rustc uses different base path for a subcrate when part of a workspace

Hi there,
I'm currently struggeling with building crates that are part of a workspace.
Assume the following structure:

workspace
├─ subfolder
│  └─ crate
│     ├─ src
│     │  └─ main.rs
│     └─ cargo.tom
└─ cargo.toml 

when the subfolder/crateis part of the workspace/cargo.toml like so:

[workspace]
members = ["subfolder/crate"]

The base path for rustcinvokations is the workspace folder, even when cargo buildis executed from within this cratefolder:
Running rustc --edition=2018 --crate-name kernel 'subfolder\crate\./src/main.rs'

On the contrary, if the crate is excluded from the workspace like so:

[workspace]
exclude = ["subfolder/crate"]

The crates folder is the base path of invocation:
Running rustc --edition=2018 --crate-name kernel /src/main.rs

Is this by intention, and if so, may I ask why? And also I'd like to know how could I force the base path of invocation to always be the path cargo build has been executed in ?
The reason for this need is, that I pass additional RUSTFLAGS to the build containing a relative path to my linker script file like so -C link-arg=-T./link.ld. This file is located in the crates folder not in the workspace folder, so based on the invokation base path this file could be found or not ....

Any suggestions are very welcome.
Thanks in advance.

Unfortunately, RUSTFLAGS is by its nature a sad fragile hack, so I suggest avoiding it if you can.

The changed directory is intentional. The entire workspace is built into one target dir.

Build scripts get CARGO_MANIFEST_DIR that points to directory of currently built crate's Cargo.toml, which you should be able to use as a starting point for linker script path.

You may need to convert the path to absolute path using fs::canonicalize. But it will give you an UNC path which many Windows programs will not understand, so you may to clean that up using dunce.

Hi,
thanks for the reply. Well it's not the issue to get the path to the current crates Cargo.toml that is build.
I'm using env::current_dir() to achieve this. However, there seem to be no env variable pointing to the workspace root folder of the current build, which is what I'd currently like to see.
So CARGO_MANIFEST_DIR, PWD and env::current_dir()are pointing the current crates path. But there is nothing that gets passed to the build script that is pointing to the workspace folder...Or am I missing something?

You're not missing anything. There's no env var for root of the workspace. That's why I'm suggesting using absolute paths instead.

Hmm, well.... thanks for the hint, however, but I think this does not look like a "portable" solution to me and it's not solving my problem, or I'm just not able to see it from a less complicated angle.....
If I'd like to put a build script template into my crates repository I'd like not to put any absolute path in there. And as the consumer of my crate might use a workspace or not to built his final binary it's hard to guess any path. So, do you see any option to get the workspace root path (relative to the current built crates path would be fine)? Would this be worth a "feature" request for upcoming rust/cargo versions?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.