tasks: Use environment variables from project (#15266)

This fixes #12125 and addresses what's described in here:

-
https://github.com/zed-industries/zed/issues/4977#issuecomment-2162094388

Before the changes in this PR, when running tasks, they inherited the
Zed process environment, but that might not be the process environment
that you'd get if you `cd` into a project directory.

We already ran into that problem with language servers and we fixed it
by loading the shell environment in the context of a projects root
directory and then passing that to the language servers when starting
them (or when looking for their binaries).

What the change here does is to add the behavior for tasks too: we use
the project-environment as the base environment with which to spawn
tasks. Everything else still works the same, except that the base env is
different.

Release Notes:

- Improved the environment-variable detection when running tasks so that
tasks can now access environment variables as if the task had been
spawned in a terminal that `cd`ed into a project directory. That means
environment variables set by `direnv`/`asdf`/`mise` and other tools are
now picked up.
([#12125](https://github.com/zed-industries/zed/issues/12125)).

Demo:


https://github.com/user-attachments/assets/8bfcc98f-0f9b-4439-b0d9-298aef1a3efe
This commit is contained in:
Thorsten Ball 2024-07-26 18:19:53 +02:00 committed by GitHub
parent 5e04753d1c
commit 0360cda543
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 205 additions and 34 deletions

View file

@ -180,7 +180,7 @@ fn active_item_selection_properties(
#[cfg(test)]
mod tests {
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};
use editor::Editor;
use gpui::{Entity, TestAppContext};
@ -306,7 +306,8 @@ mod tests {
(VariableName::WorktreeRoot, "/dir".into()),
(VariableName::Row, "1".into()),
(VariableName::Column, "1".into()),
])
]),
project_env: HashMap::default(),
}
);
@ -332,7 +333,8 @@ mod tests {
(VariableName::Column, "15".into()),
(VariableName::SelectedText, "is_i".into()),
(VariableName::Symbol, "this_is_a_rust_file".into()),
])
]),
project_env: HashMap::default(),
}
);
@ -356,7 +358,8 @@ mod tests {
(VariableName::Row, "1".into()),
(VariableName::Column, "1".into()),
(VariableName::Symbol, "this_is_a_test".into()),
])
]),
project_env: HashMap::default(),
}
);
}