@@ -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 environment
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();
@@ -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();
@@ -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