diff --git a/crates/vp_migration/src/eslint.rs b/crates/vp_migration/src/eslint.rs index 19baae67a5..fcd0a5c9da 100644 --- a/crates/vp_migration/src/eslint.rs +++ b/crates/vp_migration/src/eslint.rs @@ -251,6 +251,29 @@ mod tests { rewrite_eslint_script("cross-env NODE_ENV=test CI=true eslint --cache ."), "cross-env NODE_ENV=test CI=true vp lint ." ); + + assert_eq!( + rewrite_eslint_script("cross-env NODE_ENV=test bunx --bun eslint --cache ."), + "cross-env NODE_ENV=test bunx --bun vp lint ." + ); + assert_eq!( + rewrite_eslint_script("cross-env NODE_ENV=test echo -- bunx --bun eslint --cache ."), + "cross-env NODE_ENV=test echo -- bunx --bun eslint --cache ." + ); + assert_eq!( + rewrite_eslint_script("cross-env-shell CI=true pnpm exec bunx --bun eslint ."), + "cross-env-shell CI=true pnpm exec bunx --bun vp lint ." + ); + + // eslint passed as an argument to another command under cross-env stays untouched + assert_eq!( + rewrite_eslint_script("cross-env NODE_ENV=test vitest run eslint"), + "cross-env NODE_ENV=test vitest run eslint" + ); + assert_eq!( + rewrite_eslint_script("cross-env NODE_ENV=test echo eslint --cache"), + "cross-env NODE_ENV=test echo eslint --cache" + ); } #[test] diff --git a/crates/vp_migration/src/prettier.rs b/crates/vp_migration/src/prettier.rs index d4505cb68d..7ad5357fa6 100644 --- a/crates/vp_migration/src/prettier.rs +++ b/crates/vp_migration/src/prettier.rs @@ -189,6 +189,12 @@ mod tests { rewrite_prettier_script("cross-env NODE_ENV=test CI=true prettier --write --cache ."), "cross-env NODE_ENV=test CI=true vp fmt ." ); + + // prettier passed as an argument to another command under cross-env stays untouched + assert_eq!( + rewrite_prettier_script("cross-env NODE_ENV=test echo prettier --write"), + "cross-env NODE_ENV=test echo prettier --write" + ); } #[test] diff --git a/crates/vp_migration/src/script_rewrite.rs b/crates/vp_migration/src/script_rewrite.rs index 20a7c19acb..e5cf9ea5c2 100644 --- a/crates/vp_migration/src/script_rewrite.rs +++ b/crates/vp_migration/src/script_rewrite.rs @@ -46,25 +46,15 @@ pub(crate) fn rewrite_pack_flags(script: &str) -> String { fn rewrite_direct_pack_flags(script: &str) -> String { rewrite_simple_commands(script, |cmd| { - let words = collect_command_words(cmd); - let start = match words.first().map(|word| word.value.as_str()) { - Some("vp") => 0, - Some("cross-env" | "cross-env-shell") => { - let Some(index) = words.iter().skip(1).position(|word| !word.value.contains('=')) - else { - return false; - }; - index + 1 - } - _ => return false, + let words = collect_launched_command_words(cmd); + let [command, subcommand, args @ ..] = words.as_slice() else { + return false; }; - if words.get(start).is_none_or(|word| word.value != "vp") - || words.get(start + 1).is_none_or(|word| word.value != "pack") - { + if command.value != "vp" || subcommand.value != "pack" { return false; } let mut changed = false; - for word in words.iter().skip(start + 2) { + for word in args { if word.value == "--" { break; } @@ -247,9 +237,24 @@ struct BunxInvocation { target_suffix_index: usize, } -fn collect_command_words(cmd: &ast::SimpleCommand) -> Vec { +fn cross_env_command_index(suffix: &ast::CommandSuffix) -> Option { + suffix.0.iter().position( + |item| matches!(item, ast::CommandPrefixOrSuffixItem::Word(w) if !w.value.contains('=')), + ) +} + +fn collect_launched_command_words(cmd: &ast::SimpleCommand) -> Vec { let mut words = Vec::new(); - if let Some(name) = &cmd.word_or_name { + let Some(name) = &cmd.word_or_name else { + return words; + }; + let mut start = 0; + if matches!(name.value.as_str(), "cross-env" | "cross-env-shell") { + let Some(index) = cmd.suffix.as_ref().and_then(cross_env_command_index) else { + return words; + }; + start = index; + } else { words.push(CommandWord { position: CommandWordPosition::Name, ordinal: 0, @@ -257,7 +262,7 @@ fn collect_command_words(cmd: &ast::SimpleCommand) -> Vec { }); } if let Some(suffix) = &cmd.suffix { - for (index, item) in suffix.0.iter().enumerate() { + for (index, item) in suffix.0.iter().enumerate().skip(start) { if let ast::CommandPrefixOrSuffixItem::Word(word) = item { words.push(CommandWord { position: CommandWordPosition::Suffix(index), @@ -290,7 +295,7 @@ fn bunx_target(words: &[CommandWord], start: usize) -> Option { } /// Commands that launch a following command, either env-injection proxies -/// (`dotenv`, `cross-env`, `portless`, ...) or package runners (`npm`, `pnpm`, +/// (`dotenv`, `portless`, ...) or package runners (`npm`, `pnpm`, /// `yarn`, ...). Only when the command name is one of these does a trailing /// `--`/`run`/`exec` separator turn a subsequent `bunx` into a launcher whose /// inner command should be rewritten. @@ -300,19 +305,9 @@ fn bunx_target(words: &[CommandWord], start: usize) -> Option { /// their arguments are never corrupted by the rewrite. const KNOWN_RUNNER_WRAPPERS: &[&str] = &[ // Package runners. - "npm", - "pnpm", - "yarn", - "yarnpkg", - "bun", - "npx", - "pnpx", + "npm", "pnpm", "yarn", "yarnpkg", "bun", "npx", "pnpx", // Env-injection / proxy wrappers. - "cross-env", - "cross-env-shell", - "dotenv", - "dotenvx", - "portless", + "dotenv", "dotenvx", "portless", ]; /// Whether `name` is a command that launches a following command (see @@ -322,7 +317,7 @@ fn is_known_runner_wrapper(name: &str) -> bool { } fn find_bunx_invocations(cmd: &ast::SimpleCommand) -> Vec { - let words = collect_command_words(cmd); + let words = collect_launched_command_words(cmd); let mut invocations = Vec::new(); for start in 0..words.len() { @@ -333,33 +328,13 @@ fn find_bunx_invocations(cmd: &ast::SimpleCommand) -> Vec { continue; }; - let allowed_position = match words[start].position { - // `bunx` is itself the command being run. - CommandWordPosition::Name => true, - // `bunx` appears as an argument: only treat it as a launcher when a - // recognized wrapper command (`words[0]`) hands off to it via a - // `--`/`run`/`exec` separator. A bare `--`/`run`/`exec` on a - // non-wrapper command (e.g. `echo -- bunx ...`) is literal text. - CommandWordPosition::Suffix(runner_index) => { - let preceded_by_separator = cmd - .suffix - .as_ref() - .and_then(|suffix| runner_index.checked_sub(1).and_then(|i| suffix.0.get(i))) - .is_some_and(|item| { - matches!( - item, - ast::CommandPrefixOrSuffixItem::Word(word) - if matches!(word.value.as_str(), "--" | "run" | "exec") - ) - }); - let command_is_wrapper = cmd - .word_or_name - .as_ref() - .is_some_and(|name| is_known_runner_wrapper(&name.value)); - preceded_by_separator && command_is_wrapper - } - }; - if allowed_position { + // Only a known launcher can hand off to bunx after a separator. + // The ordinals keep a redirection between the two from looking adjacent. + let after_separator = start > 0 + && is_known_runner_wrapper(&words[0].value) + && words[start - 1].ordinal + 1 == words[start].ordinal + && matches!(words[start - 1].value.as_str(), "--" | "run" | "exec"); + if start == 0 || after_separator { invocations.push(BunxInvocation { target_suffix_index }); } } @@ -475,16 +450,17 @@ fn rewrite_in_cross_env(cmd: &mut ast::SimpleCommand, config: &ScriptRewriteConf None => return false, }; - let source_idx = suffix.0.iter().position(|item| { - matches!(item, ast::CommandPrefixOrSuffixItem::Word(w) if w.value == config.source_command) - }); - let Some(idx) = source_idx else { + let Some(idx) = cross_env_command_index(suffix) else { return false; }; - - if let ast::CommandPrefixOrSuffixItem::Word(w) = &mut suffix.0[idx] { - w.value = "vp".to_owned(); + let ast::CommandPrefixOrSuffixItem::Word(w) = &mut suffix.0[idx] else { + return false; + }; + if w.value != config.source_command { + return false; } + + w.value = "vp".to_owned(); suffix.0.insert(idx + 1, make_suffix_word(config.target_subcommand)); strip_flags_from_suffix(cmd, idx + 2, config);