about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVictor Song <vms2@rice.edu>2023-09-30 21:02:12 -0500
committerVictor Song <vms2@rice.edu>2023-10-09 02:15:05 -0500
commit53b670059456ba15e4374e5ab7ba4e2468066d0c (patch)
treea2442735791363dfbc406c406860bf63736f1e07
parentaeef7b644b135c80c2fc7a28954d97194df7fabc (diff)
downloadrust-53b670059456ba15e4374e5ab7ba4e2468066d0c.tar.gz
rust-53b670059456ba15e4374e5ab7ba4e2468066d0c.zip
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.
-rw-r--r--crates/flycheck/src/lib.rs5
-rw-r--r--crates/project-model/src/build_scripts.rs4
-rw-r--r--crates/project-model/src/cargo_workspace.rs2
-rw-r--r--crates/rust-analyzer/src/config.rs111
4 files changed, 54 insertions, 68 deletions
diff --git a/crates/flycheck/src/lib.rs b/crates/flycheck/src/lib.rs
index 2de719af92c..0749d91eb32 100644
--- a/crates/flycheck/src/lib.rs
+++ b/crates/flycheck/src/lib.rs
@@ -50,6 +50,7 @@ pub enum FlycheckConfig {
         extra_args: Vec<String>,
         extra_env: FxHashMap<String, String>,
         ansi_color_output: bool,
+        target_dir: Option<PathBuf>,
     },
     CustomCommand {
         command: String,
@@ -308,6 +309,7 @@ impl FlycheckActor {
                 features,
                 extra_env,
                 ansi_color_output,
+                target_dir,
             } => {
                 let mut cmd = Command::new(toolchain::cargo());
                 cmd.arg(command);
@@ -340,6 +342,9 @@ impl FlycheckActor {
                         cmd.arg(features.join(" "));
                     }
                 }
+                if let Some(target_dir) = target_dir {
+                    cmd.arg("--target-dir").arg(target_dir);
+                }
                 cmd.envs(extra_env);
                 (cmd, extra_args)
             }
diff --git a/crates/project-model/src/build_scripts.rs b/crates/project-model/src/build_scripts.rs
index fb0f3ab7d17..68cd40c040b 100644
--- a/crates/project-model/src/build_scripts.rs
+++ b/crates/project-model/src/build_scripts.rs
@@ -73,6 +73,10 @@ impl WorkspaceBuildScripts {
                 cmd.args(["check", "--quiet", "--workspace", "--message-format=json"]);
                 cmd.args(&config.extra_args);
 
+                if let Some(target_dir) = &config.target_dir {
+                    cmd.arg("--target-dir").arg(target_dir);
+                }
+
                 // --all-targets includes tests, benches and examples in addition to the
                 // default lib and bins. This is an independent concept from the --target
                 // flag below.
diff --git a/crates/project-model/src/cargo_workspace.rs b/crates/project-model/src/cargo_workspace.rs
index e47808a2cc9..ca3d6e0596c 100644
--- a/crates/project-model/src/cargo_workspace.rs
+++ b/crates/project-model/src/cargo_workspace.rs
@@ -96,6 +96,8 @@ pub struct CargoConfig {
     pub extra_env: FxHashMap<String, String>,
     pub invocation_strategy: InvocationStrategy,
     pub invocation_location: InvocationLocation,
+    /// Optional path to use instead of `target` when building
+    pub target_dir: Option<PathBuf>,
 }
 
 pub type Package = Idx<PackageData>;
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index 3cd6fa49b20..8f1543de004 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -1199,7 +1199,6 @@ impl Config {
     }
 
     pub fn cargo(&self) -> CargoConfig {
-        let target_directory = self.target_dir_from_config();
         let rustc_source = self.data.rustc_source.as_ref().map(|rustc_src| {
             if rustc_src == "discover" {
                 RustLibSource::Discover
@@ -1217,10 +1216,6 @@ impl Config {
         let sysroot_src =
             self.data.cargo_sysrootSrc.as_ref().map(|sysroot| self.root_path.join(sysroot));
 
-        let mut extra_args = self.data.cargo_extraArgs.clone();
-
-        add_target_dir_to_args(&mut extra_args, target_directory);
-
         CargoConfig {
             features: match &self.data.cargo_features {
                 CargoFeaturesDef::All => CargoFeatures::All,
@@ -1273,8 +1268,9 @@ impl Config {
                 InvocationLocation::Workspace => project_model::InvocationLocation::Workspace,
             },
             run_build_script_command: self.data.cargo_buildScripts_overrideCommand.clone(),
-            extra_args,
+            extra_args: self.data.cargo_extraArgs.clone(),
             extra_env: self.data.cargo_extraEnv.clone(),
+            target_dir: self.target_dir_from_config(),
         }
     }
 
@@ -1293,14 +1289,10 @@ impl Config {
     }
 
     pub fn flycheck(&self) -> FlycheckConfig {
-        let target_directory = self.target_dir_from_config();
-
         match &self.data.check_overrideCommand {
             Some(args) if !args.is_empty() => {
                 let mut args = args.clone();
                 let command = args.remove(0);
-                add_target_dir_to_args(&mut args, target_directory);
-
                 FlycheckConfig::CustomCommand {
                     command,
                     args,
@@ -1319,54 +1311,50 @@ impl Config {
                     },
                 }
             }
-            Some(_) | None => {
-                let mut extra_args = self.check_extra_args();
-                add_target_dir_to_args(&mut extra_args, target_directory);
-
-                FlycheckConfig::CargoCommand {
-                    command: self.data.check_command.clone(),
-                    target_triples: self
-                        .data
-                        .check_targets
-                        .clone()
-                        .and_then(|targets| match &targets.0[..] {
-                            [] => None,
-                            targets => Some(targets.into()),
-                        })
-                        .unwrap_or_else(|| self.data.cargo_target.clone().into_iter().collect()),
-                    all_targets: self.data.check_allTargets,
-                    no_default_features: self
-                        .data
-                        .check_noDefaultFeatures
-                        .unwrap_or(self.data.cargo_noDefaultFeatures),
-                    all_features: matches!(
-                        self.data.check_features.as_ref().unwrap_or(&self.data.cargo_features),
-                        CargoFeaturesDef::All
-                    ),
-                    features: match self
-                        .data
-                        .check_features
-                        .clone()
-                        .unwrap_or_else(|| self.data.cargo_features.clone())
-                    {
-                        CargoFeaturesDef::All => vec![],
-                        CargoFeaturesDef::Selected(it) => it,
-                    },
-                    extra_args,
-                    extra_env: self.check_extra_env(),
-                    ansi_color_output: self.color_diagnostic_output(),
-                }
-            }
+            Some(_) | None => FlycheckConfig::CargoCommand {
+                command: self.data.check_command.clone(),
+                target_triples: self
+                    .data
+                    .check_targets
+                    .clone()
+                    .and_then(|targets| match &targets.0[..] {
+                        [] => None,
+                        targets => Some(targets.into()),
+                    })
+                    .unwrap_or_else(|| self.data.cargo_target.clone().into_iter().collect()),
+                all_targets: self.data.check_allTargets,
+                no_default_features: self
+                    .data
+                    .check_noDefaultFeatures
+                    .unwrap_or(self.data.cargo_noDefaultFeatures),
+                all_features: matches!(
+                    self.data.check_features.as_ref().unwrap_or(&self.data.cargo_features),
+                    CargoFeaturesDef::All
+                ),
+                features: match self
+                    .data
+                    .check_features
+                    .clone()
+                    .unwrap_or_else(|| self.data.cargo_features.clone())
+                {
+                    CargoFeaturesDef::All => vec![],
+                    CargoFeaturesDef::Selected(it) => it,
+                },
+                extra_args: self.check_extra_args(),
+                extra_env: self.check_extra_env(),
+                ansi_color_output: self.color_diagnostic_output(),
+                target_dir: self.target_dir_from_config(),
+            },
         }
     }
 
-    fn target_dir_from_config(&self) -> Option<String> {
+    fn target_dir_from_config(&self) -> Option<PathBuf> {
         self.data
             .rust_analyzerTargetDir
             .as_ref()
             .map(|target_dir| match target_dir {
                 TargetDirectory::UseSubdirectory(yes) if *yes => {
-                    Some(String::from("target/rust-analyzer"))
+                    Some(PathBuf::from("target/rust-analyzer"))
                 }
                 TargetDirectory::UseSubdirectory(_) => None,
                 TargetDirectory::Directory(dir) => Some(dir.clone()),
@@ -1725,13 +1713,6 @@ impl Config {
         self.is_visual_studio_code
     }
 }
-
-fn add_target_dir_to_args(args: &mut Vec<String>, target_dir: Option<String>) {
-    if let Some(target_dir) = target_dir {
-        args.push(format!("--target-dir={}", target_dir));
-    }
-}
-
 // Deserialization definitions
 
 macro_rules! create_bool_or_string_de {
@@ -2084,7 +2065,7 @@ pub enum MemoryLayoutHoverRenderKindDef {
 #[serde(untagged)]
 pub enum TargetDirectory {
     UseSubdirectory(bool),
-    Directory(String),
+    Directory(PathBuf),
 }
 
 macro_rules! _config_data {
@@ -2703,9 +2684,8 @@ mod tests {
             }))
             .unwrap();
         assert_eq!(config.data.rust_analyzerTargetDir, None);
-        assert_eq!(config.cargo().extra_args.len(), 0);
         assert!(
-            matches!(config.flycheck(), FlycheckConfig::CargoCommand { extra_args, .. } if extra_args.is_empty())
+            matches!(config.flycheck(), FlycheckConfig::CargoCommand { target_dir, .. } if target_dir == None)
         );
     }
 
@@ -2726,12 +2706,8 @@ mod tests {
             config.data.rust_analyzerTargetDir,
             Some(TargetDirectory::UseSubdirectory(true))
         );
-        assert_eq!(
-            config.cargo().extra_args,
-            vec!["--target-dir=target/rust-analyzer".to_string()]
-        );
         assert!(
-            matches!(config.flycheck(), FlycheckConfig::CargoCommand { extra_args, .. } if extra_args == vec!["--target-dir=target/rust-analyzer".to_string()])
+            matches!(config.flycheck(), FlycheckConfig::CargoCommand { target_dir, .. } if target_dir == Some(PathBuf::from("target/rust-analyzer")))
         );
     }
 
@@ -2750,11 +2726,10 @@ mod tests {
             .unwrap();
         assert_eq!(
             config.data.rust_analyzerTargetDir,
-            Some(TargetDirectory::Directory("other_folder".to_string()))
+            Some(TargetDirectory::Directory(PathBuf::from("other_folder")))
         );
-        assert_eq!(config.cargo().extra_args, vec!["--target-dir=other_folder".to_string()]);
         assert!(
-            matches!(config.flycheck(), FlycheckConfig::CargoCommand { extra_args, .. } if extra_args == vec!["--target-dir=other_folder".to_string()])
+            matches!(config.flycheck(), FlycheckConfig::CargoCommand { target_dir, .. } if target_dir == Some(PathBuf::from("other_folder")))
         );
     }
 }