Function embuild::cargo::workspace_dir

source ·
pub fn workspace_dir() -> Option<PathBuf>
Expand description

Try to get the path to crate workspace dir or None if unavailable.

If the environment variable CARGO_WORKSPACE_DIR is set, it is returned. Otherwise, go up the directory tree of the current crate’s out_dir until we’re outside of the target directory.

The workspace directory is the directory containing the Cargo.lock file and the target directory (the directory where all compilation artifacts are stored).

As there is currently no cargo provided way to get the workspace directory path (see issue rust-lang/cargo#3946), we try to guess it from the current out dir. If this approach results in the wrong directory or causes issues, the user can override it by setting the CARGO_WORKSPACE_DIR environment variable.

A neat trick is to add the CARGO_WORKSPACE_DIR variable to the [env] section of the workspace’s .cargo/config.toml file, like this:

[env]
CARGO_WORKSPACE_DIR = { value = "", relative = true }