Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix agent servers loading environment from home dir instead of project dir (#52763)
Derek Parkerf7ab90721616
diff --git a/crates/project/src/agent_server_store.rs b/crates/project/src/agent_server_store.rsindex 2b7a0c4e1c7..0b6bb2b739f 100644--- a/crates/project/src/agent_server_store.rs+++ b/crates/project/src/agent_server_store.rs@@ -22,7 +22,6 @@ use schemars::JsonSchema;use serde::{Deserialize, Serialize};use settings::{RegisterSetting, SettingsStore};use sha2::{Digest, Sha256};-use task::Shell;use url::Url;use util::{ResultExt as _, debug_panic};@@ -1183,11 +1182,7 @@ impl ExternalAgentServer for LocalExtensionArchiveAgent {// Get project environmentlet mut env = project_environment.update(cx, |project_environment, cx| {- project_environment.local_directory_environment(- &Shell::System,- paths::home_dir().as_path().into(),- cx,- )+ project_environment.default_environment(cx)})?.await.unwrap_or_default();@@ -1377,11 +1372,7 @@ impl ExternalAgentServer for LocalRegistryArchiveAgent {cx.spawn(async move |cx| {let mut env = project_environment.update(cx, |project_environment, cx| {- project_environment.local_directory_environment(- &Shell::System,- paths::home_dir().as_path().into(),- cx,- )+ project_environment.default_environment(cx)})?.await.unwrap_or_default();@@ -1555,11 +1546,7 @@ impl ExternalAgentServer for LocalRegistryNpxAgent {cx.spawn(async move |cx| {let mut env = project_environment.update(cx, |project_environment, cx| {- project_environment.local_directory_environment(- &Shell::System,- paths::home_dir().as_path().into(),- cx,- )+ project_environment.default_environment(cx)})?.await.unwrap_or_default();@@ -1615,11 +1602,7 @@ impl ExternalAgentServer for LocalCustomAgent {cx.spawn(async move |cx| {let mut env = project_environment.update(cx, |project_environment, cx| {- project_environment.local_directory_environment(- &Shell::System,- paths::home_dir().as_path().into(),- cx,- )+ project_environment.default_environment(cx)})?.await.unwrap_or_default();diff --git a/crates/project/src/environment.rs b/crates/project/src/environment.rsindex 6a7f0311d04..8156e172b91 100644--- a/crates/project/src/environment.rs+++ b/crates/project/src/environment.rs@@ -194,6 +194,27 @@ impl ProjectEnvironment {.unwrap_or_else(|| Task::ready(None).shared())}+ /// Returns the project environment using the default worktree path.+ /// This ensures that project-specific environment variables (e.g. from `.envrc`)+ /// are loaded from the project directory rather than the home directory.+ pub fn default_environment(+ &mut self,+ cx: &mut App,+ ) -> Shared<Task<Option<HashMap<String, String>>>> {+ let abs_path = self+ .worktree_store+ .read_with(cx, |worktree_store, cx| {+ crate::Project::default_visible_worktree_paths(worktree_store, cx)+ .into_iter()+ .next()+ })+ .ok()+ .flatten()+ .map(|path| Arc::<Path>::from(path))+ .unwrap_or_else(|| paths::home_dir().as_path().into());+ self.local_directory_environment(&Shell::System, abs_path, cx)+ }+/// Returns the project environment, if possible./// If the project was opened from the CLI, then the inherited CLI environment is returned./// If it wasn't opened from the CLI, and an absolute path is given, then a shell is spawned in