diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/check-vite-peer.mjs b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/check-vite-peer.mjs new file mode 100644 index 0000000000..20f378a735 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/check-vite-peer.mjs @@ -0,0 +1,13 @@ +import assert from 'node:assert/strict'; +import { createRequire } from 'node:module'; + +const project = createRequire(import.meta.url); +const cli = createRequire(project.resolve('vite-plus/package.json')); +const runner = createRequire(project.resolve('vitest')); +const core = cli.resolve('vite/package.json'); + +assert.equal(project('vite/package.json').name, '@voidzero-dev/vite-plus-core'); +assert.equal(project.resolve('vite/package.json'), core); +assert.equal(runner.resolve('vite/package.json'), core); +assert.equal(project.resolve('vitest'), cli.resolve('vitest')); +console.log('The project and Vite+ share Vitest and Vite+ core'); diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots.toml index caf772f3e6..6a10e294e9 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots.toml +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots.toml @@ -40,3 +40,41 @@ steps = [ ["vp", "migrate", "--no-interactive", "--no-hooks", "--no-agent", "--no-editor"], ["vpt", "stat-file", "install-requested", "--assert", "missing"], ] + +[[case]] +name = "unresolved_lint_config_retains_vitest_peer" +vp = "local" +local-registry = true +unset-env = ["VP_SKIP_INSTALL"] +comment = "A config that still throws after install keeps the upstream Vitest import and its Vite peer. Rerunning migration does not reinstall." +steps = [ + { argv = ["vpt", "json-edit", "package.json", "devDependencies.vitest", "5.1.1"], snapshot = false }, + { argv = ["vpt", "write-file", "vite.config.ts", "export default () => { throw new Error('missing project setting'); };\n"], snapshot = false }, + { argv = ["vpt", "write-file", ".pnpmfile.cjs", "require('node:fs').writeFileSync(require('node:path').join(__dirname, 'install-requested'), 'install');\nmodule.exports = {};\n"], snapshot = false }, + { argv = ["vp", "migrate", "--no-interactive", "--no-hooks", "--no-agent", "--no-editor"], snapshot = false }, + ["vpt", "print-file", "package.json"], + ["vpt", "print-file", "unit.test.ts"], + ["node", "check-vite-peer.mjs"], + { argv = ["vpt", "cp", "pnpm-lock.yaml", "lockfile.before"], snapshot = false }, + ["vpt", "stat-file", "install-requested", "--assert", "file"], + { argv = ["vpt", "rm", "install-requested"], snapshot = false }, + { argv = ["vp", "migrate", "--no-interactive", "--no-hooks", "--no-agent", "--no-editor"], snapshot = false }, + ["vpt", "stat-file", "install-requested", "--assert", "missing"], + ["node", "-e", "const fs = require('node:fs'); require('node:assert/strict').deepEqual(fs.readFileSync('pnpm-lock.yaml'), fs.readFileSync('lockfile.before'));"], +] + +[[case]] +name = "unresolved_lint_config_skip_install" +vp = "local" +local-registry = true +env = { VP_SKIP_INSTALL = "1" } +comment = "Skipping dependency installation still supplies the retained Vitest peer for a later pnpm install." +steps = [ + { argv = ["vpt", "json-edit", "package.json", "devDependencies.vitest", "5.1.1"], snapshot = false }, + { argv = ["vpt", "write-file", "vite.config.ts", "export default () => { throw new Error('missing project setting'); };\n"], snapshot = false }, + { argv = ["vp", "migrate", "--no-interactive", "--no-hooks", "--no-agent", "--no-editor"], snapshot = false }, + ["vpt", "stat-file", "node_modules", "--assert", "missing"], + ["vpt", "print-file", "package.json"], + { argv = ["pnpm", "install", "--no-frozen-lockfile", "--ignore-scripts"], snapshot = false }, + ["node", "check-vite-peer.mjs"], +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots/migration_import_rule_install.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots/migration_import_rule_install.md index f806966d79..4fe9cd8321 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots/migration_import_rule_install.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots/migration_import_rule_install.md @@ -65,6 +65,7 @@ test("template", () => {}); "private": true, "type": "module", "devDependencies": { + "vite": "catalog:", "vitest": "catalog:" } } diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots/unresolved_lint_config_retains_vitest_peer.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots/unresolved_lint_config_retains_vitest_peer.md new file mode 100644 index 0000000000..59855d41a2 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots/unresolved_lint_config_retains_vitest_peer.md @@ -0,0 +1,77 @@ +# unresolved_lint_config_retains_vitest_peer + +A config that still throws after install keeps the upstream Vitest import and its Vite peer. Rerunning migration does not reinstall. + +## `vpt json-edit package.json devDependencies.vitest 5.1.1` + + +## `vpt write-file vite.config.ts 'export default () => { throw new Error('\''missing project setting'\''); }; +'` + + +## `vpt write-file .pnpmfile.cjs 'require('\''node:fs'\'').writeFileSync(require('\''node:path'\'').join(__dirname, '\''install-requested'\''), '\''install'\''); +module.exports = {}; +'` + + +## `vp migrate --no-interactive --no-hooks --no-agent --no-editor` + + +## `vpt print-file package.json` + +``` +{ + "devDependencies": { + "lint-policy": "file:./lint-policy", + "vite": "catalog:", + "vite-plus": "catalog:", + "vitest": "catalog:" + }, + "name": "migration-import-rule-install", + "packageManager": "pnpm@12.11.2", + "private": true, + "scripts": { + "build": "vp build" + }, + "type": "module" +} +``` + +## `vpt print-file unit.test.ts` + +``` +import { test } from 'vitest'; +test('application', () => {}); +``` + +## `node check-vite-peer.mjs` + +``` +The project and Vite+ share Vitest and Vite+ core +``` + +## `vpt cp pnpm-lock.yaml lockfile.before` + + +## `vpt stat-file install-requested --assert file` + +``` +install-requested: file +``` + +## `vpt rm install-requested` + + +## `vp migrate --no-interactive --no-hooks --no-agent --no-editor` + + +## `vpt stat-file install-requested --assert missing` + +``` +install-requested: missing +``` + +## `node -e 'const fs = require('\''node:fs'\''); require('\''node:assert/strict'\'').deepEqual(fs.readFileSync('\''pnpm-lock.yaml'\''), fs.readFileSync('\''lockfile.before'\''));'` + +``` +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots/unresolved_lint_config_skip_install.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots/unresolved_lint_config_skip_install.md new file mode 100644 index 0000000000..8f51ef4be8 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_install/snapshots/unresolved_lint_config_skip_install.md @@ -0,0 +1,48 @@ +# unresolved_lint_config_skip_install + +Skipping dependency installation still supplies the retained Vitest peer for a later pnpm install. + +## `vpt json-edit package.json devDependencies.vitest 5.1.1` + + +## `vpt write-file vite.config.ts 'export default () => { throw new Error('\''missing project setting'\''); }; +'` + + +## `vp migrate --no-interactive --no-hooks --no-agent --no-editor` + + +## `vpt stat-file node_modules --assert missing` + +``` +node_modules: missing +``` + +## `vpt print-file package.json` + +``` +{ + "devDependencies": { + "lint-policy": "file:./lint-policy", + "vite": "catalog:", + "vite-plus": "catalog:", + "vitest": "catalog:" + }, + "name": "migration-import-rule-install", + "packageManager": "pnpm@12.11.2", + "private": true, + "scripts": { + "build": "vp build" + }, + "type": "module" +} +``` + +## `pnpm install --no-frozen-lockfile --ignore-scripts` + + +## `node check-vite-peer.mjs` + +``` +The project and Vite+ share Vitest and Vite+ core +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_template_dependency/snapshots/migration_import_rule_preserved_dependency_owners.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_template_dependency/snapshots/migration_import_rule_preserved_dependency_owners.md index eb56b2a3e6..951315bd18 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_template_dependency/snapshots/migration_import_rule_preserved_dependency_owners.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_template_dependency/snapshots/migration_import_rule_preserved_dependency_owners.md @@ -53,6 +53,7 @@ import { test } from 'vitest'; test('starter', () => {}); { "devDependencies": { + "vite": "catalog:", "vitest": "catalog:", "vite-plus": "catalog:" }, @@ -73,6 +74,7 @@ export { page }; "devDependencies": { "@vitest/browser": "catalog:", "@vitest/browser-preview": "catalog:", + "vite": "catalog:", "vitest": "catalog:", "vite-plus": "catalog:" }, @@ -93,6 +95,7 @@ import 'vite-plus/test/browser-preview'; export { page }; { "devDependencies": { + "vite": "catalog:", "vitest": "catalog:", "vite-plus": "catalog:" }, diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_template_dependency/snapshots/migration_import_rule_template_dependency.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_template_dependency/snapshots/migration_import_rule_template_dependency.md index 4e65297ef1..1b8578048d 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_template_dependency/snapshots/migration_import_rule_template_dependency.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_import_rule_template_dependency/snapshots/migration_import_rule_template_dependency.md @@ -28,6 +28,7 @@ test('starter', () => {}); "private": true, "type": "module", "devDependencies": { + "vite": "catalog:", "vitest": "catalog:", "vite-plus": "catalog:" } @@ -102,6 +103,7 @@ This project is already using Vite+! Happy coding! "private": true, "type": "module", "devDependencies": { + "vite": "catalog:", "vitest": "catalog:", "vite-plus": "catalog:" } diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_monorepo_root_vitest_adjacent/snapshots/migration_monorepo_root_vitest_adjacent.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_monorepo_root_vitest_adjacent/snapshots/migration_monorepo_root_vitest_adjacent.md index 78e65ae87e..5168dbe052 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_monorepo_root_vitest_adjacent/snapshots/migration_monorepo_root_vitest_adjacent.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_monorepo_root_vitest_adjacent/snapshots/migration_monorepo_root_vitest_adjacent.md @@ -30,6 +30,7 @@ vitest pinned at the root even though vite-plus is injected first "prepare": "vp config" }, "devDependencies": { + "vite": "catalog:", "vitest-browser-svelte": "^2.1.0", "vite-plus": "catalog:", "vitest": "catalog:" diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/check-identity.mjs b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/check-identity.mjs index 5d1ef9cad9..58b3f1376e 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/check-identity.mjs +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/check-identity.mjs @@ -7,7 +7,7 @@ for (const directory of ['.', 'packages/utils', 'packages/app']) { const manifest = project('./package.json'); const cliPath = project.resolve('vite-plus/package.json'); const cli = createRequire(cliPath); - const vitestPath = cli.resolve('vitest/package.json'); + const vitestPath = cli.resolve('vitest'); const vitest = createRequire(vitestPath); const vitePath = cli.resolve('vite/package.json'); @@ -15,8 +15,17 @@ for (const directory of ['.', 'packages/utils', 'packages/app']) { // separate core instance, when its consumer has no direct Vite dependency. assert.equal(vitest.resolve('vite/package.json'), vitePath); assert.equal(cli('vite/package.json').name, '@voidzero-dev/vite-plus-core'); - if (directory === 'packages/app') { - assert.ok(manifest.devDependencies.vite, 'preserve the existing Vite dependency'); + const usesVitestUi = manifest.devDependencies['@vitest/ui'] !== undefined; + if (usesVitestUi) { + assert.ok(manifest.devDependencies.vitest); + assert.equal(project.resolve('vitest'), vitestPath); + } + if (directory === 'packages/utils' && usesVitestUi) { + assert.equal(manifest.peerDependencies.vite, '^7'); + assert.equal(manifest.devDependencies.vite, undefined); + assert.equal(project.resolve('vite/package.json'), vitePath); + } else if (directory === 'packages/app' || usesVitestUi) { + assert.ok(manifest.devDependencies.vite); assert.equal(project.resolve('vite/package.json'), vitePath); } else { assert.equal(manifest.devDependencies.vite, undefined, `${directory} needs no direct Vite`); @@ -28,5 +37,4 @@ for (const directory of ['.', 'packages/utils', 'packages/app']) { for (const [name, paths] of Object.entries(instances)) { assert.equal(paths.size, 1, `${name} must have one instance across the workspace`); } -console.log('Root and utils have no direct Vite; app keeps its declared Vite'); console.log('All packages share one Vite+, bundled Vite, and Vitest instance'); diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots.toml index 8b48964897..712de1cc23 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots.toml +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots.toml @@ -2,8 +2,9 @@ name = "pnpm_peer_identity_and_test_cache" vp = "global" local-registry = true -comment = "Regression for #1932: a root and utility package without direct Vite must share the app's core and Vitest instances, and test tasks must cache." +comment = "The root's direct Vitest and the utility package's bundled Vitest must share the app's core and runner, and test tasks must cache." steps = [ + { argv = ["vpt", "json-edit", "package.json", "devDependencies.@vitest/ui", "5.1.1"], snapshot = false }, { argv = ["vp", "migrate", "--full", "--no-interactive", "--no-hooks", "--no-agent", "--no-editor"], snapshot = false }, { argv = ["vp", "install", "--ignore-scripts"], snapshot = false }, ["node", "check-identity.mjs"], @@ -17,14 +18,34 @@ vp = "global" # Dependency installation is slow on Linux CI. skip-platforms = ["linux"] local-registry = true -comment = "The existing-Vite+ upgrade path must also preserve dependency identity and caching without injecting Vite, including on pnpm 10." +comment = "The pnpm 10 upgrade preserves the utility package's public Vite peer range and runs its direct Vitest with the same core and runner as Vite+." steps = [ { argv = ["vpt", "json-edit", "package.json", "packageManager", "pnpm@10.33.0"], snapshot = false }, + { argv = ["vpt", "json-edit", "packages/utils/package.json", "peerDependencies.vite", "^7"], snapshot = false }, + { argv = ["vpt", "json-edit", "packages/utils/package.json", "devDependencies.@vitest/ui", "5.1.1"], snapshot = false }, { argv = ["vp", "migrate", "--no-interactive"], snapshot = false }, { argv = ["vp", "install", "--ignore-scripts"], snapshot = false }, ["node", "check-identity.mjs"], { argv = ["vp", "run", "test"], cwd = "packages/utils", comment = "First run populates the test cache." }, { argv = ["vp", "run", "test"], cwd = "packages/utils", comment = "Second run must hit the cache." }, { argv = ["vp", "migrate", "--no-interactive"], snapshot = false }, - { argv = ["node", "check-identity.mjs"], comment = "Repeated migration must not inject Vite either." }, + { argv = ["node", "check-identity.mjs"], comment = "Repeated migration preserves the public peer range and the installed runner identity." }, +] + +[[case]] +name = "pnpm_optional_vite_peer_without_auto_install" +vp = "global" +local-registry = true +comment = "A workspace member with an optional public Vite peer resolves the same Vite and Vitest as Vite+ with automatic peer installation disabled." +steps = [ + { argv = ["vpt", "json-edit", "package.json", "packageManager", "pnpm@10.33.0"], snapshot = false }, + { argv = ["vpt", "replace-file-content", "pnpm-workspace.yaml", "autoInstallPeers: true", "autoInstallPeers: false\nresolvePeersFromWorkspaceRoot: false"], snapshot = false }, + { argv = ["vpt", "json-edit", "packages/utils/package.json", "peerDependencies.vite", "^7"], snapshot = false }, + { argv = ["vpt", "json-edit", "packages/utils/package.json", "peerDependenciesMeta.vite.optional", "true"], snapshot = false }, + { argv = ["vpt", "json-edit", "packages/utils/package.json", "devDependencies.@vitest/ui", "5.1.1"], snapshot = false }, + { argv = ["vp", "migrate", "--no-interactive"], snapshot = false }, + { argv = ["vp", "install", "--ignore-scripts"], snapshot = false }, + ["vpt", "grep-file", "pnpm-lock.yaml", "autoInstallPeers: false"], + ["vpt", "print-file", "packages/utils/package.json"], + ["node", "check-identity.mjs"], ] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots/pnpm10_upgrade_peer_identity_and_test_cache.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots/pnpm10_upgrade_peer_identity_and_test_cache.md index 9892fb62e3..6e8f68f611 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots/pnpm10_upgrade_peer_identity_and_test_cache.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots/pnpm10_upgrade_peer_identity_and_test_cache.md @@ -1,10 +1,16 @@ # pnpm10_upgrade_peer_identity_and_test_cache -The existing-Vite+ upgrade path must also preserve dependency identity and caching without injecting Vite, including on pnpm 10. +The pnpm 10 upgrade preserves the utility package's public Vite peer range and runs its direct Vitest with the same core and runner as Vite+. ## `vpt json-edit package.json packageManager pnpm@10.33.0` +## `vpt json-edit packages/utils/package.json peerDependencies.vite ^7` + + +## `vpt json-edit packages/utils/package.json devDependencies.@vitest/ui 5.1.1` + + ## `vp migrate --no-interactive` @@ -14,7 +20,6 @@ The existing-Vite+ upgrade path must also preserve dependency identity and cachi ## `node check-identity.mjs` ``` -Root and utils have no direct Vite; app keeps its declared Vite All packages share one Vite+, bundled Vite, and Vitest instance ``` @@ -64,9 +69,8 @@ vp run: cache hit, saved. ## `node check-identity.mjs` -Repeated migration must not inject Vite either. +Repeated migration preserves the public peer range and the installed runner identity. ``` -Root and utils have no direct Vite; app keeps its declared Vite All packages share one Vite+, bundled Vite, and Vitest instance ``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots/pnpm_optional_vite_peer_without_auto_install.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots/pnpm_optional_vite_peer_without_auto_install.md new file mode 100644 index 0000000000..453b934f70 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots/pnpm_optional_vite_peer_without_auto_install.md @@ -0,0 +1,60 @@ +# pnpm_optional_vite_peer_without_auto_install + +A workspace member with an optional public Vite peer resolves the same Vite and Vitest as Vite+ with automatic peer installation disabled. + +## `vpt json-edit package.json packageManager pnpm@10.33.0` + + +## `vpt replace-file-content pnpm-workspace.yaml 'autoInstallPeers: true' 'autoInstallPeers: false +resolvePeersFromWorkspaceRoot: false'` + + +## `vpt json-edit packages/utils/package.json peerDependencies.vite ^7` + + +## `vpt json-edit packages/utils/package.json peerDependenciesMeta.vite.optional true` + + +## `vpt json-edit packages/utils/package.json devDependencies.@vitest/ui 5.1.1` + + +## `vp migrate --no-interactive` + + +## `vp install --ignore-scripts` + + +## `vpt grep-file pnpm-lock.yaml 'autoInstallPeers: false'` + +``` +pnpm-lock.yaml: found "autoInstallPeers: false" +``` + +## `vpt print-file packages/utils/package.json` + +``` +{ + "devDependencies": { + "@vitest/ui": "catalog:", + "vite-plus": "catalog:", + "vitest": "catalog:" + }, + "name": "utils", + "peerDependencies": { + "vite": "^7" + }, + "peerDependenciesMeta": { + "vite": { + "optional": true + } + }, + "private": true, + "type": "module" +} +``` + +## `node check-identity.mjs` + +``` +All packages share one Vite+, bundled Vite, and Vitest instance +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots/pnpm_peer_identity_and_test_cache.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots/pnpm_peer_identity_and_test_cache.md index 3204d8b523..85511de936 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots/pnpm_peer_identity_and_test_cache.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_pnpm_vite_identity/snapshots/pnpm_peer_identity_and_test_cache.md @@ -1,6 +1,9 @@ # pnpm_peer_identity_and_test_cache -Regression for #1932: a root and utility package without direct Vite must share the app's core and Vitest instances, and test tasks must cache. +The root's direct Vitest and the utility package's bundled Vitest must share the app's core and runner, and test tasks must cache. + +## `vpt json-edit package.json devDependencies.@vitest/ui 5.1.1` + ## `vp migrate --full --no-interactive --no-hooks --no-agent --no-editor` @@ -11,7 +14,6 @@ Regression for #1932: a root and utility package without direct Vite must share ## `node check-identity.mjs` ``` -Root and utils have no direct Vite; app keeps its declared Vite All packages share one Vite+, bundled Vite, and Vitest instance ``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_rewrite_declare_module/snapshots/migration_rewrite_declare_module.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_rewrite_declare_module/snapshots/migration_rewrite_declare_module.md index de54e90796..d740ecbcf3 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_rewrite_declare_module/snapshots/migration_rewrite_declare_module.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_rewrite_declare_module/snapshots/migration_rewrite_declare_module.md @@ -61,6 +61,7 @@ check package.json { "name": "migration-rewrite-declare-module", "devDependencies": { + "vite": "catalog:", "vitest": "catalog:", "vite-plus": "catalog:" }, diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_peer_only_pnpm/snapshots/migration_upgrade_browser_peer_only_pnpm.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_peer_only_pnpm/snapshots/migration_upgrade_browser_peer_only_pnpm.md index 1ee7aa7d57..869ebf7a38 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_peer_only_pnpm/snapshots/migration_upgrade_browser_peer_only_pnpm.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_peer_only_pnpm/snapshots/migration_upgrade_browser_peer_only_pnpm.md @@ -24,6 +24,7 @@ provider, Playwright, and package-local Vitest are installed { "name": "migration-upgrade-browser-peer-only-pnpm", "devDependencies": { + "vite": "catalog:", "vite-plus": "catalog:", "@vitest/browser-playwright": "catalog:", "playwright": "*", diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_source_only_pnpm/snapshots/migration_upgrade_browser_source_only_pnpm.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_source_only_pnpm/snapshots/migration_upgrade_browser_source_only_pnpm.md index be418e27f8..8e065b83be 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_source_only_pnpm/snapshots/migration_upgrade_browser_source_only_pnpm.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_source_only_pnpm/snapshots/migration_upgrade_browser_source_only_pnpm.md @@ -30,6 +30,7 @@ provider, framework peer, and local vitest should be present { "name": "migration-upgrade-browser-source-only-pnpm", "devDependencies": { + "vite": "catalog:", "vite-plus": "catalog:", "@vitest/browser-playwright": "catalog:", "playwright": "*", diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/migrate_vitest4_community_provider.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/migrate_vitest4_community_provider.md index e0afce59c0..63da8c0fac 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/migrate_vitest4_community_provider.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/migrate_vitest4_community_provider.md @@ -23,7 +23,7 @@ VITE+ - The Unified Toolchain for the Web ## `vpt print-file package.json` ``` -{"name":"vitest4-browser","private":true,"type":"module","packageManager":"pnpm@10.33.0","devDependencies":{"vitest":"catalog:","@vitest/browser-webdriverio":"^5.0.0","webdriverio":"^9.20.0","vite-plus":"catalog:"}} +{"name":"vitest4-browser","private":true,"type":"module","packageManager":"pnpm@10.33.0","devDependencies":{"vite":"catalog:","vitest":"catalog:","@vitest/browser-webdriverio":"^5.0.0","webdriverio":"^9.20.0","vite-plus":"catalog:"}} ``` ## `vpt print-file vite.config.ts` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/migration_upgrade_browser_webdriverio_pnpm.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/migration_upgrade_browser_webdriverio_pnpm.md index 2264e3d0cf..a3dcfd4a4b 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/migration_upgrade_browser_webdriverio_pnpm.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/migration_upgrade_browser_webdriverio_pnpm.md @@ -30,6 +30,7 @@ ensure the provider and its framework peer are declared { "name": "migration-upgrade-browser-webdriverio-pnpm", "devDependencies": { + "vite": "catalog:", "vite-plus": "catalog:", "vitest": "catalog:", "@vitest/browser-webdriverio": "^5.0.0", diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/preserve_community_provider_version.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/preserve_community_provider_version.md index 0eff0f5412..3c18707a63 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/preserve_community_provider_version.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/preserve_community_provider_version.md @@ -27,6 +27,7 @@ VITE+ - The Unified Toolchain for the Web { "devDependencies": { "@vitest/browser-webdriverio": "^5.0.0", + "vite": "catalog:", "vite-plus": "catalog:", "vitest": "catalog:", "webdriverio": "*" diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/upgrade_community_provider_catalog.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/upgrade_community_provider_catalog.md index f93ad80924..c1fe589da6 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/upgrade_community_provider_catalog.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_browser_webdriverio_pnpm/snapshots/upgrade_community_provider_catalog.md @@ -41,6 +41,7 @@ VITE+ - The Unified Toolchain for the Web { "devDependencies": { "@vitest/browser-webdriverio": "catalog:browser", + "vite": "catalog:", "vite-plus": "catalog:", "webdriverio": "^9.20.0", "vitest": "catalog:" diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_monorepo_vitest_localized_pnpm/snapshots/migration_upgrade_monorepo_vitest_localized_pnpm.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_monorepo_vitest_localized_pnpm/snapshots/migration_upgrade_monorepo_vitest_localized_pnpm.md index 28c87514c2..4bae7f5e71 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_monorepo_vitest_localized_pnpm/snapshots/migration_upgrade_monorepo_vitest_localized_pnpm.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_monorepo_vitest_localized_pnpm/snapshots/migration_upgrade_monorepo_vitest_localized_pnpm.md @@ -46,6 +46,7 @@ only the peer consumer should gain local vitest "name": "app", "devDependencies": { "@vitest/ui": "catalog:", + "vite": "catalog:", "vite-plus": "catalog:", "vitest": "catalog:" } diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_nuxt_test_utils_monorepo/snapshots/migration_upgrade_nuxt_test_utils_monorepo.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_nuxt_test_utils_monorepo/snapshots/migration_upgrade_nuxt_test_utils_monorepo.md index fe95d68337..f0e5b86686 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_nuxt_test_utils_monorepo/snapshots/migration_upgrade_nuxt_test_utils_monorepo.md +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_upgrade_nuxt_test_utils_monorepo/snapshots/migration_upgrade_nuxt_test_utils_monorepo.md @@ -27,6 +27,7 @@ affected workspace keeps direct Vitest "private": true, "devDependencies": { "@nuxt/test-utils": "file:../../.fixture/nuxt-test-utils", + "vite": "catalog:", "vitest": "catalog:" } } diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_vitest_unmanaged_override/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_vitest_unmanaged_override/snapshots.toml index 3ebb1a7ddb..a279fd682b 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_vitest_unmanaged_override/snapshots.toml +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_vitest_unmanaged_override/snapshots.toml @@ -8,3 +8,15 @@ steps = [ { argv = ["vpt", "print-file", "pnpm-workspace.yaml"], comment = "no vitest catalog or override should be introduced", continue-on-failure = true }, { argv = ["vp", "migrate", "--no-interactive"], comment = "unmanaged Vitest ecosystem versions remain stable on rerun", continue-on-failure = true }, ] + +[[case]] +name = "unmanaged_vitest_without_vite" +vp = "global" +env = { VP_OVERRIDE_PACKAGES = "{\"vite\":\"npm:@voidzero-dev/vite-plus-core@latest\"}" } +steps = [ + { argv = ["vpt", "json-edit", "package.json", "devDependencies", "{\"@vitest/ui\":\"4.0.13\",\"vitest\":\"4.0.13\"}"], snapshot = false }, + { argv = ["vp", "migrate", "--no-interactive"], snapshot = false }, + { argv = ["vpt", "print-file", "package.json"], comment = "The project keeps its Vitest and @vitest/ui versions without gaining a direct Vite dependency." }, + { argv = ["vp", "migrate", "--no-interactive"], snapshot = false }, + ["vpt", "print-file", "package.json"], +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_vitest_unmanaged_override/snapshots/unmanaged_vitest_without_vite.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_vitest_unmanaged_override/snapshots/unmanaged_vitest_without_vite.md new file mode 100644 index 0000000000..89e52878b6 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/migration_vitest_unmanaged_override/snapshots/unmanaged_vitest_without_vite.md @@ -0,0 +1,46 @@ +# unmanaged_vitest_without_vite + +## `vpt json-edit package.json devDependencies '{"@vitest/ui":"4.0.13","vitest":"4.0.13"}'` + + +## `vp migrate --no-interactive` + + +## `vpt print-file package.json` + +The project keeps its Vitest and @vitest/ui versions without gaining a direct Vite dependency. + +``` +{ + "devDependencies": { + "@vitest/ui": "", + "vitest": "", + "vite-plus": "catalog:" + }, + "name": "migration-vitest-unmanaged-override", + "scripts": { + "test": "vp test", + "prepare": "vp config" + } +} +``` + +## `vp migrate --no-interactive` + + +## `vpt print-file package.json` + +``` +{ + "devDependencies": { + "@vitest/ui": "", + "vitest": "", + "vite-plus": "catalog:" + }, + "name": "migration-vitest-unmanaged-override", + "scripts": { + "test": "vp test", + "prepare": "vp config" + } +} +``` diff --git a/docs/guide/migrate-rules.md b/docs/guide/migrate-rules.md index 94a87b558c..1f4a883ec9 100644 --- a/docs/guide/migrate-rules.md +++ b/docs/guide/migrate-rules.md @@ -147,6 +147,10 @@ What happens to each toolchain dependency, at a glance: You can use `vite-plus` in a pnpm package without declaring `vite`. The `vite-plus` package includes a `vite` dependency aliased to its core, which provides Vitest's required peer. Migration keeps the workspace override for other Vite consumers but does not add `vite` to a package solely because it uses `vite-plus`. +When a pnpm package directly installs the Vite+-managed `vitest` and has no `vite` declaration, migration adds the core alias to its `devDependencies` for local testing. This gives Vitest the same Vite peer in both the project and Vite+, so plugins such as `@cloudflare/vitest-plugin` register tests with the runner used by `vp test`. Packages that already declare `peerDependencies.vite` keep their public peer range. If a later migration removes Vitest, it keeps the existing Vite dependency because the package may also use Vite directly. + +On pnpm 8 and 9 with `auto-install-peers=false`, a package that declares `vite` only in `peerDependencies` can be missing the core alias that its local Vitest needs. With automatic peer installation enabled, pnpm 8 can also install upstream Vite. In either case, keep the public peer range and add the workspace's Vite+ core alias to that package's `devDependencies.vite`. + Related rules: - A direct `vite` declaration is never removed merely because a root override exists. @@ -389,7 +393,7 @@ Migration converts the Volta `volta.node` pin to `.node-version`, because Vite+ - Migration does not add `vite` to `peerDependencyRules.allowAny` or `peerDependencyRules.allowedVersions`. - Migration removes `vite` from `allowAny` and removes `allowedVersions.vite` when its value is `'*'`, also when you wrote the entry. Those entries accepted the `1.x` core alias. Another `allowedVersions.vite` range, such as `'7 || 8'`, stays. Migration removes a field, or `peerDependencyRules`, when no entry remains. Other entries stay. The migrate summary names the removed rule. - For a preview build (`0.0.0-commit.`) or a Vite+ prerelease core (such as `8.4.0-vp.0`), migration keeps writing the `vite` rule. Package managers do not accept these versions for ranges such as `^8.0.0`. -- On pnpm before 10.2.0, migration also keeps writing the `vite` rule. These versions apply the `vite@*` alias override to peer ranges, so every `vite` peer fails against the core without the rule. +- On pnpm before 10.2.0, migration also keeps writing the `vite` rule because these versions apply the `vite@*` alias override to peer ranges. pnpm 10.0 and 10.1 can reject the resulting `npm:` peer spec with `ERR_PNPM_INVALID_PEER_DEPENDENCY_SPECIFICATION` before peer rules take effect. Upgrade to [pnpm 10.2.0](https://github.com/pnpm/pnpm/releases/tag/v10.2.0) or later to use the alias. - After migration, a plugin whose `vite` peer range excludes the bundled Vite (for example `^7.0.0`) produces a peer warning. With `strictPeerDependencies`, the install fails. Update the plugin, or widen the peer range of that plugin only: - pnpm 12.17.0 and later: add the override `'vite-plugin-foo>vite:peer': '*'`. pnpm deprecated `peerDependencyRules` in this version. - Earlier pnpm versions: add the rule `allowedVersions: { 'vite-plugin-foo>vite': '*' }`. diff --git a/packages/cli/src/migration/__tests__/migrator.spec.ts b/packages/cli/src/migration/__tests__/migrator.spec.ts index b306677972..6706a583a4 100644 --- a/packages/cli/src/migration/__tests__/migrator.spec.ts +++ b/packages/cli/src/migration/__tests__/migrator.spec.ts @@ -360,6 +360,21 @@ describe('rewritePackageJson', () => { // Vite+ now supplies its own Vite alias. The #1932 install-layout and cache // regression is covered by the migration_pnpm_vite_identity CLI fixture. describe('pnpm Vite dependencies', () => { + it.each([ + [true, 'catalog:'], + [false, VITE_PLUS_OVERRIDE_PACKAGES.vite], + ] as const)( + 'supplies the Vite peer for a direct Vitest install (catalogs: %s)', + (catalogs, vite) => { + const pkg = { devDependencies: { '@cloudflare/vitest-plugin': '1.8.4' } }; + rewritePackageJson(pkg, PackageManager.pnpm, catalogs); + expect(pkg.devDependencies).toMatchObject({ + vite, + vitest: catalogs ? 'catalog:' : VITEST_VERSION, + }); + }, + ); + it('does not add `vite` just because a package depends on vite-plus', () => { const sub = { devDependencies: { 'vite-plus': 'catalog:' }, @@ -405,7 +420,7 @@ describe('rewritePackageJson', () => { // A vite plugin that pins `vite` as a peer must keep its own contract; // injecting vite-plus-core as a concrete devDep would conflict with it. const pkg = { - devDependencies: { 'vite-plus': 'catalog:' }, + devDependencies: { 'vite-plus': 'catalog:', vitest: 'catalog:' }, peerDependencies: { vite: '^6.0.0' }, }; rewritePackageJson(pkg, PackageManager.pnpm, true); @@ -416,7 +431,7 @@ describe('rewritePackageJson', () => { it('does not add a second vite when an empty-string vite spec is already declared under pnpm', () => { const pkg = { dependencies: { vite: '' }, - devDependencies: { 'vite-plus': 'catalog:' }, + devDependencies: { 'vite-plus': 'catalog:', vitest: 'catalog:' }, }; rewritePackageJson(pkg, PackageManager.pnpm, true); expect(fixtureObject(pkg, 'devDependencies').vite).toBeUndefined(); @@ -658,18 +673,20 @@ describe('rewritePackageJson', () => { expect(pkg.devDependencies).not.toHaveProperty('vite'); }); - it('does not inject a direct vite for pnpm/yarn/bun browser projects', async () => { - for (const pm of [PackageManager.pnpm, PackageManager.yarn, PackageManager.bun]) { - const pkg = { - devDependencies: { - '@vitest/browser-playwright': '^4.0.0', - playwright: '^1.60.0', - vitest: '^4.0.0', - }, - }; - rewritePackageJson(pkg, pm); - expect(pkg.devDependencies).not.toHaveProperty('vite'); - } + it.each([ + [PackageManager.pnpm, VITE_PLUS_OVERRIDE_PACKAGES.vite], + [PackageManager.yarn, undefined], + [PackageManager.bun, undefined], + ] as const)('rewrites the Vite peer for a %s browser project', (pm, vite) => { + const pkg = { + devDependencies: { + '@vitest/browser-playwright': '^4.0.0', + playwright: '^1.60.0', + vitest: '^4.0.0', + }, + }; + rewritePackageJson(pkg, pm); + expect(fixtureObject(pkg, 'devDependencies').vite).toBe(vite); }); it('normalizes a pre-existing direct vite dep to the override target for an npm provider project', async () => { @@ -2513,6 +2530,86 @@ describe('ensureVitePlusBootstrap', () => { expect(pkg.devDependencies).toEqual({ 'vite-plus': 'catalog:' }); }); + it.each([true, false])( + 'reconciles a provisional pnpm Vitest on retry (import retained: %s)', + (retained) => { + const manifest = path.join(tmpDir, 'package.json'); + const workspace = makeWorkspaceInfo(tmpDir, PackageManager.pnpm, '10.26.2'); + fs.writeFileSync( + manifest, + JSON.stringify({ + name: 'app', + devDependencies: { 'vite-plus': 'latest', vitest: '^5', library: '1.0.0' }, + }), + ); + const imports = { vitest: new Set([tmpDir]), browser: new Map>() }; + ensureVitePlusBootstrap(workspace, undefined, imports, true); + expect(readJson(manifest).devDependencies).toEqual({ + library: '1.0.0', + 'vite-plus': 'catalog:', + vitest: 'catalog:', + }); + + // A retry has only the manifest left by the interrupted install. + const libraryDir = path.join(tmpDir, 'node_modules', 'library'); + fs.mkdirSync(libraryDir, { recursive: true }); + fs.writeFileSync( + path.join(libraryDir, 'package.json'), + JSON.stringify({ name: 'library', version: '1.0.0' }), + ); + const resolved = { ...imports, vitest: retained ? new Set([tmpDir]) : new Set() }; + ensureVitePlusBootstrap(workspace, undefined, resolved); + expect(readJson(manifest).devDependencies).toEqual( + retained + ? { library: '1.0.0', vite: 'catalog:', 'vite-plus': 'catalog:', vitest: 'catalog:' } + : { library: '1.0.0', 'vite-plus': 'catalog:' }, + ); + expect(ensureVitePlusBootstrap(workspace, undefined, resolved).changed).toBe(false); + }, + ); + + it('supplies the pnpm Vite peer before install for a known Vitest consumer', () => { + const workspace = makeWorkspaceInfo(tmpDir, PackageManager.pnpm, '10.26.2'); + const manifest = path.join(tmpDir, 'package.json'); + fs.writeFileSync( + manifest, + JSON.stringify({ + name: 'worker', + devDependencies: { '@cloudflare/vitest-plugin': '1.8.4', 'vite-plus': 'latest' }, + }), + ); + const imports = { vitest: new Set([tmpDir]), browser: new Map>() }; + ensureVitePlusBootstrap(workspace, undefined, imports, true); + expect(readJson(manifest).devDependencies).toMatchObject({ + vite: 'catalog:', + vitest: 'catalog:', + }); + }); + + it('repairs a missing Vite peer on a previously migrated pnpm Vitest project', () => { + const manifest = path.join(tmpDir, 'package.json'); + const workspace = makeWorkspaceInfo(tmpDir, PackageManager.pnpm, '10.26.2'); + fs.writeFileSync( + manifest, + JSON.stringify({ + name: 'worker', + devDependencies: { + '@cloudflare/vitest-plugin': '1.8.4', + 'vite-plus': 'catalog:', + vitest: 'catalog:', + }, + }), + ); + ensureVitePlusBootstrap(workspace); + const pkg = readJson(manifest); + delete fixtureObject(pkg, 'devDependencies').vite; + fs.writeFileSync(manifest, JSON.stringify(pkg)); + + checkBootstrapPass(workspace, true); + expect(readJson(manifest)).toHaveProperty(['devDependencies', 'vite'], 'catalog:'); + checkBootstrapPass(workspace, false); + }); + it('removes the stale vitest wrapper override for a non-vitest npm project', () => { fs.writeFileSync( path.join(tmpDir, 'package.json'), diff --git a/packages/cli/src/migration/bin.ts b/packages/cli/src/migration/bin.ts index ff7c6b4cbf..5c3d16a651 100644 --- a/packages/cli/src/migration/bin.ts +++ b/packages/cli/src/migration/bin.ts @@ -1,6 +1,6 @@ import fs from 'node:fs'; import path from 'node:path'; -import { isDeepStrictEqual, styleText } from 'node:util'; +import { styleText } from 'node:util'; import { confirm as promptConfirm, @@ -1365,18 +1365,27 @@ async function main() { // Runtime metadata can participate in the package manager's lockfile. let needsInstall = coreMigrationResult.dependencies || nodeUpgrades > 0; - async function reconcileBootstrap(dependencies: RetainedImportDependencies): Promise { + async function reconcileBootstrap( + dependencies: RetainedImportDependencies, + deferRetainedImportVitePeers = false, + ): Promise { const resolved = await ensureExistingPackageManager(); updateMigrationProgress('Configuring package manager'); return ensureVitePlusBootstrap( { ...workspaceInfoOptional, ...resolved }, report, dependencies, + deferRetainedImportVitePeers, ).changed; } - if (vitePlusBootstrapPending && (await reconcileBootstrap(retainedImportDependencies))) { - didMigrate = true; - needsInstall = true; + if (vitePlusBootstrapPending) { + const changed = await reconcileBootstrap( + retainedImportDependencies, + importRuleConfig === null, + ); + didMigrate ||= changed; + // Even if the only pending change was deferred, install to load the config. + needsInstall ||= changed || importRuleConfig === null; } let fixBaseUrl = false; @@ -1657,9 +1666,9 @@ async function main() { } } - async function installExistingDependencies(): Promise { + async function installExistingDependencies(message = 'Installing dependencies'): Promise { await ensureExistingPackageManager(); - updateMigrationProgress('Installing dependencies'); + updateMigrationProgress(message); const npmReinstallPreparation = packageManager === PackageManager.npm ? prepareNpmViteAliasReinstall( @@ -1721,15 +1730,17 @@ async function main() { oxlintOwnerDirs: deferredOxlintOwnerDirs, importRuleConfig: resolvedImportRuleConfig ?? DISABLED_IMPORT_RULE_CONFIG, }) || didMigrate; - // The first install used the owners we could identify without loading - // the config. Sync again only if the resolved rule changes dependencies. + // A skipped install still needs a complete manifest for the caller's next + // pnpm install. Only an install failure should leave these peers deferred. if ( - !isDeepStrictEqual(resolvedDependencies, retainedImportDependencies) && + needsInstall && + finalInstallOk && + (resolvedImportRuleConfig !== null || packageManager === PackageManager.pnpm) && bootstrapPending(resolvedDependencies) && (await reconcileBootstrap(resolvedDependencies)) ) { didMigrate = true; - await installExistingDependencies(); + await installExistingDependencies('Installing dependencies for retained imports'); } } diff --git a/packages/cli/src/migration/migrator/catalog.ts b/packages/cli/src/migration/migrator/catalog.ts index a50edc4c3f..f75257e1cc 100644 --- a/packages/cli/src/migration/migrator/catalog.ts +++ b/packages/cli/src/migration/migrator/catalog.ts @@ -55,6 +55,7 @@ import { } from '../migrator.ts'; import type { MigrationReport } from '../report.ts'; import type { + DependencyBag, PreparedOxlintConfigs, RetainedImportDependencies, PackageJsonDependencyField, @@ -66,6 +67,7 @@ import { PROVIDER_OVERRIDE_DROP_NAMES, OPT_IN_BROWSER_PROVIDERS, bundledPackagesToRemove, + findDeclaredSpec, VITEST_IS_MANAGED_OVERRIDE, isPlainRecord, pinnedPackageManagerVersion, @@ -744,6 +746,28 @@ export function setDirectViteEdge( Object.assign(target, Object.fromEntries(entries)); } +// pnpm can auto-install upstream Vite for a package-local Vitest despite the +// workspace override. Supply the same Vite peer as vite-plus so both resolve +// to one Vitest instance (#3619). +export function ensurePnpmVitestVitePeer( + pkg: DependencyBag, + packageManager: PackageManager, + supportCatalog: boolean, + catalogDependencyResolver?: CatalogDependencyResolver, +): void { + if ( + packageManager === PackageManager.pnpm && + VITEST_IS_MANAGED_OVERRIDE && + VITE_PLUS_OVERRIDE_PACKAGES.vite && + findDeclaredSpec(pkg, 'vite') === undefined && + [pkg.dependencies, pkg.devDependencies, pkg.optionalDependencies].some( + (deps) => deps?.vitest !== undefined, + ) + ) { + setDirectViteEdge(pkg, supportCatalog, catalogDependencyResolver); + } +} + // A peer declaration does not install Vitest and therefore must not keep a // workspace-wide managed Vitest catalog alive. Resolve its catalog reference to // the public peer range before that catalog is pruned, so the surviving peer diff --git a/packages/cli/src/migration/migrator/package-json.ts b/packages/cli/src/migration/migrator/package-json.ts index 8842463c28..2eb975fe61 100644 --- a/packages/cli/src/migration/migrator/package-json.ts +++ b/packages/cli/src/migration/migrator/package-json.ts @@ -11,6 +11,7 @@ import { import { VITEST_DIRECT_USAGE_EXCLUDED, alignVitestEcosystemPackages, + ensurePnpmVitestVitePeer, getAlignedVitestEcosystemDependencySpec, getCatalogDependencySpec, getScriptRulesYaml, @@ -394,9 +395,6 @@ export function rewritePackageJson( [VITE_PLUS_NAME]: canonicalVitePlusSpec, }; } - // Vite+ supplies its own `vite` alias for Vitest's peer dependency. Unlike - // the pre-#2617 package layout, pnpm consumers do not need an extra direct - // Vite dependency just to use Vite+. Keep existing declarations above. // Add `vitest` as a direct devDependency when: // - a remaining dependency likely peer-depends on vitest (e.g. // vitest-browser-svelte), OR @@ -405,9 +403,7 @@ export function rewritePackageJson( // Vite-plus already bundles upstream vitest as a direct dep, but a strict // pnpm / yarn Plug'n'Play layout will not expose that transitive `vitest` // to the package. Pinning it here points the dep at the same upstream - // version vite-plus ships with. Gated by needDirectVitest (browser-mode / - // vitest-adjacent, or some other change) — a pure normalize pass must not - // mutate the project beyond the vite-plus spec. + // version vite-plus ships with. if (needDirectVitest) { const installableDeps = { ...pkg.dependencies, @@ -435,6 +431,9 @@ export function rewritePackageJson( ); } } + // An existing direct Vitest needs its peer even when no new Vitest was added. + // First-time migration and project creation both install after rewriting manifests. + ensurePnpmVitestVitePeer(pkg, packageManager, supportCatalog, catalogDependencyResolver); return extractedStagedConfig; } diff --git a/packages/cli/src/migration/migrator/vite-plus-bootstrap.ts b/packages/cli/src/migration/migrator/vite-plus-bootstrap.ts index bcb0737a07..6bf0c2da80 100644 --- a/packages/cli/src/migration/migrator/vite-plus-bootstrap.ts +++ b/packages/cli/src/migration/migrator/vite-plus-bootstrap.ts @@ -19,6 +19,7 @@ import { editYamlFile, readYamlFile } from '../../utils/yaml.ts'; import type { PnpmPeerDependencyRuleKeys } from '../migrator.ts'; import { alignVitestEcosystemPackages, + ensurePnpmVitestVitePeer, applyBuildAllowanceToPackageJsonPnpm, applyYarnWorkspaceHoistingFix, collectProviderSourceModes, @@ -44,6 +45,7 @@ import { bunSupportsCatalog, pnpmWorkspaceMinimumReleaseAgeExemptionsPending, pnpmWorkspaceTsrsTrustPolicyExceptionPending, + projectListsRequiredVitestPeer, projectUsesVitestDirectly, pruneLegacyWrapperAliases, readPnpmWorkspaceCatalogDependencyResolver, @@ -304,11 +306,22 @@ function reconcileVitePlusBootstrapPackage( // An installed provider references the shared catalog entry added for a sibling. browserCatalogAdditions: ReadonlySet = new Set(), retainedImportDependencies?: RetainedImportDependencies, + deferRetainedImportVitePeers = false, ): boolean { const before = JSON.stringify(pkg); - const usesVitest = - retainedImportDependencies?.vitest.has(projectPath) || - projectUsesVitestDirectly(projectPath, pkg); + const retainedVitestImport = retainedImportDependencies?.vitest.has(projectPath); + const usesVitest = retainedVitestImport || projectUsesVitestDirectly(projectPath, pkg); + // An unresolved import rule can only affect ordinary imports. Browser mode, + // required peers and always-preserved references already require local Vitest. + const deferVitePeer = + packageManager === PackageManager.pnpm && + deferRetainedImportVitePeers && + retainedVitestImport && + !projectUsesVitestDirectly( + projectPath, + pkg, + projectListsRequiredVitestPeer(projectPath, pkg, { preserveIfMetadataUnavailable: false }), + ); ensureVitePlusDependencySpecs(pkg, vitePlusVersion, ensureVitePlus); const installGroups = [pkg.devDependencies, pkg.dependencies, pkg.optionalDependencies]; @@ -495,6 +508,9 @@ function reconcileVitePlusBootstrapPackage( } } + if (!deferVitePeer) { + ensurePnpmVitestVitePeer(pkg, packageManager, supportCatalog, catalogDependencyResolver); + } return before !== JSON.stringify(pkg); } @@ -969,6 +985,7 @@ export function ensureVitePlusBootstrap( workspaceInfo: WorkspaceInfo, report?: MigrationReport, retainedImportDependencies?: RetainedImportDependencies, + deferRetainedImportVitePeers = false, ): VitePlusBootstrapResult { const projectPath = workspaceInfo.rootDir; const packageJsonPath = path.join(projectPath, 'package.json'); @@ -1084,6 +1101,7 @@ export function ensureVitePlusBootstrap( catalogDependencyResolver, browserCatalogAdditions, retainedImportDependencies, + deferRetainedImportVitePeers, ); if (workspaceInfo.packageManager === PackageManager.yarn) { @@ -1184,6 +1202,7 @@ export function ensureVitePlusBootstrap( catalogDependencyResolver, browserCatalogAdditions, retainedImportDependencies, + deferRetainedImportVitePeers, ); if ( yarnHoisting && diff --git a/packages/cli/src/migration/migrator/vitest-ecosystem.ts b/packages/cli/src/migration/migrator/vitest-ecosystem.ts index a5b05b3d67..252316c6dd 100644 --- a/packages/cli/src/migration/migrator/vitest-ecosystem.ts +++ b/packages/cli/src/migration/migrator/vitest-ecosystem.ts @@ -443,6 +443,7 @@ export function projectListsRequiredVitestPeer( devDependencies?: Record; optionalDependencies?: Record; }, + { preserveIfMetadataUnavailable = true } = {}, ): boolean { const installGroups = [pkg.dependencies, pkg.devDependencies, pkg.optionalDependencies]; const hasExistingVitest = installGroups.some( @@ -487,7 +488,7 @@ export function projectListsRequiredVitestPeer( // unknown, preserve it rather than risk removing the provider for an // arbitrary integration such as vite-plugin-gherkin. A later migration with // complete metadata can safely remove a genuinely redundant pin. - return metadataUnavailable && hasExistingVitest; + return preserveIfMetadataUnavailable && metadataUnavailable && hasExistingVitest; } // True iff the project uses vitest DIRECTLY — via a dependency that is expected