Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix Rust task variables for files outside Cargo projects (#65684)
Yann Roberto2be002692fd0
diff --git a/crates/languages/src/rust.rs b/crates/languages/src/rust.rsindex 320b464d848..57865f3df42 100644--- a/crates/languages/src/rust.rs+++ b/crates/languages/src/rust.rs@@ -1274,16 +1274,19 @@ async fn target_info_from_abs_path(abs_path: &Path,project_env: Option<&HashMap<String, String>>,) -> Result<Option<(Option<TargetInfo>, Arc<Path>)>> {+ let directory = abs_path+ .parent()+ .ok_or_else(|| anyhow::anyhow!("failed to get parent directory"))?;+ if !has_cargo_manifest(directory).await? {+ return Ok(None);+ }+let mut command = util::command::new_command("cargo");if let Some(envs) = project_env {command.envs(envs);}let output = command- .current_dir(- abs_path- .parent()- .ok_or_else(|| anyhow::anyhow!("failed to get parent directory"))?,- )+ .current_dir(directory).arg("metadata").arg("--no-deps").arg("--format-version")@@ -1300,6 +1303,18 @@ async fn target_info_from_abs_path(Ok(target_info_from_metadata(metadata, abs_path))}+async fn has_cargo_manifest(directory: &Path) -> Result<bool> {+ let resolved_directory = fs::canonicalize(directory).await?;+ for ancestor in resolved_directory.ancestors() {+ match fs::metadata(ancestor.join("Cargo.toml")).await {+ Ok(_) => return Ok(true),+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}+ Err(error) => return Err(error.into()),+ }+ }+ Ok(false)+}+fn target_info_from_metadata(metadata: CargoMetadata,abs_path: &Path,@@ -1484,7 +1499,7 @@ mod tests {use pretty_assertions::assert_eq;use settings::SettingsStore;use theme::SyntaxTheme;- use util::path;+ use util::{path, paths::SanitizedPath};#[gpui::test]async fn test_process_rust_diagnostics() {@@ -2349,6 +2364,67 @@ mod tests {}}+ #[test]+ fn target_info_from_abs_path_without_manifest() -> Result<()> {+ let temporary_directory = tempfile::tempdir()?;+ let project_root = temporary_directory.path().canonicalize()?;+ let project_root = SanitizedPath::new(&project_root);++ for relative_path in ["main.rs", "nested/src/main.rs"] {+ let source_path = project_root.join(relative_path);+ std::fs::create_dir_all(source_path.parent().context("source has no parent")?)?;+ std::fs::write(&source_path, "fn main() {}")?;++ assert_eq!(+ smol::block_on(target_info_from_abs_path(&source_path, None))?,+ None+ );+ }++ Ok(())+ }++ #[test]+ fn target_info_from_abs_path_with_ancestor_manifest() -> Result<()> {+ let temporary_directory = tempfile::tempdir()?;+ let project_root = temporary_directory.path().canonicalize()?;+ let project_root = SanitizedPath::new(&project_root);+ let source_directory = project_root.join("src/nested");+ let source_path = source_directory.join("main.rs");+ std::fs::create_dir_all(&source_directory)?;+ std::fs::write(&source_path, "fn main() {}")?;+ std::fs::write(+ project_root.join("Cargo.toml"),+ r#"+[package]+name = "task-context-package"+version = "0.1.0"+edition = "2021"++[[bin]]+name = "task-context-bin"+path = "src/nested/main.rs"++[workspace]+"#,+ )?;++ assert_eq!(+ smol::block_on(target_info_from_abs_path(&source_path, None))?,+ Some((+ Some(TargetInfo {+ package_name: "task-context-package".into(),+ target_name: "task-context-bin".into(),+ target_kind: TargetKind::Bin,+ required_features: Vec::new(),+ }),+ Arc::from(project_root.as_path()),+ ))+ );++ Ok(())+ }+#[test]fn target_info_from_abs_path_failed() {let project_root = tempfile::tempdir().unwrap();