Skip to content

Commit c950a4e

Browse files
HenkPoleyGideonBear
authored andcommitted
fix(wsl): preserve bash command arguments
WSL reconstructs the Linux command line and does not reliably pass arguments appended after a bash -lc script as positional parameters. This left the topgrade path in $2 empty and produced exec: : not found. Build one safely shell-quoted script, continue forwarding verbose and assume-yes flags, and add a regression test covering spaces and single quotes. Fixes: #2184
1 parent 6ac7aa3 commit c950a4e

1 file changed

Lines changed: 35 additions & 16 deletions

File tree

‎src/steps/os/windows.rs‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,26 +134,32 @@ fn upgrade_wsl_distribution(wsl: &Path, dist: &str, ctx: &ExecutionContext) -> R
134134
.trim_end()
135135
.to_owned();
136136

137+
// WSL joins the arguments following the Linux command into a command line,
138+
// so arguments appended after the `bash -lc` script are not reliably passed
139+
// through as Bash positional parameters. Keep the complete script in one
140+
// argument and shell-quote the values that originated outside this function.
141+
let script = wsl_topgrade_script(dist, &topgrade, ctx.config().verbose(), ctx.config().yes(Step::Wsl));
142+
137143
ctx.execute(wsl)
138-
// Pass the distro name, discovered topgrade path, and forwarded flags as
139-
// positional parameters so the inner `bash -lc` receives them as single argv
140-
// elements instead of parsing their contents. `"${@:3}"` forwards the flags
141-
// to `topgrade` (not `bash`) and expands to nothing when there are none.
142-
.args([
143-
"-d",
144-
dist,
145-
"bash",
146-
"-lc",
147-
r#"TOPGRADE_PREFIX="$1" exec "$2" "${@:3}""#,
148-
"bash",
149-
])
150-
.arg(dist)
151-
.arg(&topgrade)
152-
.arg_if(ctx.config().verbose(), "-v")
153-
.arg_if(ctx.config().yes(Step::Wsl), "-y")
144+
.args(["-d", dist, "bash", "-lc", &script])
154145
.status_checked()
155146
}
156147

148+
fn wsl_topgrade_script(dist: &str, topgrade: &str, verbose: bool, yes: bool) -> String {
149+
let mut script = format!(
150+
"TOPGRADE_PREFIX={} exec {}",
151+
shell_words::quote(dist),
152+
shell_words::quote(topgrade)
153+
);
154+
if verbose {
155+
script.push_str(" -v");
156+
}
157+
if yes {
158+
script.push_str(" -y");
159+
}
160+
script
161+
}
162+
157163
pub fn run_wsl_topgrade(ctx: &ExecutionContext) -> Result<()> {
158164
if !is_wsl_installed()? {
159165
return Err(SkipStep(t!("WSL not installed").to_string()).into());
@@ -265,3 +271,16 @@ pub fn insert_startup_scripts(ctx: &ExecutionContext, git_repos: &mut RepoStep)
265271

266272
Ok(())
267273
}
274+
275+
#[cfg(test)]
276+
mod tests {
277+
use super::wsl_topgrade_script;
278+
279+
#[test]
280+
fn wsl_topgrade_script_quotes_dynamic_values_and_forwards_flags() {
281+
assert_eq!(
282+
wsl_topgrade_script("Distro's name", "/home/user/top grade", true, true),
283+
"TOPGRADE_PREFIX='Distro'\\''s name' exec '/home/user/top grade' -v -y"
284+
);
285+
}
286+
}

0 commit comments

Comments
 (0)