Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/languages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ tree-sitter-typescript = { workspace = true, optional = true }
tree-sitter-yaml = { workspace = true, optional = true }
util.workspace = true
workspace-hack.workspace = true
shlex.workspace = true

[dev-dependencies]
pretty_assertions.workspace = true
Expand Down
13 changes: 2 additions & 11 deletions crates/languages/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,15 +1180,7 @@ impl ToolchainLister for PythonToolchainProvider {
}
Some(PythonEnvironmentKind::Venv | PythonEnvironmentKind::VirtualEnv) => {
if let Some(prefix) = &toolchain.prefix {
let activate_keyword = match shell {
ShellKind::Cmd => ".",
ShellKind::Nushell => "overlay use",
ShellKind::PowerShell => ".",
ShellKind::Fish => "source",
ShellKind::Csh => "source",
ShellKind::Tcsh => "source",
ShellKind::Posix | ShellKind::Rc => "source",
};
let activate_keyword = shell.activate_keyword();
let activate_script_name = match shell {
ShellKind::Posix | ShellKind::Rc => "activate",
ShellKind::Csh => "activate.csh",
Expand All @@ -1200,8 +1192,7 @@ impl ToolchainLister for PythonToolchainProvider {
};
let path = prefix.join(BINARY_DIR).join(activate_script_name);

if let Ok(quoted) =
shlex::try_quote(&path.to_string_lossy()).map(Cow::into_owned)
if let Some(quoted) = shell.try_quote(&path.to_string_lossy())
&& fs.is_file(&path).await
{
activation_script.push(format!("{activate_keyword} {quoted}"));
Expand Down
15 changes: 12 additions & 3 deletions crates/project/src/terminals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,24 @@ impl Project {
},
None => match activation_script.clone() {
activation_script if !activation_script.is_empty() => {
let activation_script = activation_script.join("; ");
let separator = shell_kind.sequential_commands_separator();
let activation_script =
activation_script.join(&format!("{separator} "));
let to_run = format_to_run();

let arg = format!("{activation_script}; {to_run}");
let mut arg = format!("{activation_script}{separator} {to_run}");
if shell_kind == ShellKind::Cmd {
// We need to put the entire command in quotes since otherwise CMD tries to execute them
// as separate commands rather than chaining one after another.
arg = format!("\"{arg}\"");
Comment on lines +211 to +213
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ShellBuilder itself is probably prone to doing this incorrectly as well then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a good catch - confirmed that ' does not work with CMD. I tweaked ShellBuilder to also use double quotes.

}

let args = shell_kind.args_for_shell(false, arg);

(
Shell::WithArguments {
program: shell,
args: vec!["-c".to_owned(), arg],
args,
title_override: None,
},
env,
Expand Down
2 changes: 1 addition & 1 deletion crates/task/src/shell_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl ShellBuilder {
format!("{} -C '{}'", self.program, command_to_use_in_label)
}
ShellKind::Cmd => {
format!("{} /C '{}'", self.program, command_to_use_in_label)
format!("{} /C \"{}\"", self.program, command_to_use_in_label)
}
ShellKind::Posix
| ShellKind::Nushell
Expand Down
9 changes: 9 additions & 0 deletions crates/task/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,22 @@ impl Shell {
Shell::System => get_system_shell(),
}
}

pub fn program_and_args(&self) -> (String, &[String]) {
match self {
Shell::Program(program) => (program.clone(), &[]),
Shell::WithArguments { program, args, .. } => (program.clone(), args),
Shell::System => (get_system_shell(), &[]),
}
}

pub fn shell_kind(&self) -> ShellKind {
match self {
Shell::Program(program) => ShellKind::new(program),
Shell::WithArguments { program, .. } => ShellKind::new(program),
Shell::System => ShellKind::system(),
}
}
}

type VsCodeEnvVariable = String;
Expand Down
5 changes: 4 additions & 1 deletion crates/terminal/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ impl TerminalBuilder {
events_rx,
})
}

pub fn new(
working_directory: Option<PathBuf>,
task: Option<TaskState>,
Expand Down Expand Up @@ -507,8 +508,10 @@ impl TerminalBuilder {
working_directory: working_directory.clone(),
drain_on_exit: true,
env: env.clone().into_iter().collect(),
// We do not want to escape arguments if we are using CMD as our shell.
// If we do we end up with too many quotes/escaped quotes for CMD to handle.
#[cfg(windows)]
escape_args: true,
escape_args: shell.shell_kind() != util::shell::ShellKind::Cmd,
}
};

Expand Down
21 changes: 20 additions & 1 deletion crates/util/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,21 @@ impl ShellKind {
}
}

pub fn command_prefix(&self) -> Option<char> {
pub const fn command_prefix(&self) -> Option<char> {
match self {
ShellKind::PowerShell => Some('&'),
ShellKind::Nushell => Some('^'),
_ => None,
}
}

pub const fn sequential_commands_separator(&self) -> char {
match self {
ShellKind::Cmd => '&',
_ => ';',
}
}

pub fn try_quote<'a>(&self, arg: &'a str) -> Option<Cow<'a, str>> {
shlex::try_quote(arg).ok().map(|arg| match self {
// If we are running in PowerShell, we want to take extra care when escaping strings.
Expand All @@ -370,4 +377,16 @@ impl ShellKind {
_ => arg,
})
}

pub const fn activate_keyword(&self) -> &'static str {
match self {
ShellKind::Cmd => "",
ShellKind::Nushell => "overlay use",
ShellKind::PowerShell => ".",
ShellKind::Fish => "source",
ShellKind::Csh => "source",
ShellKind::Tcsh => "source",
ShellKind::Posix | ShellKind::Rc => "source",
}
}
}
Loading