Load python venv for tasks

This commit is contained in:
Lukas Wirth 2025-08-22 12:06:07 +02:00
parent d2f2142127
commit 333354024d
8 changed files with 236 additions and 115 deletions

View file

@ -141,7 +141,9 @@ impl AgentTool for TerminalTool {
let program = program.await; let program = program.await;
let env = env.await; let env = env.await;
let terminal = self.project.update(cx, |project, cx| { let terminal = self
.project
.update(cx, |project, cx| {
project.create_terminal_task( project.create_terminal_task(
task::SpawnInTerminal { task::SpawnInTerminal {
command: Some(program), command: Some(program),
@ -151,8 +153,16 @@ impl AgentTool for TerminalTool {
..Default::default() ..Default::default()
}, },
cx, cx,
project
.active_entry()
.and_then(|entry_id| project.worktree_id_for_entry(entry_id, cx))
.map(|worktree_id| project::ProjectPath {
worktree_id,
path: Arc::from(Path::new("")),
}),
) )
})??; })?
.await?;
let acp_terminal = cx.new(|cx| { let acp_terminal = cx.new(|cx| {
acp_thread::Terminal::new( acp_thread::Terminal::new(
input.command.clone(), input.command.clone(),

View file

@ -213,7 +213,8 @@ impl Tool for TerminalTool {
async move |cx| { async move |cx| {
let program = program.await; let program = program.await;
let env = env.await; let env = env.await;
project.update(cx, |project, cx| { project
.update(cx, |project, cx| {
project.create_terminal_task( project.create_terminal_task(
task::SpawnInTerminal { task::SpawnInTerminal {
command: Some(program), command: Some(program),
@ -223,8 +224,16 @@ impl Tool for TerminalTool {
..Default::default() ..Default::default()
}, },
cx, cx,
project
.active_entry()
.and_then(|entry_id| project.worktree_id_for_entry(entry_id, cx))
.map(|worktree_id| project::ProjectPath {
worktree_id,
path: Arc::from(Path::new("")),
}),
) )
})? })?
.await
} }
}); });

View file

@ -1018,8 +1018,15 @@ impl RunningState {
project.create_terminal_task( project.create_terminal_task(
task_with_shell.clone(), task_with_shell.clone(),
cx, cx,
project
.active_entry()
.and_then(|entry_id| project.worktree_id_for_entry(entry_id, cx))
.map(|worktree_id| project::ProjectPath {
worktree_id,
path: Arc::from(std::path::Path::new("")),
}),
) )
})??; })?.await?;
let terminal_view = cx.new_window_entity(|window, cx| { let terminal_view = cx.new_window_entity(|window, cx| {
TerminalView::new( TerminalView::new(
@ -1186,10 +1193,21 @@ impl RunningState {
let workspace = self.workspace.clone(); let workspace = self.workspace.clone();
let weak_project = project.downgrade(); let weak_project = project.downgrade();
let terminal_task = let terminal_task = project.update(cx, |project, cx| {
project.update(cx, |project, cx| project.create_terminal_task(kind, cx)); project.create_terminal_task(
kind,
cx,
project
.active_entry()
.and_then(|entry_id| project.worktree_id_for_entry(entry_id, cx))
.map(|worktree_id| project::ProjectPath {
worktree_id,
path: Arc::from(std::path::Path::new("")),
}),
)
});
let terminal_task = cx.spawn_in(window, async move |_, cx| { let terminal_task = cx.spawn_in(window, async move |_, cx| {
let terminal = terminal_task?; let terminal = terminal_task.await?;
let terminal_view = cx.new_window_entity(|window, cx| { let terminal_view = cx.new_window_entity(|window, cx| {
TerminalView::new(terminal.clone(), workspace, None, weak_project, window, cx) TerminalView::new(terminal.clone(), workspace, None, weak_project, window, cx)

View file

@ -28,6 +28,9 @@ pub struct Toolchain {
pub as_json: serde_json::Value, pub as_json: serde_json::Value,
/// shell -> script /// shell -> script
pub activation_script: FxHashMap<ShellKind, String>, pub activation_script: FxHashMap<ShellKind, String>,
// Option<String>
// sh activate -c "user shell -l"
// check if this work with powershell
} }
impl std::hash::Hash for Toolchain { impl std::hash::Hash for Toolchain {

View file

@ -94,7 +94,8 @@ impl Project {
&mut self, &mut self,
spawn_task: SpawnInTerminal, spawn_task: SpawnInTerminal,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) -> Result<Entity<Terminal>> { project_path_context: Option<ProjectPath>,
) -> Task<Result<Entity<Terminal>>> {
let this = &mut *self; let this = &mut *self;
let ssh_details = this.ssh_details(cx); let ssh_details = this.ssh_details(cx);
let path: Option<Arc<Path>> = if let Some(cwd) = &spawn_task.cwd { let path: Option<Arc<Path>> = if let Some(cwd) = &spawn_task.cwd {
@ -135,8 +136,14 @@ impl Project {
env.extend(settings.env); env.extend(settings.env);
let local_path = if is_ssh_terminal { None } else { path.clone() }; let local_path = if is_ssh_terminal { None } else { path.clone() };
let toolchain =
let (spawn_task, shell) = { project_path_context.map(|p| self.active_toolchain(p, LanguageName::new("Python"), cx));
cx.spawn(async move |project, cx| {
let scripts = maybe!(async {
let toolchain = toolchain?.await?;
Some(toolchain.activation_script)
})
.await;
let task_state = Some(TaskState { let task_state = Some(TaskState {
id: spawn_task.id, id: spawn_task.id,
full_label: spawn_task.full_label, full_label: spawn_task.full_label,
@ -150,7 +157,23 @@ impl Project {
completion_rx, completion_rx,
}); });
let shell = {
env.extend(spawn_task.env); env.extend(spawn_task.env);
// todo(lw): Use shell builder
let shell = match &ssh_details {
Some(ssh) => ssh.shell.clone(),
None => match &settings.shell {
Shell::Program(program) => program.clone(),
Shell::WithArguments {
program,
args: _,
title_override: _,
} => program.clone(),
Shell::System => get_system_shell(),
},
};
let shell_kind = ShellKind::new(&shell);
let activation_script = scripts.as_ref().and_then(|it| it.get(&shell_kind));
match ssh_details { match ssh_details {
Some(SshDetails { Some(SshDetails {
@ -163,6 +186,7 @@ impl Project {
log::debug!("Connecting to a remote server: {ssh_command:?}"); log::debug!("Connecting to a remote server: {ssh_command:?}");
env.entry("TERM".to_string()) env.entry("TERM".to_string())
.or_insert_with(|| "xterm-256color".to_string()); .or_insert_with(|| "xterm-256color".to_string());
let (program, args) = wrap_for_ssh( let (program, args) = wrap_for_ssh(
&shell, &shell,
&ssh_command, &ssh_command,
@ -173,23 +197,41 @@ impl Project {
path.as_deref(), path.as_deref(),
env, env,
path_style, path_style,
None, activation_script.map(String::as_str),
); );
env = HashMap::default(); env = HashMap::default();
if let Some(envs) = envs { if let Some(envs) = envs {
env.extend(envs); env.extend(envs);
} }
(
task_state,
Shell::WithArguments { Shell::WithArguments {
program, program,
args, args,
title_override: Some(format!("{} — Terminal", host).into()), title_override: Some(format!("{} — Terminal", host).into()),
}, }
) }
None => match activation_script {
Some(activation_script) => {
let to_run = if let Some(command) = spawn_task.command {
let command: Option<Cow<str>> = shlex::try_quote(&command).ok();
let args = spawn_task
.args
.iter()
.filter_map(|arg| shlex::try_quote(arg).ok());
command.into_iter().chain(args).join(" ")
} else {
format!("exec {shell} -l")
};
Shell::WithArguments {
program: shell,
args: vec![
"-c".to_owned(),
format!("{activation_script}; {to_run}",),
],
title_override: None,
}
} }
None => { None => {
let shell = if let Some(program) = spawn_task.command { if let Some(program) = spawn_task.command {
Shell::WithArguments { Shell::WithArguments {
program, program,
args: spawn_task.args, args: spawn_task.args,
@ -197,14 +239,15 @@ impl Project {
} }
} else { } else {
Shell::System Shell::System
};
(task_state, shell)
} }
} }
},
}
}; };
project.update(cx, move |this, cx| {
TerminalBuilder::new( TerminalBuilder::new(
local_path.map(|path| path.to_path_buf()), local_path.map(|path| path.to_path_buf()),
spawn_task, task_state,
shell, shell,
env, env,
settings.cursor_shape.unwrap_or_default(), settings.cursor_shape.unwrap_or_default(),
@ -239,6 +282,8 @@ impl Project {
terminal_handle terminal_handle
}) })
})?
})
} }
pub fn create_terminal_shell( pub fn create_terminal_shell(
@ -278,6 +323,7 @@ impl Project {
let toolchain = let toolchain =
project_path_context.map(|p| self.active_toolchain(p, LanguageName::new("Python"), cx)); project_path_context.map(|p| self.active_toolchain(p, LanguageName::new("Python"), cx));
cx.spawn(async move |project, cx| { cx.spawn(async move |project, cx| {
// todo(lw): Use shell builder
let shell = match &ssh_details { let shell = match &ssh_details {
Some(ssh) => ssh.shell.clone(), Some(ssh) => ssh.shell.clone(),
None => match &settings.shell { None => match &settings.shell {

View file

@ -95,6 +95,7 @@ pub enum VenvSettings {
/// to the current working directory. We recommend overriding this /// to the current working directory. We recommend overriding this
/// in your project's settings, rather than globally. /// in your project's settings, rather than globally.
activate_script: Option<ActivateScript>, activate_script: Option<ActivateScript>,
// deprecate but use
venv_name: Option<String>, venv_name: Option<String>,
directories: Option<Vec<PathBuf>>, directories: Option<Vec<PathBuf>>,
}, },

View file

@ -592,7 +592,17 @@ impl TerminalPanel {
.workspace .workspace
.update(cx, |workspace, cx| { .update(cx, |workspace, cx| {
Self::add_center_terminal(workspace, window, cx, |project, cx| { Self::add_center_terminal(workspace, window, cx, |project, cx| {
Task::ready(project.create_terminal_task(spawn_task, cx)) project.create_terminal_task(
spawn_task,
cx,
project
.active_entry()
.and_then(|entry_id| project.worktree_id_for_entry(entry_id, cx))
.map(|worktree_id| project::ProjectPath {
worktree_id,
path: Arc::from(Path::new("")),
}),
)
}) })
}) })
.unwrap_or_else(|e| Task::ready(Err(e))), .unwrap_or_else(|e| Task::ready(Err(e))),
@ -731,8 +741,21 @@ impl TerminalPanel {
terminal_panel.active_pane.clone() terminal_panel.active_pane.clone()
})?; })?;
let project = workspace.read_with(cx, |workspace, _| workspace.project().clone())?; let project = workspace.read_with(cx, |workspace, _| workspace.project().clone())?;
let terminal = let terminal = project
project.update(cx, |project, cx| project.create_terminal_task(task, cx))??; .update(cx, |project, cx| {
project.create_terminal_task(
task,
cx,
project
.active_entry()
.and_then(|entry_id| project.worktree_id_for_entry(entry_id, cx))
.map(|worktree_id| project::ProjectPath {
worktree_id,
path: Arc::from(Path::new("")),
}),
)
})?
.await?;
let result = workspace.update_in(cx, |workspace, window, cx| { let result = workspace.update_in(cx, |workspace, window, cx| {
let terminal_view = Box::new(cx.new(|cx| { let terminal_view = Box::new(cx.new(|cx| {
TerminalView::new( TerminalView::new(
@ -909,9 +932,21 @@ impl TerminalPanel {
this.workspace this.workspace
.update(cx, |workspace, _| workspace.project().clone()) .update(cx, |workspace, _| workspace.project().clone())
})??; })??;
let new_terminal = project.update(cx, |project, cx| { let new_terminal = project
project.create_terminal_task(spawn_task, cx) .update(cx, |project, cx| {
})??; project.create_terminal_task(
spawn_task,
cx,
project
.active_entry()
.and_then(|entry_id| project.worktree_id_for_entry(entry_id, cx))
.map(|worktree_id| project::ProjectPath {
worktree_id,
path: Arc::from(Path::new("")),
}),
)
})?
.await?;
terminal_to_replace.update_in(cx, |terminal_to_replace, window, cx| { terminal_to_replace.update_in(cx, |terminal_to_replace, window, cx| {
terminal_to_replace.set_terminal(new_terminal.clone(), window, cx); terminal_to_replace.set_terminal(new_terminal.clone(), window, cx);
})?; })?;

View file

@ -1389,7 +1389,6 @@ impl WorkspaceDb {
relative_path: String, relative_path: String,
language_name: LanguageName, language_name: LanguageName,
) -> Result<Option<Toolchain>> { ) -> Result<Option<Toolchain>> {
return Ok(None);
self.write(move |this| { self.write(move |this| {
let mut select = this let mut select = this
.select_bound(sql!( .select_bound(sql!(
@ -1415,7 +1414,6 @@ impl WorkspaceDb {
&self, &self,
workspace_id: WorkspaceId, workspace_id: WorkspaceId,
) -> Result<Vec<(Toolchain, WorktreeId, Arc<Path>)>> { ) -> Result<Vec<(Toolchain, WorktreeId, Arc<Path>)>> {
return Ok(vec![]);
self.write(move |this| { self.write(move |this| {
let mut select = this let mut select = this
.select_bound(sql!( .select_bound(sql!(
@ -1426,6 +1424,7 @@ impl WorkspaceDb {
let toolchain: Vec<(String, String, u64, String, String, String)> = let toolchain: Vec<(String, String, u64, String, String, String)> =
select(workspace_id)?; select(workspace_id)?;
// todo look into re-serializing these if we fix up
Ok(toolchain.into_iter().filter_map(|(name, path, worktree_id, relative_worktree_path, language_name, raw_json)| Some((Toolchain { Ok(toolchain.into_iter().filter_map(|(name, path, worktree_id, relative_worktree_path, language_name, raw_json)| Some((Toolchain {
name: name.into(), name: name.into(),
path: path.into(), path: path.into(),