Skip to content

Commit ec38198

Browse files
committed
Add dedicated target_dir field to CargoConfig and FlycheckConfig
Add dedicated field for `target_dir` in the configurations for Cargo and Flycheck. Also change the directory to be a `PathBuf` as opposed to a `String` to be more appropriate to the operating system.
1 parent c0b5db4 commit ec38198

File tree

4 files changed

+54
-68
lines changed

4 files changed

+54
-68
lines changed

crates/flycheck/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub enum FlycheckConfig {
5050
extra_args: Vec<String>,
5151
extra_env: FxHashMap<String, String>,
5252
ansi_color_output: bool,
53+
target_dir: Option<PathBuf>,
5354
},
5455
CustomCommand {
5556
command: String,
@@ -308,6 +309,7 @@ impl FlycheckActor {
308309
features,
309310
extra_env,
310311
ansi_color_output,
312+
target_dir,
311313
} => {
312314
let mut cmd = Command::new(toolchain::cargo());
313315
cmd.arg(command);
@@ -340,6 +342,9 @@ impl FlycheckActor {
340342
cmd.arg(features.join(" "));
341343
}
342344
}
345+
if let Some(target_dir) = target_dir {
346+
cmd.arg("--target-dir").arg(target_dir);
347+
}
343348
cmd.envs(extra_env);
344349
(cmd, extra_args)
345350
}

crates/project-model/src/build_scripts.rs

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ impl WorkspaceBuildScripts {
7373
cmd.args(["check", "--quiet", "--workspace", "--message-format=json"]);
7474
cmd.args(&config.extra_args);
7575

76+
if let Some(target_dir) = &config.target_dir {
77+
cmd.arg("--target-dir").arg(target_dir);
78+
}
79+
7680
// --all-targets includes tests, benches and examples in addition to the
7781
// default lib and bins. This is an independent concept from the --target
7882
// flag below.

crates/project-model/src/cargo_workspace.rs

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ pub struct CargoConfig {
9696
pub extra_env: FxHashMap<String, String>,
9797
pub invocation_strategy: InvocationStrategy,
9898
pub invocation_location: InvocationLocation,
99+
/// Optional path to use instead of `target` when building
100+
pub target_dir: Option<PathBuf>,
99101
}
100102

101103
pub type Package = Idx<PackageData>;

crates/rust-analyzer/src/config.rs

+43-68
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,6 @@ impl Config {
11991199
}
12001200

12011201
pub fn cargo(&self) -> CargoConfig {
1202-
let target_directory = self.target_dir_from_config();
12031202
let rustc_source = self.data.rustc_source.as_ref().map(|rustc_src| {
12041203
if rustc_src == "discover" {
12051204
RustLibSource::Discover
@@ -1217,10 +1216,6 @@ impl Config {
12171216
let sysroot_src =
12181217
self.data.cargo_sysrootSrc.as_ref().map(|sysroot| self.root_path.join(sysroot));
12191218

1220-
let mut extra_args = self.data.cargo_extraArgs.clone();
1221-
1222-
add_target_dir_to_args(&mut extra_args, target_directory);
1223-
12241219
CargoConfig {
12251220
features: match &self.data.cargo_features {
12261221
CargoFeaturesDef::All => CargoFeatures::All,
@@ -1273,8 +1268,9 @@ impl Config {
12731268
InvocationLocation::Workspace => project_model::InvocationLocation::Workspace,
12741269
},
12751270
run_build_script_command: self.data.cargo_buildScripts_overrideCommand.clone(),
1276-
extra_args,
1271+
extra_args: self.data.cargo_extraArgs.clone(),
12771272
extra_env: self.data.cargo_extraEnv.clone(),
1273+
target_dir: self.target_dir_from_config(),
12781274
}
12791275
}
12801276

@@ -1293,14 +1289,10 @@ impl Config {
12931289
}
12941290

12951291
pub fn flycheck(&self) -> FlycheckConfig {
1296-
let target_directory = self.target_dir_from_config();
1297-
12981292
match &self.data.check_overrideCommand {
12991293
Some(args) if !args.is_empty() => {
13001294
let mut args = args.clone();
13011295
let command = args.remove(0);
1302-
add_target_dir_to_args(&mut args, target_directory);
1303-
13041296
FlycheckConfig::CustomCommand {
13051297
command,
13061298
args,
@@ -1319,54 +1311,50 @@ impl Config {
13191311
},
13201312
}
13211313
}
1322-
Some(_) | None => {
1323-
let mut extra_args = self.check_extra_args();
1324-
add_target_dir_to_args(&mut extra_args, target_directory);
1325-
1326-
FlycheckConfig::CargoCommand {
1327-
command: self.data.check_command.clone(),
1328-
target_triples: self
1329-
.data
1330-
.check_targets
1331-
.clone()
1332-
.and_then(|targets| match &targets.0[..] {
1333-
[] => None,
1334-
targets => Some(targets.into()),
1335-
})
1336-
.unwrap_or_else(|| self.data.cargo_target.clone().into_iter().collect()),
1337-
all_targets: self.data.check_allTargets,
1338-
no_default_features: self
1339-
.data
1340-
.check_noDefaultFeatures
1341-
.unwrap_or(self.data.cargo_noDefaultFeatures),
1342-
all_features: matches!(
1343-
self.data.check_features.as_ref().unwrap_or(&self.data.cargo_features),
1344-
CargoFeaturesDef::All
1345-
),
1346-
features: match self
1347-
.data
1348-
.check_features
1349-
.clone()
1350-
.unwrap_or_else(|| self.data.cargo_features.clone())
1351-
{
1352-
CargoFeaturesDef::All => vec![],
1353-
CargoFeaturesDef::Selected(it) => it,
1354-
},
1355-
extra_args,
1356-
extra_env: self.check_extra_env(),
1357-
ansi_color_output: self.color_diagnostic_output(),
1358-
}
1359-
}
1314+
Some(_) | None => FlycheckConfig::CargoCommand {
1315+
command: self.data.check_command.clone(),
1316+
target_triples: self
1317+
.data
1318+
.check_targets
1319+
.clone()
1320+
.and_then(|targets| match &targets.0[..] {
1321+
[] => None,
1322+
targets => Some(targets.into()),
1323+
})
1324+
.unwrap_or_else(|| self.data.cargo_target.clone().into_iter().collect()),
1325+
all_targets: self.data.check_allTargets,
1326+
no_default_features: self
1327+
.data
1328+
.check_noDefaultFeatures
1329+
.unwrap_or(self.data.cargo_noDefaultFeatures),
1330+
all_features: matches!(
1331+
self.data.check_features.as_ref().unwrap_or(&self.data.cargo_features),
1332+
CargoFeaturesDef::All
1333+
),
1334+
features: match self
1335+
.data
1336+
.check_features
1337+
.clone()
1338+
.unwrap_or_else(|| self.data.cargo_features.clone())
1339+
{
1340+
CargoFeaturesDef::All => vec![],
1341+
CargoFeaturesDef::Selected(it) => it,
1342+
},
1343+
extra_args: self.check_extra_args(),
1344+
extra_env: self.check_extra_env(),
1345+
ansi_color_output: self.color_diagnostic_output(),
1346+
target_dir: self.target_dir_from_config(),
1347+
},
13601348
}
13611349
}
13621350

1363-
fn target_dir_from_config(&self) -> Option<String> {
1351+
fn target_dir_from_config(&self) -> Option<PathBuf> {
13641352
self.data
13651353
.rust_analyzerTargetDir
13661354
.as_ref()
13671355
.map(|target_dir| match target_dir {
13681356
TargetDirectory::UseSubdirectory(yes) if *yes => {
1369-
Some(String::from("target/rust-analyzer"))
1357+
Some(PathBuf::from("target/rust-analyzer"))
13701358
}
13711359
TargetDirectory::UseSubdirectory(_) => None,
13721360
TargetDirectory::Directory(dir) => Some(dir.clone()),
@@ -1725,13 +1713,6 @@ impl Config {
17251713
self.is_visual_studio_code
17261714
}
17271715
}
1728-
1729-
fn add_target_dir_to_args(args: &mut Vec<String>, target_dir: Option<String>) {
1730-
if let Some(target_dir) = target_dir {
1731-
args.push(format!("--target-dir={}", target_dir));
1732-
}
1733-
}
1734-
17351716
// Deserialization definitions
17361717

17371718
macro_rules! create_bool_or_string_de {
@@ -2084,7 +2065,7 @@ pub enum MemoryLayoutHoverRenderKindDef {
20842065
#[serde(untagged)]
20852066
pub enum TargetDirectory {
20862067
UseSubdirectory(bool),
2087-
Directory(String),
2068+
Directory(PathBuf),
20882069
}
20892070

20902071
macro_rules! _config_data {
@@ -2703,9 +2684,8 @@ mod tests {
27032684
}))
27042685
.unwrap();
27052686
assert_eq!(config.data.rust_analyzerTargetDir, None);
2706-
assert_eq!(config.cargo().extra_args.len(), 0);
27072687
assert!(
2708-
matches!(config.flycheck(), FlycheckConfig::CargoCommand { extra_args, .. } if extra_args.is_empty())
2688+
matches!(config.flycheck(), FlycheckConfig::CargoCommand { target_dir, .. } if target_dir == None)
27092689
);
27102690
}
27112691

@@ -2726,12 +2706,8 @@ mod tests {
27262706
config.data.rust_analyzerTargetDir,
27272707
Some(TargetDirectory::UseSubdirectory(true))
27282708
);
2729-
assert_eq!(
2730-
config.cargo().extra_args,
2731-
vec!["--target-dir=target/rust-analyzer".to_string()]
2732-
);
27332709
assert!(
2734-
matches!(config.flycheck(), FlycheckConfig::CargoCommand { extra_args, .. } if extra_args == vec!["--target-dir=target/rust-analyzer".to_string()])
2710+
matches!(config.flycheck(), FlycheckConfig::CargoCommand { target_dir, .. } if target_dir == Some(PathBuf::from("target/rust-analyzer")))
27352711
);
27362712
}
27372713

@@ -2750,11 +2726,10 @@ mod tests {
27502726
.unwrap();
27512727
assert_eq!(
27522728
config.data.rust_analyzerTargetDir,
2753-
Some(TargetDirectory::Directory("other_folder".to_string()))
2729+
Some(TargetDirectory::Directory(PathBuf::from("other_folder")))
27542730
);
2755-
assert_eq!(config.cargo().extra_args, vec!["--target-dir=other_folder".to_string()]);
27562731
assert!(
2757-
matches!(config.flycheck(), FlycheckConfig::CargoCommand { extra_args, .. } if extra_args == vec!["--target-dir=other_folder".to_string()])
2732+
matches!(config.flycheck(), FlycheckConfig::CargoCommand { target_dir, .. } if target_dir == Some(PathBuf::from("other_folder")))
27582733
);
27592734
}
27602735
}

0 commit comments

Comments
 (0)