mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-11-03 13:13:18 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									aeef7b644b
								
							
						
					
					
						commit
						53b6700594
					
				@ -50,6 +50,7 @@ pub enum FlycheckConfig {
 | 
				
			|||||||
        extra_args: Vec<String>,
 | 
					        extra_args: Vec<String>,
 | 
				
			||||||
        extra_env: FxHashMap<String, String>,
 | 
					        extra_env: FxHashMap<String, String>,
 | 
				
			||||||
        ansi_color_output: bool,
 | 
					        ansi_color_output: bool,
 | 
				
			||||||
 | 
					        target_dir: Option<PathBuf>,
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    CustomCommand {
 | 
					    CustomCommand {
 | 
				
			||||||
        command: String,
 | 
					        command: String,
 | 
				
			||||||
@ -308,6 +309,7 @@ impl FlycheckActor {
 | 
				
			|||||||
                features,
 | 
					                features,
 | 
				
			||||||
                extra_env,
 | 
					                extra_env,
 | 
				
			||||||
                ansi_color_output,
 | 
					                ansi_color_output,
 | 
				
			||||||
 | 
					                target_dir,
 | 
				
			||||||
            } => {
 | 
					            } => {
 | 
				
			||||||
                let mut cmd = Command::new(toolchain::cargo());
 | 
					                let mut cmd = Command::new(toolchain::cargo());
 | 
				
			||||||
                cmd.arg(command);
 | 
					                cmd.arg(command);
 | 
				
			||||||
@ -340,6 +342,9 @@ impl FlycheckActor {
 | 
				
			|||||||
                        cmd.arg(features.join(" "));
 | 
					                        cmd.arg(features.join(" "));
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					                if let Some(target_dir) = target_dir {
 | 
				
			||||||
 | 
					                    cmd.arg("--target-dir").arg(target_dir);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
                cmd.envs(extra_env);
 | 
					                cmd.envs(extra_env);
 | 
				
			||||||
                (cmd, extra_args)
 | 
					                (cmd, extra_args)
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
				
			|||||||
@ -73,6 +73,10 @@ impl WorkspaceBuildScripts {
 | 
				
			|||||||
                cmd.args(["check", "--quiet", "--workspace", "--message-format=json"]);
 | 
					                cmd.args(["check", "--quiet", "--workspace", "--message-format=json"]);
 | 
				
			||||||
                cmd.args(&config.extra_args);
 | 
					                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
 | 
					                // --all-targets includes tests, benches and examples in addition to the
 | 
				
			||||||
                // default lib and bins. This is an independent concept from the --target
 | 
					                // default lib and bins. This is an independent concept from the --target
 | 
				
			||||||
                // flag below.
 | 
					                // flag below.
 | 
				
			||||||
 | 
				
			|||||||
@ -96,6 +96,8 @@ pub struct CargoConfig {
 | 
				
			|||||||
    pub extra_env: FxHashMap<String, String>,
 | 
					    pub extra_env: FxHashMap<String, String>,
 | 
				
			||||||
    pub invocation_strategy: InvocationStrategy,
 | 
					    pub invocation_strategy: InvocationStrategy,
 | 
				
			||||||
    pub invocation_location: InvocationLocation,
 | 
					    pub invocation_location: InvocationLocation,
 | 
				
			||||||
 | 
					    /// Optional path to use instead of `target` when building
 | 
				
			||||||
 | 
					    pub target_dir: Option<PathBuf>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub type Package = Idx<PackageData>;
 | 
					pub type Package = Idx<PackageData>;
 | 
				
			||||||
 | 
				
			|||||||
@ -1199,7 +1199,6 @@ impl Config {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn cargo(&self) -> CargoConfig {
 | 
					    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| {
 | 
					        let rustc_source = self.data.rustc_source.as_ref().map(|rustc_src| {
 | 
				
			||||||
            if rustc_src == "discover" {
 | 
					            if rustc_src == "discover" {
 | 
				
			||||||
                RustLibSource::Discover
 | 
					                RustLibSource::Discover
 | 
				
			||||||
@ -1217,10 +1216,6 @@ impl Config {
 | 
				
			|||||||
        let sysroot_src =
 | 
					        let sysroot_src =
 | 
				
			||||||
            self.data.cargo_sysrootSrc.as_ref().map(|sysroot| self.root_path.join(sysroot));
 | 
					            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 {
 | 
					        CargoConfig {
 | 
				
			||||||
            features: match &self.data.cargo_features {
 | 
					            features: match &self.data.cargo_features {
 | 
				
			||||||
                CargoFeaturesDef::All => CargoFeatures::All,
 | 
					                CargoFeaturesDef::All => CargoFeatures::All,
 | 
				
			||||||
@ -1273,8 +1268,9 @@ impl Config {
 | 
				
			|||||||
                InvocationLocation::Workspace => project_model::InvocationLocation::Workspace,
 | 
					                InvocationLocation::Workspace => project_model::InvocationLocation::Workspace,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            run_build_script_command: self.data.cargo_buildScripts_overrideCommand.clone(),
 | 
					            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(),
 | 
					            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 {
 | 
					    pub fn flycheck(&self) -> FlycheckConfig {
 | 
				
			||||||
        let target_directory = self.target_dir_from_config();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        match &self.data.check_overrideCommand {
 | 
					        match &self.data.check_overrideCommand {
 | 
				
			||||||
            Some(args) if !args.is_empty() => {
 | 
					            Some(args) if !args.is_empty() => {
 | 
				
			||||||
                let mut args = args.clone();
 | 
					                let mut args = args.clone();
 | 
				
			||||||
                let command = args.remove(0);
 | 
					                let command = args.remove(0);
 | 
				
			||||||
                add_target_dir_to_args(&mut args, target_directory);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                FlycheckConfig::CustomCommand {
 | 
					                FlycheckConfig::CustomCommand {
 | 
				
			||||||
                    command,
 | 
					                    command,
 | 
				
			||||||
                    args,
 | 
					                    args,
 | 
				
			||||||
@ -1319,54 +1311,50 @@ impl Config {
 | 
				
			|||||||
                    },
 | 
					                    },
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            Some(_) | None => {
 | 
					            Some(_) | None => FlycheckConfig::CargoCommand {
 | 
				
			||||||
                let mut extra_args = self.check_extra_args();
 | 
					                command: self.data.check_command.clone(),
 | 
				
			||||||
                add_target_dir_to_args(&mut extra_args, target_directory);
 | 
					                target_triples: self
 | 
				
			||||||
 | 
					                    .data
 | 
				
			||||||
                FlycheckConfig::CargoCommand {
 | 
					                    .check_targets
 | 
				
			||||||
                    command: self.data.check_command.clone(),
 | 
					                    .clone()
 | 
				
			||||||
                    target_triples: self
 | 
					                    .and_then(|targets| match &targets.0[..] {
 | 
				
			||||||
                        .data
 | 
					                        [] => None,
 | 
				
			||||||
                        .check_targets
 | 
					                        targets => Some(targets.into()),
 | 
				
			||||||
                        .clone()
 | 
					                    })
 | 
				
			||||||
                        .and_then(|targets| match &targets.0[..] {
 | 
					                    .unwrap_or_else(|| self.data.cargo_target.clone().into_iter().collect()),
 | 
				
			||||||
                            [] => None,
 | 
					                all_targets: self.data.check_allTargets,
 | 
				
			||||||
                            targets => Some(targets.into()),
 | 
					                no_default_features: self
 | 
				
			||||||
                        })
 | 
					                    .data
 | 
				
			||||||
                        .unwrap_or_else(|| self.data.cargo_target.clone().into_iter().collect()),
 | 
					                    .check_noDefaultFeatures
 | 
				
			||||||
                    all_targets: self.data.check_allTargets,
 | 
					                    .unwrap_or(self.data.cargo_noDefaultFeatures),
 | 
				
			||||||
                    no_default_features: self
 | 
					                all_features: matches!(
 | 
				
			||||||
                        .data
 | 
					                    self.data.check_features.as_ref().unwrap_or(&self.data.cargo_features),
 | 
				
			||||||
                        .check_noDefaultFeatures
 | 
					                    CargoFeaturesDef::All
 | 
				
			||||||
                        .unwrap_or(self.data.cargo_noDefaultFeatures),
 | 
					                ),
 | 
				
			||||||
                    all_features: matches!(
 | 
					                features: match self
 | 
				
			||||||
                        self.data.check_features.as_ref().unwrap_or(&self.data.cargo_features),
 | 
					                    .data
 | 
				
			||||||
                        CargoFeaturesDef::All
 | 
					                    .check_features
 | 
				
			||||||
                    ),
 | 
					                    .clone()
 | 
				
			||||||
                    features: match self
 | 
					                    .unwrap_or_else(|| self.data.cargo_features.clone())
 | 
				
			||||||
                        .data
 | 
					                {
 | 
				
			||||||
                        .check_features
 | 
					                    CargoFeaturesDef::All => vec![],
 | 
				
			||||||
                        .clone()
 | 
					                    CargoFeaturesDef::Selected(it) => it,
 | 
				
			||||||
                        .unwrap_or_else(|| self.data.cargo_features.clone())
 | 
					                },
 | 
				
			||||||
                    {
 | 
					                extra_args: self.check_extra_args(),
 | 
				
			||||||
                        CargoFeaturesDef::All => vec![],
 | 
					                extra_env: self.check_extra_env(),
 | 
				
			||||||
                        CargoFeaturesDef::Selected(it) => it,
 | 
					                ansi_color_output: self.color_diagnostic_output(),
 | 
				
			||||||
                    },
 | 
					                target_dir: self.target_dir_from_config(),
 | 
				
			||||||
                    extra_args,
 | 
					            },
 | 
				
			||||||
                    extra_env: self.check_extra_env(),
 | 
					 | 
				
			||||||
                    ansi_color_output: self.color_diagnostic_output(),
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn target_dir_from_config(&self) -> Option<String> {
 | 
					    fn target_dir_from_config(&self) -> Option<PathBuf> {
 | 
				
			||||||
        self.data
 | 
					        self.data
 | 
				
			||||||
            .rust_analyzerTargetDir
 | 
					            .rust_analyzerTargetDir
 | 
				
			||||||
            .as_ref()
 | 
					            .as_ref()
 | 
				
			||||||
            .map(|target_dir| match target_dir {
 | 
					            .map(|target_dir| match target_dir {
 | 
				
			||||||
                TargetDirectory::UseSubdirectory(yes) if *yes => {
 | 
					                TargetDirectory::UseSubdirectory(yes) if *yes => {
 | 
				
			||||||
                    Some(String::from("target/rust-analyzer"))
 | 
					                    Some(PathBuf::from("target/rust-analyzer"))
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                TargetDirectory::UseSubdirectory(_) => None,
 | 
					                TargetDirectory::UseSubdirectory(_) => None,
 | 
				
			||||||
                TargetDirectory::Directory(dir) => Some(dir.clone()),
 | 
					                TargetDirectory::Directory(dir) => Some(dir.clone()),
 | 
				
			||||||
@ -1725,13 +1713,6 @@ impl Config {
 | 
				
			|||||||
        self.is_visual_studio_code
 | 
					        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
 | 
					// Deserialization definitions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
macro_rules! create_bool_or_string_de {
 | 
					macro_rules! create_bool_or_string_de {
 | 
				
			||||||
@ -2084,7 +2065,7 @@ pub enum MemoryLayoutHoverRenderKindDef {
 | 
				
			|||||||
#[serde(untagged)]
 | 
					#[serde(untagged)]
 | 
				
			||||||
pub enum TargetDirectory {
 | 
					pub enum TargetDirectory {
 | 
				
			||||||
    UseSubdirectory(bool),
 | 
					    UseSubdirectory(bool),
 | 
				
			||||||
    Directory(String),
 | 
					    Directory(PathBuf),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
macro_rules! _config_data {
 | 
					macro_rules! _config_data {
 | 
				
			||||||
@ -2703,9 +2684,8 @@ mod tests {
 | 
				
			|||||||
            }))
 | 
					            }))
 | 
				
			||||||
            .unwrap();
 | 
					            .unwrap();
 | 
				
			||||||
        assert_eq!(config.data.rust_analyzerTargetDir, None);
 | 
					        assert_eq!(config.data.rust_analyzerTargetDir, None);
 | 
				
			||||||
        assert_eq!(config.cargo().extra_args.len(), 0);
 | 
					 | 
				
			||||||
        assert!(
 | 
					        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,
 | 
					            config.data.rust_analyzerTargetDir,
 | 
				
			||||||
            Some(TargetDirectory::UseSubdirectory(true))
 | 
					            Some(TargetDirectory::UseSubdirectory(true))
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
        assert_eq!(
 | 
					 | 
				
			||||||
            config.cargo().extra_args,
 | 
					 | 
				
			||||||
            vec!["--target-dir=target/rust-analyzer".to_string()]
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
        assert!(
 | 
					        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();
 | 
					            .unwrap();
 | 
				
			||||||
        assert_eq!(
 | 
					        assert_eq!(
 | 
				
			||||||
            config.data.rust_analyzerTargetDir,
 | 
					            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!(
 | 
					        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")))
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user