From 8610c2289bc41b1b7e0d659c50bb565328030419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Thu, 27 Oct 2022 09:50:21 +0200 Subject: [PATCH 1/2] Fix bug in wasmer run registry.wapm.io/python/python --- lib/cli/src/commands/run.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/cli/src/commands/run.rs b/lib/cli/src/commands/run.rs index 569b37394..21e416249 100644 --- a/lib/cli/src/commands/run.rs +++ b/lib/cli/src/commands/run.rs @@ -628,7 +628,12 @@ pub(crate) fn try_autoinstall_package( // Try auto-installing the remote package let mut args_without_package = args.to_vec(); - args_without_package.remove(1); + if args_without_package.get(1) == Some(&sv.original) { + let _ = args_without_package.remove(1); + } else if args_without_package.get(2) == Some(&sv.original) { + let _ = args_without_package.remove(1); + let _ = args_without_package.remove(1); + } let mut run_args = RunWithoutFile::try_parse_from(args_without_package.iter())?; run_args.command_name = sv.command.clone(); From 103025459157fd95516091e2d08137f1065e00ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Thu, 27 Oct 2022 11:21:47 +0200 Subject: [PATCH 2/2] Extract argument fixing into separate function --- lib/cli/src/commands/run.rs | 64 ++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/lib/cli/src/commands/run.rs b/lib/cli/src/commands/run.rs index 21e416249..fd66b7acd 100644 --- a/lib/cli/src/commands/run.rs +++ b/lib/cli/src/commands/run.rs @@ -627,14 +627,7 @@ pub(crate) fn try_autoinstall_package( }; // Try auto-installing the remote package - let mut args_without_package = args.to_vec(); - if args_without_package.get(1) == Some(&sv.original) { - let _ = args_without_package.remove(1); - } else if args_without_package.get(2) == Some(&sv.original) { - let _ = args_without_package.remove(1); - let _ = args_without_package.remove(1); - } - + let args_without_package = fixup_args(args, &sv.original); let mut run_args = RunWithoutFile::try_parse_from(args_without_package.iter())?; run_args.command_name = sv.command.clone(); @@ -664,17 +657,7 @@ fn try_execute_local_package( .map_err(|e| ExecuteLocalPackageError::BeforeExec(anyhow::anyhow!("{e}")))?; // Try finding the local package - let mut args_without_package = args.to_vec(); - - // remove either "run" or $package - args_without_package.remove(1); - - // "wasmer package arg1 arg2" => "wasmer arg1 arg2" - if (args_without_package.get(1).is_some() && args_without_package[1].starts_with(&sv.original)) - || (sv.command.is_some() && args_without_package[1].ends_with(sv.command.as_ref().unwrap())) - { - args_without_package.remove(1); - } + let args_without_package = fixup_args(args, &sv.original); RunWithoutFile::try_parse_from(args_without_package.iter()) .map_err(|e| ExecuteLocalPackageError::DuringExec(e.into()))? @@ -707,17 +690,46 @@ fn try_lookup_command(sv: &mut SplitVersion) -> Result Vec { + let mut args_without_package = args.to_vec(); + if args_without_package.get(1).map(|s| s.as_str()) == Some(command) { + let _ = args_without_package.remove(1); + } else if args_without_package.get(2).map(|s| s.as_str()) == Some(command) { + let _ = args_without_package.remove(1); + let _ = args_without_package.remove(1); + } + args_without_package +} + +#[test] +fn test_fixup_args() { + let first_args = vec![ + format!("wasmer"), + format!("run"), + format!("python/python"), + format!("--arg1"), + format!("--arg2"), + ]; + + let second_args = vec![ + format!("wasmer"), // no "run" + format!("python/python"), + format!("--arg1"), + format!("--arg2"), + ]; + + let arg1_transformed = fixup_args(&first_args, "python/python"); + let arg2_transformed = fixup_args(&second_args, "python/python"); + + assert_eq!(arg1_transformed, arg2_transformed); +} + pub(crate) fn try_run_package_or_file(args: &[String], r: &Run) -> Result<(), anyhow::Error> { // Check "r.path" is a file or a package / command name if r.path.exists() { if r.path.is_dir() && r.path.join("wapm.toml").exists() { - let mut args_without_package = args.to_vec(); - if args_without_package.get(1) == Some(&format!("{}", r.path.display())) { - let _ = args_without_package.remove(1); - } else if args_without_package.get(2) == Some(&format!("{}", r.path.display())) { - let _ = args_without_package.remove(1); - let _ = args_without_package.remove(1); - } + let args_without_package = fixup_args(args, &format!("{}", r.path.display())); return RunWithoutFile::try_parse_from(args_without_package.iter())? .into_run_args(r.path.clone(), r.command_name.as_deref())? .execute();