Skip to content

Commit 8773ee7

Browse files
committed
fix: in ui Run, avoid subshell execution
This may solve a problem on windows with the `--data` argument and quoting. It also avoids a powershell/sh/bash choice. Signed-off-by: Nick Mitchell <[email protected]>
1 parent 961475e commit 8773ee7

File tree

1 file changed

+30
-33
lines changed
  • pdl-live-react/src-tauri/src/cli

1 file changed

+30
-33
lines changed

pdl-live-react/src-tauri/src/cli/run.rs

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1+
use ::file_diff::diff;
2+
use ::std::fs::{copy, create_dir_all};
3+
use ::std::path::PathBuf;
14
use duct::cmd;
2-
use file_diff::diff;
3-
use std::fs::{copy, create_dir_all};
45

56
use tauri::path::BaseDirectory;
67
use tauri::Manager;
78

89
#[cfg(desktop)]
9-
fn pip_install_if_needed(app_handle: tauri::AppHandle) -> Result<String, tauri::Error> {
10+
fn pip_install_if_needed(app_handle: tauri::AppHandle) -> Result<PathBuf, tauri::Error> {
1011
let cache_path = app_handle.path().cache_dir()?.join("pdl");
1112

1213
create_dir_all(&cache_path)?;
1314
let venv_path = cache_path.join("interpreter-python");
14-
let activate_path0 = if cfg!(windows) {
15+
let activate_path = if cfg!(windows) {
1516
venv_path.join("Scripts").join("Activate.ps1")
1617
} else {
1718
venv_path.join("bin/activate")
1819
};
19-
let activate_path = activate_path0.into_os_string().into_string().unwrap();
20+
let activate_path_str = activate_path
21+
.clone()
22+
.into_os_string()
23+
.into_string()
24+
.unwrap();
2025
let cached_requirements_path = venv_path
2126
.join("requirements.txt")
2227
.into_os_string()
@@ -54,13 +59,13 @@ fn pip_install_if_needed(app_handle: tauri::AppHandle) -> Result<String, tauri::
5459
if cfg!(windows) {
5560
format!(
5661
"{activate} ; pip install -r '{requirements}'",
57-
activate = activate_path,
62+
activate = activate_path_str,
5863
requirements = requirements_path
5964
)
6065
} else {
6166
format!(
6267
"source '{activate}' && pip install -r '{requirements}'",
63-
activate = activate_path,
68+
activate = activate_path_str,
6469
requirements = requirements_path
6570
)
6671
}
@@ -76,13 +81,13 @@ fn pip_install_if_needed(app_handle: tauri::AppHandle) -> Result<String, tauri::
7681
if cfg!(windows) {
7782
format!(
7883
"{activate} ; pip install -r '{requirements}'",
79-
activate = activate_path,
84+
activate = activate_path_str,
8085
requirements = requirements_path
8186
)
8287
} else {
8388
format!(
8489
"source '{activate}' && pip install -r '{requirements}'",
85-
activate = activate_path,
90+
activate = activate_path_str,
8691
requirements = requirements_path
8792
)
8893
}
@@ -93,7 +98,10 @@ fn pip_install_if_needed(app_handle: tauri::AppHandle) -> Result<String, tauri::
9398
copy(requirements_path, cached_requirements_path)?;
9499
}
95100

96-
Ok(activate_path)
101+
match activate_path.parent() {
102+
Some(parent) => Ok(parent.to_path_buf()),
103+
_ => Err(tauri::Error::UnknownPath),
104+
}
97105
}
98106

99107
#[cfg(desktop)]
@@ -105,10 +113,10 @@ pub fn run_pdl_program(
105113
stream: Option<&tauri_plugin_cli::ArgData>,
106114
) -> Result<(), tauri::Error> {
107115
println!("Running {:?}", source_file_path);
108-
let activate = pip_install_if_needed(app_handle)?;
116+
let bin_path = pip_install_if_needed(app_handle)?;
109117
let trace_arg = if let Some(arg) = trace_file {
110118
if let serde_json::Value::String(f) = &arg.value {
111-
"--trace ".to_owned() + f
119+
"--trace=".to_owned() + f
112120
} else {
113121
"".to_owned()
114122
}
@@ -118,7 +126,7 @@ pub fn run_pdl_program(
118126

119127
let data_arg = if let Some(arg) = data {
120128
if let serde_json::Value::String(s) = &arg.value {
121-
format!("--data '{}'", s)
129+
format!("--data={}", s)
122130
} else {
123131
"".to_owned()
124132
}
@@ -128,33 +136,22 @@ pub fn run_pdl_program(
128136

129137
let stream_arg = if let Some(arg) = stream {
130138
if let serde_json::Value::String(s) = &arg.value {
131-
"--stream ".to_owned() + s
139+
"--stream=".to_owned() + s
132140
} else {
133141
"".to_owned()
134142
}
135143
} else {
136144
"".to_owned()
137145
};
138146

139-
cmd!(
140-
if cfg!(windows) { "powershell" } else { "sh" },
141-
if cfg!(windows) {
142-
"invoke-expression"
143-
} else {
144-
"-c"
145-
},
146-
&[
147-
if cfg!(windows) { "" } else { "source" },
148-
activate.as_str(),
149-
"; pdl",
150-
trace_arg.as_str(),
151-
data_arg.as_str(),
152-
stream_arg.as_str(),
153-
source_file_path.as_str(),
154-
]
155-
.join(" "),
156-
)
157-
.run()?;
147+
let mut args = vec![
148+
source_file_path.as_str(),
149+
trace_arg.as_str(),
150+
data_arg.as_str(),
151+
stream_arg.as_str(),
152+
];
153+
args.retain(|x| x.chars().count() > 0);
154+
cmd(bin_path.join("pdl"), &args).run()?;
158155

159156
Ok(())
160157
}

0 commit comments

Comments
 (0)