diff --git a/Cargo.lock b/Cargo.lock index 39c72a018e..141e5f5b1f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8418,6 +8418,7 @@ dependencies = [ "vt_select", "vt_str", "vt_workspace", + "wax", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 58f6960a79..b3caa9bc0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -326,6 +326,7 @@ vt_snapshot_test = { git = "https://github.com/voidzero-dev/vite-task.git", rev vt_str = { git = "https://github.com/voidzero-dev/vite-task.git", rev = "6a23096aa2f04eb59225c6467b15c2965ee6ae33" } vt_workspace = { git = "https://github.com/voidzero-dev/vite-task.git", rev = "6a23096aa2f04eb59225c6467b15c2965ee6ae33" } walkdir = "2.5.0" +wax = "0.7.0" webpki-root-certs = "1.0.10" which = "8.0.6" winreg = "0.56.0" diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/check_cache_enabled/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/check_cache_enabled/package.json index 997527d326..5e88a5ddac 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/check_cache_enabled/package.json +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/check_cache_enabled/package.json @@ -2,6 +2,7 @@ "name": "check-cache-enabled-test", "type": "module", "scripts": { - "check": "vp check" + "check": "vp check", + "check:chdir": "vp -C . check" } } diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/check_cache_enabled/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/check_cache_enabled/snapshots.toml index 83199abfeb..74322d4ec7 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/check_cache_enabled/snapshots.toml +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/check_cache_enabled/snapshots.toml @@ -9,3 +9,13 @@ steps = [ { argv = ["vp", "run", "check"], comment = "third run should be cache miss after new file added", continue-on-failure = true }, { argv = ["vp", "run", "check"], envs = [["VP_SKIP_STATIC_CONFIG", "1"]], comment = "changing VP_SKIP_STATIC_CONFIG should be a cache miss", continue-on-failure = true }, ] + +[[case]] +name = "check_cache_chdir" +vp = "local" +skip-platforms = ["windows"] +comment = "A leading -C keeps the cache config of vp check, so its tracked env still invalidates the cache (#3503)." +steps = [ + { argv = ["vp", "run", "check:chdir"], comment = "first run should be cache miss", continue-on-failure = true }, + { argv = ["vp", "run", "check:chdir"], envs = [["VP_SKIP_STATIC_CONFIG", "1"]], comment = "changing VP_SKIP_STATIC_CONFIG should be a cache miss", continue-on-failure = true }, +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/check_cache_enabled/snapshots/check_cache_chdir.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/check_cache_enabled/snapshots/check_cache_chdir.md new file mode 100644 index 0000000000..f27da4c20f --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/check_cache_enabled/snapshots/check_cache_chdir.md @@ -0,0 +1,23 @@ +# check_cache_chdir + +A leading -C keeps the cache config of vp check, so its tracked env still invalidates the cache (#3503). + +## `vp run check:chdir` + +first run should be cache miss + +``` +$ vp -C . check +pass: All 3 files are correctly formatted (, threads) +pass: Found no warnings or lint errors in 2 files (, threads) +``` + +## `VP_SKIP_STATIC_CONFIG=1 vp run check:chdir` + +changing VP_SKIP_STATIC_CONFIG should be a cache miss + +``` +$ vp -C . check ○ cache miss: env 'VP_SKIP_STATIC_CONFIG' changed, executing +pass: All 3 files are correctly formatted (, threads) +pass: Found no warnings or lint errors in 2 files (, threads) +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_init_inline_config/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_init_inline_config/package.json index f47b862220..ba676eeaa6 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_init_inline_config/package.json +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_init_inline_config/package.json @@ -1,5 +1,8 @@ { "name": "command-init-inline-config", "version": "0.0.0", - "private": true + "private": true, + "scripts": { + "fmt:init": "vp -C . fmt --init" + } } diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_init_inline_config/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_init_inline_config/snapshots.toml index b55353ff47..cbd55dbd4b 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_init_inline_config/snapshots.toml +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_init_inline_config/snapshots.toml @@ -10,3 +10,13 @@ steps = [ { argv = ["vpt", "print-file", "vite.config.ts"], continue-on-failure = true }, { argv = ["vpt", "stat-file", ".oxfmtrc.json", "--assert-not", "file"], comment = "check .oxfmtrc.json is removed", continue-on-failure = true }, ] + +[[case]] +name = "command_init_inline_config_chdir_script" +vp = "local" +comment = "An init command after vp -C in a task script merges its config into vite.config.ts like a direct call." +steps = [ + { argv = ["vp", "run", "fmt:init"], continue-on-failure = true }, + { argv = ["vpt", "print-file", "vite.config.ts"], continue-on-failure = true }, + { argv = ["vpt", "stat-file", ".oxfmtrc.json", "--assert-not", "file"], comment = "check .oxfmtrc.json is removed", continue-on-failure = true }, +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_init_inline_config/snapshots/command_init_inline_config_chdir_script.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_init_inline_config/snapshots/command_init_inline_config_chdir_script.md new file mode 100644 index 0000000000..6df32c2957 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_init_inline_config/snapshots/command_init_inline_config_chdir_script.md @@ -0,0 +1,30 @@ +# command_init_inline_config_chdir_script + +An init command after vp -C in a task script merges its config into vite.config.ts like a direct call. + +## `vp run fmt:init` + +``` +$ vp -C . fmt --init ⊘ cache disabled +Added 'fmt' to 'vite.config.ts'. +``` + +## `vpt print-file vite.config.ts` + +``` +import { defineConfig } from "vite-plus"; + +export default defineConfig({ + fmt: { + ignorePatterns: [], + }, +}); +``` + +## `vpt stat-file .oxfmtrc.json --assert-not file` + +check .oxfmtrc.json is removed + +``` +.oxfmtrc.json: missing +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/package.json index dea0bba909..b2b4a28b48 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/package.json +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/package.json @@ -5,6 +5,9 @@ "packages/*" ], "scripts": { - "where:hello": "vp -C packages/hello run where" + "where:hello": "vp -C packages/hello run where", + "build:missing": "vp -C packages/missing build", + "version:chdir": "vp -C packages/hello --version", + "help:chdir": "vp -C packages/hello" } } diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots.toml index 3369ab8938..cf791e3591 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots.toml +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots.toml @@ -21,6 +21,9 @@ steps = [ { argv = ["vp", "pack", "packages/hello"], comment = "positional stays a tsdown entry resolved from the invocation directory" }, { argv = ["vpt", "list-dir", "dist"], comment = "upstream semantics: output lands at the invocation directory" }, { argv = ["vp", "run", "where:hello"], comment = "a script whose command starts with vp -C runs verbatim and honors the directory" }, + { argv = ["vp", "run", "version:chdir"], comment = "a script with --version after -C runs verbatim, so vp prints versions like a direct invocation" }, + { argv = ["vp", "run", "help:chdir"], snapshot = false, tty = false, comment = "a script with only -C runs in a child vp, which shows help like a direct invocation" }, + { argv = ["vp", "run", "build:missing"], comment = "a script with a missing -C directory runs in a child vp, which reports it like a direct invocation", continue-on-failure = true }, ] # Regression guard for the -C direct-exec PWD gap: a command run through diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots/cwd_flag.global.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots/cwd_flag.global.md index 665ffc60b1..c26a35e919 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots/cwd_flag.global.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots/cwd_flag.global.md @@ -143,3 +143,50 @@ VITE+ - The Unified Toolchain for the Web ~/packages/hello$ node -e "console.log('cwd base: ' + require('node:path').basename(process.cwd()))" ⊘ cache disabled cwd base: hello ``` + +## `vp run version:chdir` + +a script with --version after -C runs verbatim, so vp prints versions like a direct invocation + +``` +VITE+ - The Unified Toolchain for the Web + +$ vp -C packages/hello --version ⊘ cache disabled +VITE+ - The Unified Toolchain for the Web + +vp + +Local vite-plus: + vite-plus + +Tools: + vite + rolldown + vitest + oxfmt + oxlint + tsrs + tsdown + +Environment: + Package manager Not found + Node.js +``` + +## `vp run help:chdir` + +a script with only -C runs in a child vp, which shows help like a direct invocation + + +## `vp run build:missing` + +a script with a missing -C directory runs in a child vp, which reports it like a direct invocation + +**Exit code:** 1 + +``` +VITE+ - The Unified Toolchain for the Web + +$ vp -C packages/missing build ⊘ cache disabled +directory not found: packages/missing +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots/cwd_flag.local.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots/cwd_flag.local.md index daf28d8085..aa5b40f2ef 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots/cwd_flag.local.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag/snapshots/cwd_flag.local.md @@ -129,3 +129,33 @@ $ vp -C packages/hello run where ⊘ cache disabled ~/packages/hello$ node -e "console.log('cwd base: ' + require('node:path').basename(process.cwd()))" ⊘ cache disabled cwd base: hello ``` + +## `vp run version:chdir` + +a script with --version after -C runs verbatim, so vp prints versions like a direct invocation + +``` +$ vp -C packages/hello --version ⊘ cache disabled +VITE+ - The Unified Toolchain for the Web + +vp + +Local vite-plus: + vite-plus Not found +``` + +## `vp run help:chdir` + +a script with only -C runs in a child vp, which shows help like a direct invocation + + +## `vp run build:missing` + +a script with a missing -C directory runs in a child vp, which reports it like a direct invocation + +**Exit code:** 1 + +``` +$ vp -C packages/missing build ⊘ cache disabled +error: directory not found: packages/missing +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/package.json new file mode 100644 index 0000000000..2660a24dad --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/package.json @@ -0,0 +1,11 @@ +{ + "name": "cwd-flag-script-pack-cache", + "private": true, + "type": "module", + "workspaces": [ + "packages/*" + ], + "scripts": { + "pack:app": "vp -C packages/app pack" + } +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/packages/app/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/packages/app/package.json new file mode 100644 index 0000000000..33f2660adc --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/packages/app/package.json @@ -0,0 +1,5 @@ +{ + "name": "app", + "private": true, + "type": "module" +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/packages/app/src/index.ts b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/packages/app/src/index.ts new file mode 100644 index 0000000000..673dfa21a7 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/packages/app/src/index.ts @@ -0,0 +1 @@ +export { value } from '../../../dist/shared.js'; diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/snapshots.toml new file mode 100644 index 0000000000..e12ecf362b --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/snapshots.toml @@ -0,0 +1,17 @@ +[[case]] +name = "cwd_flag_script_pack_cache" +vp = "local" +skip-platforms = ["windows"] +comment = """ +A cached root task runs vp -C packages/app pack. Pack excludes its own dist +output from the cache inputs, and that exclusion must apply to packages/app/dist +rather than the root dist, which the package reads as an input. +""" +steps = [ + { argv = ["vpt", "write-file", "dist/shared.js", "export const value = 'shared-one';\n"], comment = "dist is ignored by git, so the root input is created here" }, + { argv = ["vp", "run", "pack:app"], comment = "first run is a cache miss" }, + { argv = ["vp", "run", "pack:app"], comment = "second run without changes is a cache hit" }, + ["vpt", "write-file", "dist/shared.js", "export const value = 'shared-two';\n"], + { argv = ["vp", "run", "pack:app"], comment = "changing the root dist input is a cache miss" }, + ["vpt", "grep-file", "packages/app/dist/index.mjs", "shared-two"], +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/snapshots/cwd_flag_script_pack_cache.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/snapshots/cwd_flag_script_pack_cache.md new file mode 100644 index 0000000000..a14cbdadff --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/snapshots/cwd_flag_script_pack_cache.md @@ -0,0 +1,68 @@ +# cwd_flag_script_pack_cache + +A cached root task runs vp -C packages/app pack. Pack excludes its own dist +output from the cache inputs, and that exclusion must apply to packages/app/dist +rather than the root dist, which the package reads as an input. + +## `vpt write-file dist/shared.js 'export const value = '\''shared-one'\''; +'` + +dist is ignored by git, so the root input is created here + +``` +``` + +## `vp run pack:app` + +first run is a cache miss + +``` +$ vp -C packages/app pack +ℹ entry: src/index.ts +ℹ Build start +ℹ dist/index.mjs kB │ gzip: kB +ℹ 1 files, total: kB +✔ Build complete in +``` + +## `vp run pack:app` + +second run without changes is a cache hit + +``` +$ vp -C packages/app pack ◉ cache hit, replaying +ℹ entry: src/index.ts +ℹ Build start +ℹ dist/index.mjs kB │ gzip: kB +ℹ 1 files, total: kB +✔ Build complete in + +--- +vp run: cache hit, saved. +``` + +## `vpt write-file dist/shared.js 'export const value = '\''shared-two'\''; +'` + +``` +``` + +## `vp run pack:app` + +changing the root dist input is a cache miss + +``` +$ vp -C packages/app pack ○ cache miss: 'dist/shared.js' modified, executing +ℹ entry: src/index.ts +ℹ Build start +ℹ Cleaning files +ℹ dist/index.mjs kB │ gzip: kB +ℹ 1 files, total: kB +✔ Build complete in +``` + +## `vpt grep-file packages/app/dist/index.mjs shared-two` + +``` +packages/app/dist/index.mjs: found "shared-two" +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/vite.config.ts b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/vite.config.ts new file mode 100644 index 0000000000..5f95ceeb78 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_pack_cache/vite.config.ts @@ -0,0 +1,5 @@ +export default { + run: { + cache: true, + }, +}; diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/.node-version b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/.node-version new file mode 100644 index 0000000000..b8b23ca93c --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/.node-version @@ -0,0 +1 @@ +22.23.4 diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/package.json new file mode 100644 index 0000000000..8589496946 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/package.json @@ -0,0 +1,10 @@ +{ + "name": "cwd-flag-script-runtime", + "private": true, + "workspaces": [ + "packages/*" + ], + "scripts": { + "build:app": "vp -C packages/app build" + } +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/.node-version b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/.node-version new file mode 100644 index 0000000000..ce62013c39 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/.node-version @@ -0,0 +1 @@ +24.22.1 diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/index.html b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/index.html new file mode 100644 index 0000000000..3b66214425 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/index.html @@ -0,0 +1 @@ + diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/package.json new file mode 100644 index 0000000000..33f2660adc --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/package.json @@ -0,0 +1,5 @@ +{ + "name": "app", + "private": true, + "type": "module" +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/vite.config.ts b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/vite.config.ts new file mode 100644 index 0000000000..0783d78efe --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/packages/app/vite.config.ts @@ -0,0 +1,8 @@ +import { writeFileSync } from 'node:fs'; + +// The task runner also loads this config, so only the build records its runtime. +if (process.argv.includes('build')) { + writeFileSync('node-major.txt', `node ${process.versions.node.split('.')[0]}`); +} + +export default {}; diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/snapshots.toml new file mode 100644 index 0000000000..fc6065d0ae --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/snapshots.toml @@ -0,0 +1,12 @@ +[[case]] +name = "cwd_flag_script_runtime" +vp = "global" +comment = """ +A task script that starts with vp -C runs in a child vp, which selects the +target directory's Node runtime like a direct invocation. The root pins +Node 22 and the target package pins Node 24. +""" +steps = [ + { argv = ["vp", "run", "build:app"], snapshot = false }, + ["vpt", "grep-file", "packages/app/node-major.txt", "node 24"], +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/snapshots/cwd_flag_script_runtime.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/snapshots/cwd_flag_script_runtime.md new file mode 100644 index 0000000000..18166336a3 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/cwd_flag_script_runtime/snapshots/cwd_flag_script_runtime.md @@ -0,0 +1,14 @@ +# cwd_flag_script_runtime + +A task script that starts with vp -C runs in a child vp, which selects the +target directory's Node runtime like a direct invocation. The root pins +Node 22 and the target package pins Node 24. + +## `vp run build:app` + + +## `vpt grep-file packages/app/node-major.txt 'node 24'` + +``` +packages/app/node-major.txt: found "node 24" +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/package.json index d309e85f41..9941170cfc 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/package.json +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/package.json @@ -1,5 +1,8 @@ { "name": "vite-argv-passthrough", "private": true, - "type": "module" + "type": "module", + "scripts": { + "build:chdir": "vp -C . build -- --help" + } } diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/snapshots.toml index a449acd29f..26cb12e693 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/snapshots.toml +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/snapshots.toml @@ -68,3 +68,13 @@ steps = [ ["vpt", "grep-file", "vite-argv.json", '["build","--outDir","custom-dist","--","--app","web","--label","two words"]'], ["vpt", "grep-file", "custom-dist/index.html", "Build from the project root"], ] + +[[case]] +name = "build_script_chdir_forwards_separator" +vp = ["local", "global"] +comment = "A task script that starts with vp -C forwards the separator to Vite like a direct call." +steps = [ + { argv = ["vp", "run", "build:chdir"], snapshot = false }, + ["vpt", "grep-file", "vite-argv.json", '["build","--","--help"]'], + ["vpt", "grep-file", "dist/index.html", "Build from the project root"], +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/snapshots/build_script_chdir_forwards_separator.global.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/snapshots/build_script_chdir_forwards_separator.global.md new file mode 100644 index 0000000000..9920d17183 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/snapshots/build_script_chdir_forwards_separator.global.md @@ -0,0 +1,18 @@ +# build_script_chdir_forwards_separator + +A task script that starts with vp -C forwards the separator to Vite like a direct call. + +## `vp run build:chdir` + + +## `vpt grep-file vite-argv.json '["build","--","--help"]'` + +``` +vite-argv.json: found "[\"build\",\"--\",\"--help\"]" +``` + +## `vpt grep-file dist/index.html 'Build from the project root'` + +``` +dist/index.html: found "Build from the project root" +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/snapshots/build_script_chdir_forwards_separator.local.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/snapshots/build_script_chdir_forwards_separator.local.md new file mode 100644 index 0000000000..9920d17183 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/vite_argv_passthrough/snapshots/build_script_chdir_forwards_separator.local.md @@ -0,0 +1,18 @@ +# build_script_chdir_forwards_separator + +A task script that starts with vp -C forwards the separator to Vite like a direct call. + +## `vp run build:chdir` + + +## `vpt grep-file vite-argv.json '["build","--","--help"]'` + +``` +vite-argv.json: found "[\"build\",\"--\",\"--help\"]" +``` + +## `vpt grep-file dist/index.html 'Build from the project root'` + +``` +dist/index.html: found "Build from the project root" +``` diff --git a/packages/cli/binding/Cargo.toml b/packages/cli/binding/Cargo.toml index 6c4dc99b49..a0062d4273 100644 --- a/packages/cli/binding/Cargo.toml +++ b/packages/cli/binding/Cargo.toml @@ -41,6 +41,7 @@ vt_str = { workspace = true } vt = { workspace = true } vt_casefold = { workspace = true } vt_workspace = { workspace = true } +wax = { workspace = true } rolldown_binding = { workspace = true, optional = true, features = ["disable_panic_hook"] } oxc-node = { workspace = true, optional = true } diff --git a/packages/cli/binding/src/cli/handler.rs b/packages/cli/binding/src/cli/handler.rs index 9f37c0a67b..4962c5f39a 100644 --- a/packages/cli/binding/src/cli/handler.rs +++ b/packages/cli/binding/src/cli/handler.rs @@ -3,14 +3,14 @@ use std::iter; use clap::{Parser, error::ErrorKind}; use vt::{ CommandHandler, HandledCommand, ScriptCommand, - config::user::{EnabledCacheConfig, UserCacheConfig, UserRunConfig}, + config::user::{InputBase, UserCacheConfig, UserInputEntry, UserRunConfig}, loader::UserConfigLoader, }; use vt_path::AbsolutePath; use vt_str::Str; use super::{ - resolver::{SubcommandResolver, check_cache_inputs, resolve_vite_config_fields}, + resolver::{SubcommandResolver, cache_config_for, resolve_vite_config_fields}, types::{CLIArgs, SynthesizableSubcommand, ViteConfigResolverFn}, }; @@ -57,14 +57,35 @@ impl CommandHandler for VitePlusCommandHandler { return Ok(HandledCommand::Verbatim); } - // A leading global `-C` runs verbatim: CLIArgs has no global flags, and - // the spawned vp/vpr binary applies the directory change (and target - // elicitation) exactly like a direct invocation. - if command.args.first().is_some_and(|arg| arg.starts_with("-C")) { - return Ok(HandledCommand::Verbatim); - } // "vpr " is shorthand for "vp run ", so prepend "run" for parsing. let is_vpr = program == "vpr"; + + // A leading global `-C ` keeps running in a child vp, which applies the + // directory, runtime, and toolchain like a direct invocation. A built-in subcommand + // only adds its cache config, so its tracked envs and inputs apply (#3503). + if command.args.first().is_some_and(|arg| arg.starts_with("-C")) { + // `vpr` always means `vp run`, which has no built-in cache config to attach. + if is_vpr { + return Ok(HandledCommand::Verbatim); + } + let parsed = split_leading_chdir(&command.args).and_then(|(dir, rest)| { + let args = iter::once("vp").chain(rest.iter().map(Str::as_str)); + match CLIArgs::try_parse_from(args).ok()? { + CLIArgs::Synthesizable(subcmd) => Some((dir, subcmd)), + _ => None, + } + }); + let Some((dir, subcmd)) = parsed else { + return Ok(HandledCommand::Verbatim); + }; + let mut cache_config = cache_config_for(&subcmd); + let target = command.cwd.join(dir).clean(); + let target = target.strip_prefix(self.resolver.workspace_path()).ok().flatten(); + rebase_package_inputs(&mut cache_config, target.as_ref().map(|path| path.as_str())); + return Ok(HandledCommand::Synthesized( + command.to_synthetic_plan_request(cache_config), + )); + } let cli_args = match CLIArgs::try_parse_from( iter::once("vp") .chain(is_vpr.then_some("run")) @@ -79,20 +100,11 @@ impl CommandHandler for VitePlusCommandHandler { Err(err) => return Err(err.into()), }; match cli_args { - CLIArgs::Synthesizable(SynthesizableSubcommand::Check { .. }) => { + CLIArgs::Synthesizable(subcmd @ SynthesizableSubcommand::Check { .. }) => { // Check is a composite command (fmt + lint) — run as a subprocess in task scripts - Ok(HandledCommand::Synthesized(command.to_synthetic_plan_request( - UserCacheConfig::with_config(EnabledCacheConfig { - env: Some(Box::new([ - Str::from("OXLINT_TSGOLINT_PATH"), - Str::from(vp_shared::env_vars::VP_SKIP_STATIC_CONFIG), - ])), - untracked_env: None, - input: Some(check_cache_inputs()), - output: None, - remote: None, - }), - ))) + Ok(HandledCommand::Synthesized( + command.to_synthetic_plan_request(cache_config_for(&subcmd)), + )) } CLIArgs::Synthesizable(subcmd) => { // Bare app commands in scripts get the same workspace-root @@ -117,6 +129,40 @@ impl CommandHandler for VitePlusCommandHandler { } } +fn split_leading_chdir(args: &[Str]) -> Option<(&str, &[Str])> { + let rest = args.first()?.as_str().strip_prefix("-C")?; + let (dir, remaining) = if rest.is_empty() { + (args.get(1)?.as_str(), &args[2..]) + } else { + (rest.strip_prefix('=').unwrap_or(rest), &args[1..]) + }; + (!dir.is_empty()).then_some((dir, remaining)) +} + +/// The plan keeps the task's package as the base of package-relative globs, while the +/// child runs in the `-C` target, so point those globs at the target from the workspace +/// root. A target outside the workspace cannot be expressed that way, so its +/// package-relative globs are dropped. +fn rebase_package_inputs(cache_config: &mut UserCacheConfig, target: Option<&str>) { + let UserCacheConfig::Config(config) = cache_config else { return }; + let Some(inputs) = &mut config.input else { return }; + inputs.retain_mut(|entry| { + let UserInputEntry::GlobWithBase(glob) = entry else { return true }; + if glob.base != InputBase::Package { + return true; + } + let Some(target) = target else { return false }; + if !target.is_empty() { + let (negation, pattern) = + glob.pattern.strip_prefix('!').map_or(("", glob.pattern.as_str()), |p| ("!", p)); + let target = wax::escape(target); + glob.pattern = Str::from(format!("{negation}{target}/{pattern}").as_str()); + } + glob.base = InputBase::Workspace; + true + }); +} + /// Vite Task rewrites workspace `.bin` shims. Managed package managers live /// outside the workspace, so apply Vite+'s launcher policy before the plan is /// fingerprinted. This also preserves the normal non-TTY and missing-.ps1 @@ -172,3 +218,57 @@ impl UserConfigLoader for VitePlusConfigLoader { Ok(config.run.map(serde_json::from_value).transpose()?) } } + +#[cfg(test)] +mod tests { + use super::*; + + fn split(args: &[&str]) -> Option<(String, Vec)> { + let args: Vec = args.iter().map(|arg| Str::from(*arg)).collect(); + split_leading_chdir(&args) + .map(|(dir, rest)| (dir.to_owned(), rest.iter().map(ToString::to_string).collect())) + } + + #[test] + fn splits_every_leading_chdir_form() { + let expected = Some(("apps/web".to_owned(), vec!["lint".to_owned()])); + assert_eq!(split(&["-C", "apps/web", "lint"]), expected); + assert_eq!(split(&["-C=apps/web", "lint"]), expected); + assert_eq!(split(&["-Capps/web", "lint"]), expected); + assert_eq!(split(&["-C"]), None); + assert_eq!(split(&["-C=", "lint"]), None); + assert_eq!(split(&["lint", "-C", "apps/web"]), None); + } + + fn rebased_inputs(target: Option<&str>) -> Vec { + let mut config = cache_config_for(&SynthesizableSubcommand::Pack { args: vec![] }); + rebase_package_inputs(&mut config, target); + let UserCacheConfig::Config(config) = config else { panic!("pack caches") }; + config.input.expect("pack declares its inputs") + } + + fn glob(pattern: &str, base: InputBase) -> UserInputEntry { + UserInputEntry::GlobWithBase(vt::config::user::GlobWithBase { + pattern: Str::from(pattern), + base, + }) + } + + #[test] + fn rebases_package_inputs_onto_the_chdir_target() { + let auto = UserInputEntry::Auto(vt::config::user::AutoTracking { auto: true }); + assert_eq!( + rebased_inputs(Some("packages/app")), + [auto.clone(), glob("!packages/app/dist/**", InputBase::Workspace)] + ); + assert_eq!( + rebased_inputs(Some("packages/(app)")), + [auto.clone(), glob(r"!packages/\(app\)/dist/**", InputBase::Workspace)] + ); + assert_eq!( + rebased_inputs(Some("")), + [auto.clone(), glob("!dist/**", InputBase::Workspace)] + ); + assert_eq!(rebased_inputs(None), [auto]); + } +} diff --git a/packages/cli/binding/src/cli/resolver.rs b/packages/cli/binding/src/cli/resolver.rs index f2c5c4b656..7df7201709 100644 --- a/packages/cli/binding/src/cli/resolver.rs +++ b/packages/cli/binding/src/cli/resolver.rs @@ -68,6 +68,7 @@ impl SubcommandResolver { envs: &Arc, cwd: &AbsolutePath, ) -> anyhow::Result { + let cache_config = cache_config_for(&subcommand); match subcommand { SynthesizableSubcommand::Lint { args } => { let cli_options = self.cli_options()?; @@ -86,13 +87,7 @@ impl SubcommandResolver { .into_iter() .chain(args.into_iter().map(Str::from)) .collect(), - cache_config: UserCacheConfig::with_config(EnabledCacheConfig { - env: Some(Box::new([Str::from("OXLINT_TSGOLINT_PATH")])), - untracked_env: None, - input: None, - output: None, - remote: None, - }), + cache_config, envs: merge_resolved_envs_with_version(envs, resolved.envs), }) } @@ -109,13 +104,7 @@ impl SubcommandResolver { args: iter::once(Str::from(js_path_str)) .chain(args.into_iter().map(Str::from)) .collect(), - cache_config: UserCacheConfig::with_config(EnabledCacheConfig { - env: None, - untracked_env: None, - input: None, - output: None, - remote: None, - }), + cache_config, envs: merge_resolved_envs_with_version(envs, resolved.envs), }) } @@ -133,19 +122,7 @@ impl SubcommandResolver { .chain(iter::once(Str::from("build"))) .chain(args.into_iter().map(Str::from)) .collect(), - // No synthetic cache config: vite reports its inputs/outputs/ - // envs to the runner via `@voidzero-dev/vite-task-client`. - // All fields `None` keep caching enabled with auto input and - // auto output inference (the latter drives output restoration); - // vite's `ignoreInput`/`ignoreOutput`/`getEnv`/`getEnvs` refine - // the fingerprint at runtime. - cache_config: UserCacheConfig::with_config(EnabledCacheConfig { - env: None, - untracked_env: None, - input: None, - output: None, - remote: None, - }), + cache_config, envs: merge_resolved_envs_with_version(envs, resolved.envs), }) } @@ -166,19 +143,7 @@ impl SubcommandResolver { Ok(ResolvedSubcommand { program: Arc::clone(&cli_options.node_exec_path), args: iter::once(Str::from(js_path_str)).chain(vitest_args).collect(), - cache_config: UserCacheConfig::with_config(EnabledCacheConfig { - env: None, - untracked_env: None, - input: Some(vec![ - UserInputEntry::Auto(AutoTracking { auto: true }), - exclude_glob( - "!node_modules/.vite/vitest/**/results.json", - InputBase::Package, - ), - ]), - output: None, - remote: None, - }), + cache_config, envs: merge_resolved_envs_with_version(envs, resolved.envs), }) } @@ -195,13 +160,7 @@ impl SubcommandResolver { args: iter::once(Str::from(js_path_str)) .chain(args.into_iter().map(Str::from)) .collect(), - cache_config: UserCacheConfig::with_config(EnabledCacheConfig { - env: None, - untracked_env: None, - input: Some(build_pack_cache_inputs()), - output: None, - remote: None, - }), + cache_config, envs: merge_resolved_envs(envs, resolved.envs), }) } @@ -219,7 +178,7 @@ impl SubcommandResolver { .chain(iter::once(Str::from("dev"))) .chain(args.into_iter().map(Str::from)) .collect(), - cache_config: UserCacheConfig::disabled(), + cache_config, envs: merge_resolved_envs_with_version(envs, resolved.envs), }) } @@ -237,7 +196,7 @@ impl SubcommandResolver { .chain(iter::once(Str::from("preview"))) .chain(args.into_iter().map(Str::from)) .collect(), - cache_config: UserCacheConfig::disabled(), + cache_config, envs: merge_resolved_envs_with_version(envs, resolved.envs), }) } @@ -254,13 +213,7 @@ impl SubcommandResolver { args: iter::once(Str::from(js_path_str)) .chain(args.into_iter().map(Str::from)) .collect(), - cache_config: UserCacheConfig::with_config(EnabledCacheConfig { - env: None, - untracked_env: None, - input: None, - output: None, - remote: None, - }), + cache_config, envs: merge_resolved_envs(envs, resolved.envs), }) } @@ -331,6 +284,41 @@ fn exclude_glob(pattern: &str, base: InputBase) -> UserInputEntry { UserInputEntry::GlobWithBase(GlobWithBase { pattern: Str::from(pattern), base }) } +/// Cache config for each synthesizable subcommand, shared by the resolver and by task +/// commands that keep running in a child `vp`. +pub(super) fn cache_config_for(subcommand: &SynthesizableSubcommand) -> UserCacheConfig { + let enabled = |env: Option<&[&str]>, input: Option>| { + UserCacheConfig::with_config(EnabledCacheConfig { + env: env.map(|names| names.iter().copied().map(Str::from).collect()), + input, + ..EnabledCacheConfig::default() + }) + }; + match subcommand { + SynthesizableSubcommand::Check { .. } => enabled( + Some(&["OXLINT_TSGOLINT_PATH", vp_shared::env_vars::VP_SKIP_STATIC_CONFIG]), + Some(check_cache_inputs()), + ), + SynthesizableSubcommand::Lint { .. } => enabled(Some(&["OXLINT_TSGOLINT_PATH"]), None), + SynthesizableSubcommand::Test { .. } => enabled( + None, + Some(vec![ + UserInputEntry::Auto(AutoTracking { auto: true }), + exclude_glob("!node_modules/.vite/vitest/**/results.json", InputBase::Package), + ]), + ), + SynthesizableSubcommand::Pack { .. } => enabled(None, Some(build_pack_cache_inputs())), + SynthesizableSubcommand::Dev { .. } | SynthesizableSubcommand::Preview { .. } => { + UserCacheConfig::disabled() + } + // Vite reports its inputs, outputs, and envs to the runner through + // `@voidzero-dev/vite-task-client`, so build keeps automatic tracking. + SynthesizableSubcommand::Fmt { .. } + | SynthesizableSubcommand::Build { .. } + | SynthesizableSubcommand::Doc { .. } => enabled(None, None), + } +} + /// Common cache input entries for the pack command. /// Excludes dist output files that are both read and written. /// TODO: The hardcoded `!dist/**` exclusion is a temporary workaround. It will be replaced @@ -346,7 +334,7 @@ fn build_pack_cache_inputs() -> Vec { /// The vp check subprocess is a full vp CLI process (not resolved to a binary like /// build/lint/fmt), so it accesses additional directories that must be excluded: /// - `.vite/task-cache`: task runner state files that change after each run -pub(super) fn check_cache_inputs() -> Vec { +fn check_cache_inputs() -> Vec { vec![ UserInputEntry::Auto(AutoTracking { auto: true }), exclude_glob("!node_modules/.vite/task-cache/**", InputBase::Workspace),