Skip to content

fix: Respect .cargo/config.toml build.target-dir #20072

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

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion crates/project-model/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Cargo-like environment variables injection.
use base_db::Env;
use paths::Utf8Path;
use paths::{Utf8Path, Utf8PathBuf};
use rustc_hash::FxHashMap;
use toolchain::Tool;

Expand Down Expand Up @@ -123,6 +123,26 @@ fn parse_output_cargo_config_env(manifest: &ManifestPath, stdout: &str) -> Env {
env
}

pub(crate) fn cargo_config_build_target_dir(
manifest: &ManifestPath,
extra_env: &FxHashMap<String, Option<String>>,
sysroot: &Sysroot,
) -> Option<Utf8PathBuf> {
let mut cargo_config = sysroot.tool(Tool::Cargo, manifest.parent(), extra_env);
cargo_config
.args(["-Z", "unstable-options", "config", "get", "build.target-dir"])
.env("RUSTC_BOOTSTRAP", "1");
if manifest.is_rust_manifest() {
cargo_config.arg("-Zscript");
}
utf8_stdout(&mut cargo_config)
.map(|stdout| {
Utf8Path::new(stdout.trim_start_matches("build.target-dir = ").trim_matches('"'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this breaks if the path contains quotes, e.g. build.target-dir = 'a " b' or build.target-dir = """a " ' b""".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. There is general cleanup potential here. For one, we invoke cargo config three times now for the different things, which is unnecessary, we can just invoke it plain and read out the info from that we want from that output. Secondly, it supports json output (and json-value though I have no idea what that means). We should just use the json output for more robust deserialization.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json is {"build":{"target-dir":"a \" b"}}, json-value is "a \" b".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, it made no difference when I tested it but that was because I ran it without a target config so it returned the root object in both :D

.to_owned()
})
.ok()
}

#[test]
fn parse_output_cargo_config_env_works() {
let stdout = r#"
Expand Down
32 changes: 23 additions & 9 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use crate::{
WorkspaceBuildScripts,
build_dependencies::BuildScriptOutput,
cargo_workspace::{CargoMetadataConfig, DepKind, PackageData, RustLibSource},
env::{cargo_config_env, inject_cargo_env, inject_cargo_package_env, inject_rustc_tool_env},
env::{
cargo_config_build_target_dir, cargo_config_env, inject_cargo_env,
inject_cargo_package_env, inject_rustc_tool_env,
},
project_json::{Crate, CrateArrayIdx},
sysroot::RustLibSrcWorkspace,
toolchain_info::{QueryConfig, rustc_cfg, target_data_layout, target_tuple, version},
Expand Down Expand Up @@ -280,8 +283,11 @@ impl ProjectWorkspace {
.ok()
.flatten();

let target_dir =
config.target_dir.clone().unwrap_or_else(|| workspace_dir.join("target").into());
let target_dir = config
.target_dir
.clone()
.or_else(|| cargo_config_build_target_dir(cargo_toml, extra_env, &sysroot))
.unwrap_or_else(|| workspace_dir.join("target").into());

// We spawn a bunch of processes to query various information about the workspace's
// toolchain and sysroot
Expand Down Expand Up @@ -452,6 +458,14 @@ impl ProjectWorkspace {
let targets = target_tuple::get(query_config, config.target.as_deref(), &config.extra_env)
.unwrap_or_default();
let toolchain = version::get(query_config, &config.extra_env).ok().flatten();
let project_root = project_json.project_root();
let target_dir = config
.target_dir
.clone()
.or_else(|| {
cargo_config_build_target_dir(project_json.manifest()?, &config.extra_env, &sysroot)
})
.unwrap_or_else(|| project_root.join("target").into());

// We spawn a bunch of processes to query various information about the workspace's
// toolchain and sysroot
Expand All @@ -469,18 +483,13 @@ impl ProjectWorkspace {
)
});
let loaded_sysroot = s.spawn(|| {
let project_root = project_json.project_root();
if let Some(sysroot_project) = sysroot_project {
sysroot.load_workspace(
&RustSourceWorkspaceConfig::Json(*sysroot_project),
project_root,
progress,
)
} else {
let target_dir = config
.target_dir
.clone()
.unwrap_or_else(|| project_root.join("target").into());
sysroot.load_workspace(
&RustSourceWorkspaceConfig::CargoMetadata(sysroot_metadata_config(
config,
Expand Down Expand Up @@ -535,7 +544,12 @@ impl ProjectWorkspace {
.unwrap_or_default();
let rustc_cfg = rustc_cfg::get(query_config, None, &config.extra_env);
let data_layout = target_data_layout::get(query_config, None, &config.extra_env);
let target_dir = config.target_dir.clone().unwrap_or_else(|| dir.join("target").into());
let target_dir = config
.target_dir
.clone()
.or_else(|| cargo_config_build_target_dir(detached_file, &config.extra_env, &sysroot))
.unwrap_or_else(|| dir.join("target").into());

let loaded_sysroot = sysroot.load_workspace(
&RustSourceWorkspaceConfig::CargoMetadata(sysroot_metadata_config(
config,
Expand Down