From 8526ac3281052f371fc7cf214d6a6a33248a89dd Mon Sep 17 00:00:00 2001 From: ReemX Date: Sat, 21 Nov 2026 13:18:41 +0200 Subject: [PATCH] feat(workspace): support pnpm's changed-since filter selector `vp run --filter "[]"` now selects the packages with a file changed since a git revision, as pnpm does, and combines with the rest of the selector: `...[]`, `[]...`, the `^` forms, `name[]`, `{dir}[]` and `!...[]`. Before, `[]` was parsed as a glob character class, matched nothing and exited 0. Detection follows pnpm's get_changed_projects: git diff --name-only against the merge base of and HEAD, so uncommitted changes to tracked files count and untracked files don't; each file belongs to the deepest package that contains it, the root package for files outside every other one. When pnpm-workspace.yaml changed, the packages that take a changed catalog entry count as changed too. An unknown revision, a workspace outside a git repository, or no merge base (as in a shallow clone) is an error instead of an empty match. A trailing `[...]` group is now always the revision, as in pnpm's selector regex, so `foo[ab]` is no longer a glob; `foo[ab]*` still is. Refs voidzero-dev/vite-plus#2903. --- CHANGELOG.md | 1 + .../filter_changed_since/package.json | 4 + .../packages/app/package.json | 10 + .../packages/app/src/index.js | 1 + .../packages/lib/package.json | 9 + .../packages/lib/src/index.js | 1 + .../packages/utils/package.json | 6 + .../packages/utils/src/index.js | 1 + .../filter_changed_since/pnpm-workspace.yaml | 4 + .../filter_changed_since/snapshots.toml | 251 +++++++++++ .../catalog_change_selects_its_users.md | 40 ++ ...nged_leaf_selects_it_and_its_dependents.md | 56 +++ .../outside_git_repository_errors.md | 12 + .../snapshots/uncommitted_change_counts.md | 30 ++ .../snapshots/unknown_revision_errors.md | 27 ++ crates/vt_plan/tests/plan_snapshots/README.md | 19 + .../filter_changed_since/package.json | 7 + .../packages/app/package.json | 10 + .../packages/app/src/index.js | 1 + .../packages/docs/package.json | 9 + .../packages/docs/src/index.js | 1 + .../packages/lib/package.json | 9 + .../packages/lib/src/index.js | 1 + .../packages/utils/package.json | 7 + .../packages/utils/src/index.js | 1 + .../filter_changed_since/pnpm-workspace.yaml | 7 + .../filter_changed_since/snapshots.toml | 68 +++ .../query_changed_since_catalog_only.jsonc | 5 + ...d_since_catalog_only_in_packages_dir.jsonc | 4 + ...nged_since_directory_dependents_only.jsonc | 7 + .../query_changed_since_exclusion_only.jsonc | 5 + ...y_changed_since_head_selects_nothing.jsonc | 2 + ...query_changed_since_name_not_changed.jsonc | 2 + ...changed_since_name_with_dependencies.jsonc | 10 + ...ry_changed_since_option_like_revision.snap | 1 + .../query_changed_since_unknown_revision.snap | 1 + .../query_changed_since_with_dependents.jsonc | 11 + .../query_changed_since_with_exclusion.jsonc | 7 + .../snapshots/task_graph.md | 211 +++++++++ .../fixtures/filter_workspace/snapshots.toml | 5 + ..._changed_since_outside_git_repository.snap | 1 + crates/vt_plan/tests/plan_snapshots/main.rs | 52 +++ crates/vt_workspace/src/changed_since.rs | 416 ++++++++++++++++++ crates/vt_workspace/src/lib.rs | 2 + crates/vt_workspace/src/package.rs | 9 +- crates/vt_workspace/src/package_filter.rs | 255 ++++++++++- crates/vt_workspace/src/package_graph.rs | 380 +++++++++++++++- 47 files changed, 1968 insertions(+), 11 deletions(-) create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/package.json create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/app/package.json create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/app/src/index.js create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/lib/package.json create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/lib/src/index.js create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/utils/package.json create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/utils/src/index.js create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/pnpm-workspace.yaml create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots.toml create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/catalog_change_selects_its_users.md create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/changed_leaf_selects_it_and_its_dependents.md create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/outside_git_repository_errors.md create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/uncommitted_change_counts.md create mode 100644 crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/unknown_revision_errors.md create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/package.json create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/app/package.json create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/app/src/index.js create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/docs/package.json create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/docs/src/index.js create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/lib/package.json create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/lib/src/index.js create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/utils/package.json create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/utils/src/index.js create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/pnpm-workspace.yaml create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots.toml create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_catalog_only.jsonc create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_catalog_only_in_packages_dir.jsonc create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_directory_dependents_only.jsonc create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_exclusion_only.jsonc create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_head_selects_nothing.jsonc create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_name_not_changed.jsonc create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_name_with_dependencies.jsonc create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_option_like_revision.snap create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_unknown_revision.snap create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_with_dependents.jsonc create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_with_exclusion.jsonc create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/task_graph.md create mode 100644 crates/vt_plan/tests/plan_snapshots/fixtures/filter_workspace/snapshots/query_filter_changed_since_outside_git_repository.snap create mode 100644 crates/vt_workspace/src/changed_since.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d5cfa856..6fea1ab69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Changelog +- **Added** `vp run --filter` supports pnpm's changed-since selector: `--filter "[]"` selects the packages with a file changed since the git revision ``, and combines with the other selector parts, as in `...[origin/main]` (plus their dependents), `[origin/main]...` (plus their dependencies), `foo[origin/main]` or `{packages/*}[origin/main]` (only those that changed) and `!...[origin/main]`. As in pnpm, changes are counted from the commit where the current branch forked from ``, uncommitted changes to tracked files count, and a package that takes a changed catalog entry in `pnpm-workspace.yaml` counts as changed. An unknown revision, a workspace outside a git repository, or a `` with no common history with `HEAD` (as in a shallow clone) is an error instead of an empty match. A trailing `[...]` in a filter is now always the revision, so a filter like `foo[ab]` no longer matches `fooa` and `foob` ([vite-plus#2903](https://github.com/voidzero-dev/vite-plus/issues/2903), [#1009](https://github.com/voidzero-dev/vite-task/pull/1009)). - **Fixed** On Windows, tasks with automatic file-access tracking can now run Git Bash's `sh` and other MSYS2 or Cygwin programs, which aborted with `fatal error - MapViewOfFileEx 'shared.5'(…) Win32 error 487` ([#830](https://github.com/voidzero-dev/vite-task/issues/830), [#952](https://github.com/voidzero-dev/vite-task/pull/952)). - **Fixed** On Linux, cached tasks no longer fail to spawn child processes in restricted sandboxes, for example rootless bubblewrap that denies the `seccomp` syscall. When the `seccomp` filter for file-access tracking cannot be installed, the process runs untracked instead and the run is reported as not cached. The not-cached message now reads "automatic file tracking couldn't record everything this task did", which fits both this case and a full tracking channel ([#700](https://github.com/voidzero-dev/vite-task/issues/700), [#701](https://github.com/voidzero-dev/vite-task/pull/701)). - **Fixed** `vp run ` in a package that doesn't define the task now reports `Task "" not found` when another package's `vite.config.*` fails to load, instead of failing with that package's error. Showing the tasks to choose from loads every package's config, so the task selector or task list is skipped then ([#987](https://github.com/voidzero-dev/vite-task/pull/987)). diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/package.json b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/package.json new file mode 100644 index 000000000..250da234e --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/package.json @@ -0,0 +1,4 @@ +{ + "name": "filter-changed-since-test", + "private": true +} diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/app/package.json b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/app/package.json new file mode 100644 index 000000000..a1a12c162 --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/app/package.json @@ -0,0 +1,10 @@ +{ + "name": "@test/app", + "scripts": { + "build": "vtt print built-app" + }, + "dependencies": { + "@test/lib": "workspace:*", + "react": "catalog:" + } +} diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/app/src/index.js b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/app/src/index.js new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/app/src/index.js @@ -0,0 +1 @@ +export {}; diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/lib/package.json b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/lib/package.json new file mode 100644 index 000000000..f17e3b919 --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/lib/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/lib", + "scripts": { + "build": "vtt print built-lib" + }, + "dependencies": { + "@test/utils": "workspace:*" + } +} diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/lib/src/index.js b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/lib/src/index.js new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/lib/src/index.js @@ -0,0 +1 @@ +export {}; diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/utils/package.json b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/utils/package.json new file mode 100644 index 000000000..ab9f0c043 --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/utils/package.json @@ -0,0 +1,6 @@ +{ + "name": "@test/utils", + "scripts": { + "build": "vtt print built-utils" + } +} diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/utils/src/index.js b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/utils/src/index.js new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/packages/utils/src/index.js @@ -0,0 +1 @@ +export {}; diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/pnpm-workspace.yaml b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/pnpm-workspace.yaml new file mode 100644 index 000000000..abbbc0689 --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/pnpm-workspace.yaml @@ -0,0 +1,4 @@ +packages: + - packages/* +catalog: + react: ^18.0.0 diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots.toml b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots.toml new file mode 100644 index 000000000..33b415185 --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots.toml @@ -0,0 +1,251 @@ +# pnpm's changed-since `--filter "[]"` selector, against a real git +# repository created in the staged fixture. Dependencies: app -> lib -> utils. + +[[e2e]] +name = "changed_leaf_selects_it_and_its_dependents" +comment = """ +A commit changes only the leaf `@test/utils`. `...[HEAD~1]` selects it and every package that depends on it, and builds them in dependency order. +""" +steps = [ + [ + "git", + "-c", + "init.defaultBranch=main", + "init", + "-q", + ], + [ + "git", + "-c", + "core.autocrlf=false", + "add", + "-A", + ], + [ + "git", + "-c", + "user.name=test", + "-c", + "user.email=test@example.com", + "-c", + "commit.gpgsign=false", + "commit", + "-q", + "--no-verify", + "-m", + "init", + ], + [ + "vtt", + "replace-file-content", + "packages/utils/src/index.js", + "export {};", + "export const changed = 1;", + ], + [ + "git", + "-c", + "core.autocrlf=false", + "add", + "-A", + ], + [ + "git", + "-c", + "user.name=test", + "-c", + "user.email=test@example.com", + "-c", + "commit.gpgsign=false", + "commit", + "-q", + "--no-verify", + "-m", + "change", + ], + [ + "vt", + "run", + "--filter", + "...[HEAD~1]", + "build", + ], + [ + "vt", + "run", + "--filter", + "[HEAD~1]", + "build", + ], +] + +[[e2e]] +name = "uncommitted_change_counts" +comment = """ +An uncommitted change to a tracked file counts as a change since `HEAD`. +""" +steps = [ + [ + "git", + "-c", + "init.defaultBranch=main", + "init", + "-q", + ], + [ + "git", + "-c", + "core.autocrlf=false", + "add", + "-A", + ], + [ + "git", + "-c", + "user.name=test", + "-c", + "user.email=test@example.com", + "-c", + "commit.gpgsign=false", + "commit", + "-q", + "--no-verify", + "-m", + "init", + ], + [ + "vtt", + "replace-file-content", + "packages/lib/src/index.js", + "export {};", + "export const changed = 1;", + ], + [ + "vt", + "run", + "--filter", + "[HEAD]", + "build", + ], +] + +[[e2e]] +name = "catalog_change_selects_its_users" +comment = """ +A commit changes only the default catalog's `react` in `pnpm-workspace.yaml`. `@test/app`, which takes `react: catalog:`, counts as changed, and so does the root package that holds the file (it has no `build`). +""" +steps = [ + [ + "git", + "-c", + "init.defaultBranch=main", + "init", + "-q", + ], + [ + "git", + "-c", + "core.autocrlf=false", + "add", + "-A", + ], + [ + "git", + "-c", + "user.name=test", + "-c", + "user.email=test@example.com", + "-c", + "commit.gpgsign=false", + "commit", + "-q", + "--no-verify", + "-m", + "init", + ], + [ + "vtt", + "replace-file-content", + "pnpm-workspace.yaml", + "react: ^18.0.0", + "react: ^19.0.0", + ], + [ + "git", + "-c", + "core.autocrlf=false", + "add", + "-A", + ], + [ + "git", + "-c", + "user.name=test", + "-c", + "user.email=test@example.com", + "-c", + "commit.gpgsign=false", + "commit", + "-q", + "--no-verify", + "-m", + "change", + ], + [ + "vt", + "run", + "--filter", + "[HEAD~1]", + "build", + ], +] + +[[e2e]] +name = "unknown_revision_errors" +comment = """ +A revision git doesn't know is an error, not an empty match that exits 0. +""" +steps = [ + [ + "git", + "-c", + "init.defaultBranch=main", + "init", + "-q", + ], + [ + "git", + "-c", + "core.autocrlf=false", + "add", + "-A", + ], + [ + "git", + "-c", + "user.name=test", + "-c", + "user.email=test@example.com", + "-c", + "commit.gpgsign=false", + "commit", + "-q", + "--no-verify", + "-m", + "init", + ], + [ + "vt", + "run", + "--filter", + "...[no-such-branch]", + "build", + ], +] + +[[e2e]] +name = "outside_git_repository_errors" +comment = """ +Outside a git repository, `[]` is an error, not an empty match that exits 0. +""" +steps = [["vt", "run", "--filter", "...[HEAD~1]", "build"]] diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/catalog_change_selects_its_users.md b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/catalog_change_selects_its_users.md new file mode 100644 index 000000000..0c5fb272e --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/catalog_change_selects_its_users.md @@ -0,0 +1,40 @@ +# catalog_change_selects_its_users + +A commit changes only the default catalog's `react` in `pnpm-workspace.yaml`. `@test/app`, which takes `react: catalog:`, counts as changed, and so does the root package that holds the file (it has no `build`). + +## `git -c init.defaultBranch=main init -q` + +``` +``` + +## `git -c core.autocrlf=false add -A` + +``` +``` + +## `git -c user.name=test -c user.email=test@example.com -c commit.gpgsign=false commit -q --no-verify -m init` + +``` +``` + +## `vtt replace-file-content pnpm-workspace.yaml 'react: ^18.0.0' 'react: ^19.0.0'` + +``` +``` + +## `git -c core.autocrlf=false add -A` + +``` +``` + +## `git -c user.name=test -c user.email=test@example.com -c commit.gpgsign=false commit -q --no-verify -m change` + +``` +``` + +## `vt run --filter [HEAD~1] build` + +``` +~/packages/app$ vtt print built-app ⊘ cache disabled +built-app +``` diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/changed_leaf_selects_it_and_its_dependents.md b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/changed_leaf_selects_it_and_its_dependents.md new file mode 100644 index 000000000..054abdded --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/changed_leaf_selects_it_and_its_dependents.md @@ -0,0 +1,56 @@ +# changed_leaf_selects_it_and_its_dependents + +A commit changes only the leaf `@test/utils`. `...[HEAD~1]` selects it and every package that depends on it, and builds them in dependency order. + +## `git -c init.defaultBranch=main init -q` + +``` +``` + +## `git -c core.autocrlf=false add -A` + +``` +``` + +## `git -c user.name=test -c user.email=test@example.com -c commit.gpgsign=false commit -q --no-verify -m init` + +``` +``` + +## `vtt replace-file-content packages/utils/src/index.js 'export {};' 'export const changed = 1;'` + +``` +``` + +## `git -c core.autocrlf=false add -A` + +``` +``` + +## `git -c user.name=test -c user.email=test@example.com -c commit.gpgsign=false commit -q --no-verify -m change` + +``` +``` + +## `vt run --filter ...[HEAD~1] build` + +``` +~/packages/utils$ vtt print built-utils ⊘ cache disabled +built-utils + +~/packages/lib$ vtt print built-lib ⊘ cache disabled +built-lib + +~/packages/app$ vtt print built-app ⊘ cache disabled +built-app + +--- +vt run: 3/3 successful. (Run `vt run --last-details` for full details) +``` + +## `vt run --filter [HEAD~1] build` + +``` +~/packages/utils$ vtt print built-utils ⊘ cache disabled +built-utils +``` diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/outside_git_repository_errors.md b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/outside_git_repository_errors.md new file mode 100644 index 000000000..accba103f --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/outside_git_repository_errors.md @@ -0,0 +1,12 @@ +# outside_git_repository_errors + +Outside a git repository, `[]` is an error, not an empty match that exits 0. + +## `vt run --filter ...[HEAD~1] build` + +**Exit code:** 1 + +``` +error: Failed to find the packages changed since 'HEAD~1' +* is not in a git repository: fatal: not a git repository (or any of the parent directories): .git +``` diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/uncommitted_change_counts.md b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/uncommitted_change_counts.md new file mode 100644 index 000000000..e87d049c2 --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/uncommitted_change_counts.md @@ -0,0 +1,30 @@ +# uncommitted_change_counts + +An uncommitted change to a tracked file counts as a change since `HEAD`. + +## `git -c init.defaultBranch=main init -q` + +``` +``` + +## `git -c core.autocrlf=false add -A` + +``` +``` + +## `git -c user.name=test -c user.email=test@example.com -c commit.gpgsign=false commit -q --no-verify -m init` + +``` +``` + +## `vtt replace-file-content packages/lib/src/index.js 'export {};' 'export const changed = 1;'` + +``` +``` + +## `vt run --filter [HEAD] build` + +``` +~/packages/lib$ vtt print built-lib ⊘ cache disabled +built-lib +``` diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/unknown_revision_errors.md b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/unknown_revision_errors.md new file mode 100644 index 000000000..236189271 --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/filter_changed_since/snapshots/unknown_revision_errors.md @@ -0,0 +1,27 @@ +# unknown_revision_errors + +A revision git doesn't know is an error, not an empty match that exits 0. + +## `git -c init.defaultBranch=main init -q` + +``` +``` + +## `git -c core.autocrlf=false add -A` + +``` +``` + +## `git -c user.name=test -c user.email=test@example.com -c commit.gpgsign=false commit -q --no-verify -m init` + +``` +``` + +## `vt run --filter ...[no-such-branch] build` + +**Exit code:** 1 + +``` +error: Failed to find the packages changed since 'no-such-branch' +* Unknown git revision 'no-such-branch' +``` diff --git a/crates/vt_plan/tests/plan_snapshots/README.md b/crates/vt_plan/tests/plan_snapshots/README.md index 235415583..14df355e2 100644 --- a/crates/vt_plan/tests/plan_snapshots/README.md +++ b/crates/vt_plan/tests/plan_snapshots/README.md @@ -65,6 +65,25 @@ plan. The option applies to non-compact plan cases in the file; it is not accept inside `[[plan]]`. Cases with `compact = true` keep their compact format and ignore `fields`. Task graph Markdown and error snapshots are unaffected. +## Git history + +A fixture that needs a git repository, such as one testing `--filter "[]"`, +sets `git` at the file level. The staged fixture is committed into a new +repository on `main`, then each entry of `commits` writes its files and is +committed on top: + +```toml +[[git.commits]] +files = { "packages/utils/src/index.js" = "export const changed = 1 +" } + +[[plan]] +name = "changed_since_with_dependents" +args = ["run", "--filter", "...[HEAD~1]", "build"] +``` + +Without `git`, the staged fixture is not in a git repository. + ## Adding a new test 1. Create a new fixture directory under `fixtures/` diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/package.json b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/package.json new file mode 100644 index 000000000..aac789ff7 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/package.json @@ -0,0 +1,7 @@ +{ + "name": "monorepo", + "private": true, + "scripts": { + "build": "echo building monorepo" + } +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/app/package.json b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/app/package.json new file mode 100644 index 000000000..add09b026 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/app/package.json @@ -0,0 +1,10 @@ +{ + "name": "app", + "scripts": { + "build": "echo building app" + }, + "dependencies": { + "lib": "workspace:*", + "react": "catalog:" + } +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/app/src/index.js b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/app/src/index.js new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/app/src/index.js @@ -0,0 +1 @@ +export {}; diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/docs/package.json b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/docs/package.json new file mode 100644 index 000000000..0e90e227f --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/docs/package.json @@ -0,0 +1,9 @@ +{ + "name": "docs", + "scripts": { + "build": "echo building docs" + }, + "dependencies": { + "lodash": "catalog:legacy" + } +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/docs/src/index.js b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/docs/src/index.js new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/docs/src/index.js @@ -0,0 +1 @@ +export {}; diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/lib/package.json b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/lib/package.json new file mode 100644 index 000000000..5eff4eef6 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/lib/package.json @@ -0,0 +1,9 @@ +{ + "name": "lib", + "scripts": { + "build": "echo building lib" + }, + "dependencies": { + "utils": "workspace:*" + } +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/lib/src/index.js b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/lib/src/index.js new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/lib/src/index.js @@ -0,0 +1 @@ +export {}; diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/utils/package.json b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/utils/package.json new file mode 100644 index 000000000..1f5ce13fe --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/utils/package.json @@ -0,0 +1,7 @@ +{ + "name": "utils", + "scripts": { + "build": "echo building utils" + }, + "dependencies": {} +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/utils/src/index.js b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/utils/src/index.js new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/packages/utils/src/index.js @@ -0,0 +1 @@ +export {}; diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/pnpm-workspace.yaml b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/pnpm-workspace.yaml new file mode 100644 index 000000000..3ae2a48c4 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/pnpm-workspace.yaml @@ -0,0 +1,7 @@ +packages: + - packages/* +catalog: + react: ^18.0.0 +catalogs: + legacy: + lodash: ^4.0.0 diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots.toml b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots.toml new file mode 100644 index 000000000..5da88934f --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots.toml @@ -0,0 +1,68 @@ +# Tests pnpm's changed-since `--filter "[]"` selector. +# +# History: the fixture (HEAD~2), then a change in the leaf `utils` (HEAD~1), +# then a bump of the default catalog's `react`, which `app` takes (HEAD). +# Dependencies: app -> lib -> utils; docs takes lodash from the `legacy` catalog. + +[[git.commits]] +files = { "packages/utils/src/index.js" = "export const changed = 1;\n" } + +[[git.commits]] +files = { "pnpm-workspace.yaml" = "packages:\n - packages/*\ncatalog:\n react: ^19.0.0\ncatalogs:\n legacy:\n lodash: ^4.0.0\n" } + +# Only pnpm-workspace.yaml changed: its owner (the root) and the package that +# takes the changed catalog entry are selected; docs uses another catalog. +[[plan]] +compact = true +name = "changed_since_catalog_only" +args = ["run", "--filter", "[HEAD~1]", "build"] + +[[plan]] +compact = true +name = "changed_since_catalog_only_in_packages_dir" +args = ["run", "--filter", "{packages/*}[HEAD~1]", "build"] + +# The changed leaf, the catalog change, and every dependent. +[[plan]] +compact = true +name = "changed_since_with_dependents" +args = ["run", "--filter", "...[HEAD~2]", "build"] + +[[plan]] +compact = true +name = "changed_since_directory_dependents_only" +args = ["run", "--filter", "...^{packages/utils}[HEAD~2]", "build"] + +[[plan]] +compact = true +name = "changed_since_name_with_dependencies" +args = ["run", "--filter", "app[HEAD~2]...", "build"] + +# docs didn't change, so the name narrows the changed packages to nothing. +[[plan]] +compact = true +name = "changed_since_name_not_changed" +args = ["run", "--filter", "docs[HEAD~2]", "build"] + +[[plan]] +compact = true +name = "changed_since_with_exclusion" +args = ["run", "--filter", "...[HEAD~2]", "--filter", "!...[HEAD~1]", "build"] + +[[plan]] +compact = true +name = "changed_since_exclusion_only" +args = ["run", "--filter", "![HEAD~2]", "build"] + +[[plan]] +compact = true +name = "changed_since_head_selects_nothing" +args = ["run", "--filter", "[HEAD]", "build"] + +[[plan]] +name = "changed_since_unknown_revision" +args = ["run", "--filter", "...[no-such-branch]", "build"] + +[[plan]] +name = "changed_since_option_like_revision" +args = ["run", "--filter", "[--output=x]", "build"] diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_catalog_only.jsonc b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_catalog_only.jsonc new file mode 100644 index 000000000..fa94e8eef --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_catalog_only.jsonc @@ -0,0 +1,5 @@ +// run --filter [HEAD~1] build +{ + "#build": [], + "packages/app#build": [] +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_catalog_only_in_packages_dir.jsonc b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_catalog_only_in_packages_dir.jsonc new file mode 100644 index 000000000..dad20c481 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_catalog_only_in_packages_dir.jsonc @@ -0,0 +1,4 @@ +// run --filter {packages/*}[HEAD~1] build +{ + "packages/app#build": [] +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_directory_dependents_only.jsonc b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_directory_dependents_only.jsonc new file mode 100644 index 000000000..1e95db858 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_directory_dependents_only.jsonc @@ -0,0 +1,7 @@ +// run --filter ...^{packages/utils}[HEAD~2] build +{ + "packages/app#build": [ + "packages/lib#build" + ], + "packages/lib#build": [] +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_exclusion_only.jsonc b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_exclusion_only.jsonc new file mode 100644 index 000000000..d720c06d1 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_exclusion_only.jsonc @@ -0,0 +1,5 @@ +// run --filter ![HEAD~2] build +{ + "packages/docs#build": [], + "packages/lib#build": [] +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_head_selects_nothing.jsonc b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_head_selects_nothing.jsonc new file mode 100644 index 000000000..ea176031d --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_head_selects_nothing.jsonc @@ -0,0 +1,2 @@ +// run --filter [HEAD] build +{} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_name_not_changed.jsonc b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_name_not_changed.jsonc new file mode 100644 index 000000000..9d69cd5f0 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_name_not_changed.jsonc @@ -0,0 +1,2 @@ +// run --filter docs[HEAD~2] build +{} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_name_with_dependencies.jsonc b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_name_with_dependencies.jsonc new file mode 100644 index 000000000..3429c5975 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_name_with_dependencies.jsonc @@ -0,0 +1,10 @@ +// run --filter app[HEAD~2]... build +{ + "packages/app#build": [ + "packages/lib#build" + ], + "packages/lib#build": [ + "packages/utils#build" + ], + "packages/utils#build": [] +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_option_like_revision.snap b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_option_like_revision.snap new file mode 100644 index 000000000..09e8c0e0e --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_option_like_revision.snap @@ -0,0 +1 @@ +Failed to find the packages changed since '--output=x': '--output=x' is not a git revision \ No newline at end of file diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_unknown_revision.snap b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_unknown_revision.snap new file mode 100644 index 000000000..914572951 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_unknown_revision.snap @@ -0,0 +1 @@ +Failed to find the packages changed since 'no-such-branch': Unknown git revision 'no-such-branch' \ No newline at end of file diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_with_dependents.jsonc b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_with_dependents.jsonc new file mode 100644 index 000000000..9c45f34f3 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_with_dependents.jsonc @@ -0,0 +1,11 @@ +// run --filter ...[HEAD~2] build +{ + "#build": [], + "packages/app#build": [ + "packages/lib#build" + ], + "packages/lib#build": [ + "packages/utils#build" + ], + "packages/utils#build": [] +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_with_exclusion.jsonc b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_with_exclusion.jsonc new file mode 100644 index 000000000..f0c3a236e --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/query_changed_since_with_exclusion.jsonc @@ -0,0 +1,7 @@ +// run --filter ...[HEAD~2] --filter !...[HEAD~1] build +{ + "packages/lib#build": [ + "packages/utils#build" + ], + "packages/utils#build": [] +} diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/task_graph.md b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/task_graph.md new file mode 100644 index 000000000..c4843af11 --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_changed_since/snapshots/task_graph.md @@ -0,0 +1,211 @@ +# task graph + +```mermaid +flowchart TD + task_0["/#build"] + task_1["/packages/app#build"] + task_2["/packages/docs#build"] + task_3["/packages/lib#build"] + task_4["/packages/utils#build"] +``` + +## `/#build` + +```json +{ + "task_display": { + "package_name": "monorepo", + "task_name": "build", + "package_path": "/" + }, + "resolved_config": { + "commands": [ + "echo building monorepo" + ], + "resolved_options": { + "cwd": "/", + "cache_config": { + "remote_cache_allowed": true, + "env_config": { + "fingerprinted_envs": [], + "untracked_env": [ + "" + ] + }, + "input_config": { + "includes_auto": true, + "positive_globs": [], + "negative_globs": [] + }, + "output_config": { + "includes_auto": true, + "positive_globs": [], + "negative_globs": [] + } + } + } + }, + "source": "PackageJsonScript" +} +``` + +## `/packages/app#build` + +```json +{ + "task_display": { + "package_name": "app", + "task_name": "build", + "package_path": "/packages/app" + }, + "resolved_config": { + "commands": [ + "echo building app" + ], + "resolved_options": { + "cwd": "/packages/app", + "cache_config": { + "remote_cache_allowed": true, + "env_config": { + "fingerprinted_envs": [], + "untracked_env": [ + "" + ] + }, + "input_config": { + "includes_auto": true, + "positive_globs": [], + "negative_globs": [] + }, + "output_config": { + "includes_auto": true, + "positive_globs": [], + "negative_globs": [] + } + } + } + }, + "source": "PackageJsonScript" +} +``` + +## `/packages/docs#build` + +```json +{ + "task_display": { + "package_name": "docs", + "task_name": "build", + "package_path": "/packages/docs" + }, + "resolved_config": { + "commands": [ + "echo building docs" + ], + "resolved_options": { + "cwd": "/packages/docs", + "cache_config": { + "remote_cache_allowed": true, + "env_config": { + "fingerprinted_envs": [], + "untracked_env": [ + "" + ] + }, + "input_config": { + "includes_auto": true, + "positive_globs": [], + "negative_globs": [] + }, + "output_config": { + "includes_auto": true, + "positive_globs": [], + "negative_globs": [] + } + } + } + }, + "source": "PackageJsonScript" +} +``` + +## `/packages/lib#build` + +```json +{ + "task_display": { + "package_name": "lib", + "task_name": "build", + "package_path": "/packages/lib" + }, + "resolved_config": { + "commands": [ + "echo building lib" + ], + "resolved_options": { + "cwd": "/packages/lib", + "cache_config": { + "remote_cache_allowed": true, + "env_config": { + "fingerprinted_envs": [], + "untracked_env": [ + "" + ] + }, + "input_config": { + "includes_auto": true, + "positive_globs": [], + "negative_globs": [] + }, + "output_config": { + "includes_auto": true, + "positive_globs": [], + "negative_globs": [] + } + } + } + }, + "source": "PackageJsonScript" +} +``` + +## `/packages/utils#build` + +```json +{ + "task_display": { + "package_name": "utils", + "task_name": "build", + "package_path": "/packages/utils" + }, + "resolved_config": { + "commands": [ + "echo building utils" + ], + "resolved_options": { + "cwd": "/packages/utils", + "cache_config": { + "remote_cache_allowed": true, + "env_config": { + "fingerprinted_envs": [], + "untracked_env": [ + "" + ] + }, + "input_config": { + "includes_auto": true, + "positive_globs": [], + "negative_globs": [] + }, + "output_config": { + "includes_auto": true, + "positive_globs": [], + "negative_globs": [] + } + } + } + }, + "source": "PackageJsonScript" +} +``` + diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_workspace/snapshots.toml b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_workspace/snapshots.toml index fa58d1093..f0339e29a 100644 --- a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_workspace/snapshots.toml +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_workspace/snapshots.toml @@ -236,3 +236,8 @@ args = ["run", "-w", "-r", "check"] compact = true name = "workspace_root_with_transitive" args = ["run", "-w", "-t", "check"] + +# The fixture isn't in a git repository, so `[]` is an error, not an empty match +[[plan]] +name = "filter_changed_since_outside_git_repository" +args = ["run", "--filter", "...[main]", "build"] diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/filter_workspace/snapshots/query_filter_changed_since_outside_git_repository.snap b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_workspace/snapshots/query_filter_changed_since_outside_git_repository.snap new file mode 100644 index 000000000..42a4b466f --- /dev/null +++ b/crates/vt_plan/tests/plan_snapshots/fixtures/filter_workspace/snapshots/query_filter_changed_since_outside_git_repository.snap @@ -0,0 +1 @@ +Failed to find the packages changed since 'main': / is not in a git repository: fatal: not a git repository (or any of the parent directories): .git \ No newline at end of file diff --git a/crates/vt_plan/tests/plan_snapshots/main.rs b/crates/vt_plan/tests/plan_snapshots/main.rs index a9e3f8d25..607f795da 100644 --- a/crates/vt_plan/tests/plan_snapshots/main.rs +++ b/crates/vt_plan/tests/plan_snapshots/main.rs @@ -57,10 +57,57 @@ struct SnapshotsFile { /// filter used by the e2e harness. #[serde(default)] pub platform: Option, + /// Commits made after the staged fixture is committed into a new git + /// repository. Without it, the staged fixture is not a git repository. + #[serde(default)] + pub git: Option, #[serde(rename = "plan", default)] // toml usually uses singular for arrays pub plan_cases: Vec, } +/// A git history for the staged fixture: an initial commit of the fixture, +/// then one commit per entry, each writing its `files` (path → content). +#[derive(serde::Deserialize, Debug)] +#[serde(deny_unknown_fields)] +struct GitHistory { + #[serde(default)] + pub commits: Vec, +} + +#[derive(serde::Deserialize, Debug)] +#[serde(deny_unknown_fields)] +struct GitCommit { + pub files: BTreeMap, +} + +impl GitHistory { + #[expect(clippy::disallowed_types, reason = "Path required for std::process::Command")] + fn create(&self, repo: &std::path::Path) { + let git = |args: &[&str]| { + let output = std::process::Command::new("git") + .args(["-c", "user.name=test", "-c", "user.email=test@example.com"]) + .args(["-c", "commit.gpgsign=false", "-c", "core.autocrlf=false"]) + .args(args) + .current_dir(repo) + .output() + .unwrap(); + assert!(output.status.success(), "git {args:?} failed: {output:?}"); + }; + git(&["-c", "init.defaultBranch=main", "init", "-q"]); + git(&["add", "-A"]); + git(&["commit", "-q", "-m", "fixture"]); + for (index, commit) in self.commits.iter().enumerate() { + for (path, content) in &commit.files { + let path = repo.join(path.as_str()); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, content.as_str()).unwrap(); + } + git(&["add", "-A"]); + git(&["commit", "-q", "-m", vt_str::format!("commit {}", index + 1).as_str()]); + } + } +} + /// Returns whether the current build should run the fixture. Panics on an /// unknown platform string so typos surface loudly. fn should_run_on_this_platform(platform: Option<&Str>) -> bool { @@ -193,6 +240,11 @@ fn run_case_inner( // Copy the case directory to a temporary directory to avoid discovering workspace outside of the test case. let stage_path = tmpdir.join(fixture_name); copy_dir(fixture_path, &stage_path).unwrap(); + if let Some(git) = &cases_file.git { + // `snapshots/` and `snapshots.toml` are copied too; they are committed + // with the fixture, so they never show up as changes. + git.create(stage_path.as_path()); + } let (workspace_root, _cwd) = find_workspace_root(&stage_path).unwrap(); diff --git a/crates/vt_workspace/src/changed_since.rs b/crates/vt_workspace/src/changed_since.rs new file mode 100644 index 000000000..c098dc3c3 --- /dev/null +++ b/crates/vt_workspace/src/changed_since.rs @@ -0,0 +1,416 @@ +//! Changed files for the `[]` filter selector. +//! +//! Follows pnpm's `get_changed_projects` +//! (): +//! `git` is run as a subprocess, and the working tree is compared with the +//! merge base of `` and `HEAD`. Changes to tracked files count whether +//! they are committed or not; untracked files don't. +//! +//! When `pnpm-workspace.yaml` changed, its catalogs at the merge base are +//! compared with the ones on disk, so that the packages that take a changed +//! entry through `catalog:` count as changed too. + +use std::{fs, io, process::Command}; + +use rustc_hash::{FxHashMap, FxHashSet}; +use serde::Deserialize; +use vt_path::AbsolutePath; +use vt_str::Str; + +use crate::{PackageJson, strip_bom}; + +/// The file name of the pnpm workspace manifest, which holds the catalogs. +pub const PNPM_WORKSPACE_YAML: &str = "pnpm-workspace.yaml"; + +/// Errors from finding what changed since a git revision. +#[derive(Debug, thiserror::Error)] +pub enum ChangedSinceError { + #[error("Failed to run `git`")] + Spawn(#[source] io::Error), + + #[error("{} is not in a git repository: {stderr}", .workspace_root.as_path().display())] + NotARepository { workspace_root: Box, stderr: Str }, + + #[error("'{since}' is not a git revision")] + InvalidRevision { since: Str }, + + #[error("Unknown git revision '{since}'")] + UnknownRevision { since: Str }, + + #[error( + "'{since}' and HEAD have no common ancestor{}", + if *.shallow { + "; the repository is a shallow clone, so fetch more history, e.g. `git fetch --unshallow`" + } else { + "" + } + )] + NoMergeBase { since: Str, shallow: bool }, + + #[error("`git {args}` failed: {stderr}")] + Git { args: Str, stderr: Str }, + + #[error("Failed to read {PNPM_WORKSPACE_YAML}")] + ReadWorkspaceYaml(#[source] io::Error), + + #[error("Failed to parse {PNPM_WORKSPACE_YAML} at {revision}")] + ParseWorkspaceYaml { + revision: Str, + #[source] + error: serde_norway::Error, + }, +} + +/// What changed in the workspace since a git revision. +#[derive(Debug)] +pub struct WorkspaceChanges { + /// Changed files, relative to the workspace root, with `/` separators. + pub files: Vec, + + /// Changed catalog entries: dependency names by catalog name. + pub catalogs: ChangedCatalogs, +} + +/// Changed catalog entries: dependency names by catalog name (`default` for `catalog:`). +pub type ChangedCatalogs = FxHashMap>; + +/// Find what changed in the workspace at `workspace_root` since `since`. +/// +/// # Errors +/// +/// Fails, instead of reporting no changes, when `git` can't run, the +/// workspace is not in a git repository, `since` is unknown, or `since` and +/// `HEAD` have no merge base (as in a shallow clone). +pub fn workspace_changes( + workspace_root: &AbsolutePath, + since: &str, +) -> Result { + let git = Git { cwd: workspace_root }; + + let output = git.run(&["rev-parse", "--git-dir"])?; + if !output.status.success() { + return Err(ChangedSinceError::NotARepository { + workspace_root: workspace_root.into(), + stderr: stderr_text(&output), + }); + } + + // Revisions never start with `-`; rejecting them keeps `since` from being + // read as an option, without `--end-of-options` (git 2.24+). + if since.starts_with('-') { + return Err(ChangedSinceError::InvalidRevision { since: since.into() }); + } + let commit = vt_str::format!("{since}^{{commit}}"); + let output = git.run(&["rev-parse", "--verify", "--quiet", &commit])?; + if !output.status.success() { + return Err(ChangedSinceError::UnknownRevision { since: since.into() }); + } + let since_commit = stdout_line(&output); + + let output = git.run(&["merge-base", &since_commit, "HEAD"])?; + let base = match output.status.code() { + Some(0) => stdout_line(&output), + // `git merge-base` exits with 1, printing nothing, when there is no merge base. + Some(1) if output.stderr.is_empty() => { + let shallow = + stdout_line(&git.run(&["rev-parse", "--is-shallow-repository"])?) == "true"; + return Err(ChangedSinceError::NoMergeBase { since: since.into(), shallow }); + } + _ => return Err(git_error(&["merge-base", &since_commit, "HEAD"], &output)), + }; + + // `--relative` lists paths from the workspace root, leaving out files outside it. + // `--no-renames` lists both sides of a move, so both owners count as changed. + let diff_args = ["diff", "--name-only", "-z", "--no-renames", "--relative", &base, "--"]; + let output = git.run(&diff_args)?; + if !output.status.success() { + return Err(git_error(&diff_args, &output)); + } + let files: Vec = + lossy(&output.stdout).split('\0').filter(|file| !file.is_empty()).map(Str::from).collect(); + + let catalogs = if files.iter().any(|file| file == PNPM_WORKSPACE_YAML) { + changed_catalogs_since(&git, workspace_root, &base)? + } else { + ChangedCatalogs::default() + }; + + Ok(WorkspaceChanges { files, catalogs }) +} + +/// The catalog entries that differ between `pnpm-workspace.yaml` at `base` +/// and on disk. +fn changed_catalogs_since( + git: &Git<'_>, + workspace_root: &AbsolutePath, + base: &str, +) -> Result { + // `:./` names the file relative to the current directory, the workspace root. + let object = vt_str::format!("{base}:./{PNPM_WORKSPACE_YAML}"); + let old_content = if git.run(&["cat-file", "-e", &object])?.status.success() { + let output = git.run(&["show", &object])?; + if !output.status.success() { + return Err(git_error(&["show", &object], &output)); + } + Some(output.stdout) + } else { + // The file didn't exist at the merge base. + None + }; + let new_content = match fs::read(workspace_root.join(PNPM_WORKSPACE_YAML)) { + Ok(content) => Some(content), + Err(error) if error.kind() == io::ErrorKind::NotFound => None, + Err(error) => return Err(ChangedSinceError::ReadWorkspaceYaml(error)), + }; + + let old_catalogs = parse_catalogs(old_content.as_deref(), base)?; + let new_catalogs = parse_catalogs(new_content.as_deref(), "the working tree")?; + Ok(diff_catalogs(&old_catalogs, &new_catalogs)) +} + +/// Catalog entries by catalog name, then dependency name. +type Catalogs = FxHashMap>; + +/// The catalog fields of `pnpm-workspace.yaml`. +/// +/// +#[derive(Debug, Default, Deserialize)] +struct WorkspaceCatalogs { + /// The default catalog, referenced as `catalog:` or `catalog:default`. + #[serde(default)] + catalog: Option>, + + /// Named catalogs, referenced as `catalog:`. + #[serde(default)] + catalogs: Option>>>, +} + +const DEFAULT_CATALOG: &str = "default"; + +fn parse_catalogs(content: Option<&[u8]>, revision: &str) -> Result { + let Some(content) = content else { + return Ok(Catalogs::default()); + }; + let manifest: Option = + serde_norway::from_slice(strip_bom(content)).map_err(|error| { + ChangedSinceError::ParseWorkspaceYaml { revision: revision.into(), error } + })?; + let WorkspaceCatalogs { catalog, catalogs } = manifest.unwrap_or_default(); + + let mut result: Catalogs = catalogs + .unwrap_or_default() + .into_iter() + .map(|(name, entries)| (name, entries.unwrap_or_default())) + .collect(); + // pnpm rejects a manifest that sets both `catalog` and `catalogs.default`; + // here `catalog` wins. + if let Some(catalog) = catalog { + result.entry(DEFAULT_CATALOG.into()).or_default().extend(catalog); + } + Ok(result) +} + +/// The entries added, removed or changed between `old` and `new`. +fn diff_catalogs(old: &Catalogs, new: &Catalogs) -> ChangedCatalogs { + let mut changed = ChangedCatalogs::default(); + let names: FxHashSet<&Str> = old.keys().chain(new.keys()).collect(); + for name in names { + let old_entries = old.get(name); + let new_entries = new.get(name); + let dependencies: FxHashSet<&Str> = + old_entries.into_iter().chain(new_entries).flat_map(FxHashMap::keys).collect(); + for dependency in dependencies { + if old_entries.and_then(|entries| entries.get(dependency)) + != new_entries.and_then(|entries| entries.get(dependency)) + { + changed.entry(name.clone()).or_default().insert(dependency.clone()); + } + } + } + changed +} + +/// Whether `package_json` takes any of the `changed` catalog entries. +pub fn uses_changed_catalog(package_json: &PackageJson, changed: &ChangedCatalogs) -> bool { + [ + &package_json.dependencies, + &package_json.dev_dependencies, + &package_json.peer_dependencies, + &package_json.optional_dependencies, + ] + .into_iter() + .flatten() + .any(|(name, specifier)| { + catalog_reference(name, specifier).is_some_and(|(catalog, dependency)| { + changed.get(catalog).is_some_and(|entries| entries.contains(dependency)) + }) + }) +} + +/// The catalog name and catalog entry a dependency specifier refers to. +/// +/// `catalog:` and `catalog:default` refer to the default catalog. An alias +/// such as `npm:lodash@catalog:` refers to the aliased package's entry, as in +/// pnpm's `parse_catalog_dep`. +fn catalog_reference<'a>(name: &'a str, specifier: &'a str) -> Option<(&'a str, &'a str)> { + if let Some(catalog) = parse_catalog_protocol(specifier) { + return Some((catalog, name)); + } + let aliased = specifier.strip_prefix("npm:")?; + let (aliased_name, aliased_specifier) = aliased.rsplit_once('@')?; + if aliased_name.is_empty() { + return None; + } + Some((parse_catalog_protocol(aliased_specifier)?, aliased_name)) +} + +fn parse_catalog_protocol(specifier: &str) -> Option<&str> { + let catalog = specifier.strip_prefix("catalog:")?.trim(); + Some(if catalog.is_empty() { DEFAULT_CATALOG } else { catalog }) +} + +/// Runs `git` in the workspace root. +struct Git<'a> { + cwd: &'a AbsolutePath, +} + +impl Git<'_> { + fn run(&self, args: &[&str]) -> Result { + Command::new("git") + // Keep a user's `diff.relative` setting from changing which paths are listed. + .args(["-c", "diff.relative=false"]) + .args(args) + .current_dir(self.cwd.as_path()) + .output() + .map_err(ChangedSinceError::Spawn) + } +} + +/// `bytes` as UTF-8, with invalid sequences replaced by `U+FFFD`. +fn lossy(bytes: &[u8]) -> Str { + let mut text = Str::with_capacity(bytes.len()); + for chunk in bytes.utf8_chunks() { + text.push_str(chunk.valid()); + if !chunk.invalid().is_empty() { + text.push(char::REPLACEMENT_CHARACTER); + } + } + text +} + +fn stdout_line(output: &std::process::Output) -> Str { + lossy(&output.stdout).trim().into() +} + +fn stderr_text(output: &std::process::Output) -> Str { + lossy(&output.stderr).trim().into() +} + +fn git_error(args: &[&str], output: &std::process::Output) -> ChangedSinceError { + ChangedSinceError::Git { args: args.join(" ").into(), stderr: stderr_text(output) } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn catalogs(yaml: &str) -> Catalogs { + parse_catalogs(Some(yaml.as_bytes()), "test").unwrap() + } + + fn changed(old: &str, new: &str) -> Vec<(Str, Vec)> { + let mut changed: Vec<(Str, Vec)> = diff_catalogs(&catalogs(old), &catalogs(new)) + .into_iter() + .map(|(name, entries)| { + let mut entries: Vec = entries.into_iter().collect(); + entries.sort_unstable(); + (name, entries) + }) + .collect(); + changed.sort_unstable(); + changed + } + + #[test] + fn diff_reports_changed_added_and_removed_entries() { + let old = "catalog:\n react: ^18.0.0\n lodash: ^4.0.0\n zod: ^3.0.0\n"; + let new = "catalog:\n react: ^19.0.0\n zod: ^3.0.0\n vue: ^3.0.0\n"; + assert_eq!( + changed(old, new), + vec![(Str::from("default"), vec!["lodash".into(), "react".into(), "vue".into()])] + ); + } + + #[test] + fn diff_covers_named_catalogs() { + let old = "catalogs:\n legacy:\n react: ^17.0.0\n next:\n react: ^19.0.0\n"; + let new = "catalogs:\n legacy:\n react: ^17.0.2\n next:\n react: ^19.0.0\n"; + assert_eq!(changed(old, new), vec![(Str::from("legacy"), vec!["react".into()])]); + } + + #[test] + fn catalog_and_catalogs_default_are_the_same_catalog() { + let old = "catalog:\n react: ^18.0.0\n"; + let new = "catalogs:\n default:\n react: ^18.0.0\n"; + assert!(changed(old, new).is_empty()); + } + + #[test] + fn unrelated_manifest_changes_change_no_catalog() { + let old = "packages:\n - packages/*\ncatalog:\n react: ^18.0.0\n"; + let new = "packages:\n - packages/*\n - apps/*\ncatalog:\n react: ^18.0.0\n"; + assert!(changed(old, new).is_empty()); + } + + #[test] + fn missing_or_empty_manifest_has_no_catalogs() { + assert!(parse_catalogs(None, "test").unwrap().is_empty()); + assert!(catalogs("").is_empty()); + assert!(catalogs("catalog:\ncatalogs:\n empty:\n").values().all(FxHashMap::is_empty)); + } + + #[test] + fn invalid_manifest_is_an_error() { + let error = parse_catalogs(Some(b"catalog: [unclosed"), "abc123").unwrap_err(); + assert!( + matches!(&error, ChangedSinceError::ParseWorkspaceYaml { revision, .. } if revision == "abc123"), + "{error:?}" + ); + } + + #[test] + fn catalog_references() { + assert_eq!(catalog_reference("react", "catalog:"), Some(("default", "react"))); + assert_eq!(catalog_reference("react", "catalog:default"), Some(("default", "react"))); + assert_eq!(catalog_reference("react", "catalog:legacy"), Some(("legacy", "react"))); + assert_eq!(catalog_reference("ui", "npm:@acme/ui@catalog:"), Some(("default", "@acme/ui"))); + assert_eq!(catalog_reference("react", "^18.0.0"), None); + assert_eq!(catalog_reference("lib", "workspace:*"), None); + assert_eq!(catalog_reference("lodash", "npm:lodash@^4.0.0"), None); + } + + #[test] + fn package_uses_changed_catalog() { + let package_json: PackageJson = serde_json::from_str( + r#"{ + "name": "app", + "dependencies": { "react": "catalog:" }, + "devDependencies": { "typescript": "catalog:tools" }, + "optionalDependencies": { "fsevents": "catalog:" } + }"#, + ) + .unwrap(); + let changed_entries = |catalog: &str, dependency: &str| -> ChangedCatalogs { + std::iter::once((Str::from(catalog), std::iter::once(Str::from(dependency)).collect())) + .collect() + }; + + assert!(uses_changed_catalog(&package_json, &changed_entries("default", "react"))); + assert!(uses_changed_catalog(&package_json, &changed_entries("tools", "typescript"))); + assert!(uses_changed_catalog(&package_json, &changed_entries("default", "fsevents"))); + // Same dependency, other catalog. + assert!(!uses_changed_catalog(&package_json, &changed_entries("default", "typescript"))); + assert!(!uses_changed_catalog(&package_json, &changed_entries("default", "vue"))); + assert!(!uses_changed_catalog(&package_json, &ChangedCatalogs::default())); + } +} diff --git a/crates/vt_workspace/src/lib.rs b/crates/vt_workspace/src/lib.rs index 349f28e1e..6441d9fd9 100644 --- a/crates/vt_workspace/src/lib.rs +++ b/crates/vt_workspace/src/lib.rs @@ -1,3 +1,4 @@ +mod changed_since; mod error; pub mod package; pub mod package_filter; @@ -19,6 +20,7 @@ use wax::{ }; pub use crate::{ + changed_since::ChangedSinceError, error::Error, package::{DependencyType, PackageJson}, package_manager::{ diff --git a/crates/vt_workspace/src/package.rs b/crates/vt_workspace/src/package.rs index 2a6d9fcc0..91c1a03c0 100644 --- a/crates/vt_workspace/src/package.rs +++ b/crates/vt_workspace/src/package.rs @@ -23,6 +23,10 @@ pub struct PackageJson { pub dev_dependencies: FxHashMap, #[serde(default)] pub peer_dependencies: FxHashMap, + /// Read only to find the packages that use a changed catalog entry + /// (`--filter "[]"`); not part of the package graph. + #[serde(default)] + pub optional_dependencies: FxHashMap, } impl std::fmt::Debug for PackageJson { @@ -30,12 +34,13 @@ impl std::fmt::Debug for PackageJson { if std::env::var("VITE_DEBUG_VERBOSE").is_ok_and(|v| v != "0" && v != "false") { write!( f, - "PackageJson {{ name: {:?}, scripts: {:?}, dependencies: {:?}, dev_dependencies: {:?}, peer_dependencies: {:?} }}", + "PackageJson {{ name: {:?}, scripts: {:?}, dependencies: {:?}, dev_dependencies: {:?}, peer_dependencies: {:?}, optional_dependencies: {:?} }}", self.name, self.scripts, self.dependencies, self.dev_dependencies, - self.peer_dependencies + self.peer_dependencies, + self.optional_dependencies ) } else { write!(f, "PackageJson {{ name: {:?}, scripts: {:?} }}", self.name, self.scripts) diff --git a/crates/vt_workspace/src/package_filter.rs b/crates/vt_workspace/src/package_filter.rs index b2858f89e..d8595213f 100644 --- a/crates/vt_workspace/src/package_filter.rs +++ b/crates/vt_workspace/src/package_filter.rs @@ -21,6 +21,14 @@ //! - `...^foo` → foo's dependents only (exclude foo itself) //! - `...foo...` → foo + dependencies + dependents //! - `!foo` → exclude foo from results +//! - `[]` → packages with files changed since a git revision +//! - `foo[]` → same, narrowed to packages matching `foo` +//! (`{./dir}[]` and `foo{./dir}[]` narrow by directory) +//! - `...[]` → changed packages + their dependents (all traversal +//! markers above apply) +//! +//! As in pnpm, a trailing `[...]` group is the `` revision, so +//! `foo[ab]` is not a glob character class; one elsewhere, as in `foo[ab]*`, is. //! //! The `ContainingPackage` selector variant is NOT produced by `parse_filter`. //! It is synthesized internally for `vp run build` (implicit cwd) and `vp run -t build` @@ -142,6 +150,16 @@ pub(crate) enum PackageSelector { /// Select the workspace root package (the package with empty relative path). /// Produced by `-w` / `--workspace-root`. WorkspaceRoot, + + /// Packages with files changed since a git revision, optionally narrowed + /// to those that also match `name` and `directory`. + /// Produced by `--filter "[]"`, `"foo[]"`, `"{./dir}[]"` + /// and `"foo{./dir}[]"`. + ChangedSince { + since: Str, + name: Option, + directory: Option, + }, } /// Direction to traverse the package dependency graph from the initially matched packages. @@ -275,6 +293,8 @@ Match packages by name, directory, or glob pattern. --filter ... Select package and its dependencies --filter ... Select package and its dependents --filter ^... Select only the dependencies (exclude the package itself) + --filter [] Select packages changed since a git revision (e.g. [origin/main]) + --filter ...[] Select changed packages and their dependents --filter ! Exclude packages matching the pattern" )] filters: Vec, @@ -513,11 +533,13 @@ pub(crate) fn parse_filter( /// Parse the core selector string (after stripping `!` and `...` markers). /// -/// Implements pnpm's [`SELECTOR_REGEX`] logic: `^([^.][^{}[\]]*)?(\{[^}]+\})?$` +/// Implements pnpm's [`SELECTOR_REGEX`] logic: `^([^.][^{}[\]]*)?(\{[^}]+\})?(\[[^\]]+\])?$` /// /// [`SELECTOR_REGEX`]: https://github.com/pnpm/pnpm/blob/05dd45ea82fff9c0b687cdc8f478a1027077d343/workspace/filter-workspace-packages/src/parsePackageSelector.ts#L37 /// /// Decision tree: +/// 0. If the string ends with a `[]` group (see [`split_since`]), parse +/// the rest as an optional name and brace-directory narrowing the changed packages. /// 1. If the string ends with `}` and contains a `{`, split name and brace-directory. /// The name part must not start with `.` for the brace split to be valid /// (per the regex rule that Group 1 must not start with `.`). @@ -530,6 +552,12 @@ fn parse_core_selector( core: &str, cwd: &AbsolutePath, ) -> Result<(PackageSelector, bool), PackageFilterParseError> { + if let Some((name_part, dir_inner, since)) = split_since(core) { + let name = if name_part.is_empty() { None } else { Some(build_name_pattern(name_part)?) }; + let directory = dir_inner.map(|dir| resolve_directory_pattern(dir, cwd)).transpose()?; + return Ok((PackageSelector::ChangedSince { since: since.into(), name, directory }, true)); + } + // Try to extract a brace-enclosed directory suffix: `{...}`. // The name part before the brace must not start with `.` (pnpm regex Group 1 constraint). if let Some(without_closing) = core.strip_suffix('}') @@ -572,6 +600,47 @@ fn parse_core_selector( Ok((PackageSelector::Name(build_name_pattern(core)?), true)) } +/// Split a selector that ends with a `[]` group into its name, its +/// brace-directory contents and the revision. +/// +/// Mirrors the `^([^.][^{}[\]]*)?(\{[^}]+\})?(\[[^\]]+\])?$` regex of pnpm's +/// [`parse_project_selector`]: the revision is non-empty and has no `]`, the +/// brace group (if any) has no `}`, and the name doesn't start with `.` and has +/// no `{`, `}`, `[` or `]` after its first character. Returns `None` when the +/// selector doesn't have that shape, so it's parsed as before (e.g. `[ab]*` +/// stays a glob). +/// +/// [`parse_project_selector`]: https://github.com/pnpm/pnpm/blob/main/pnpm/crates/workspace-projects-filter/src/parse_project_selector.rs +fn split_since(core: &str) -> Option<(&str, Option<&str>, &str)> { + let without_closing = core.strip_suffix(']')?; + let open = without_closing.rfind('[')?; + let since = &without_closing[open + 1..]; + if since.is_empty() || since.contains(']') { + return None; + } + let before = &without_closing[..open]; + + let (name, dir_inner) = + match before.strip_suffix('}').and_then(|b| b.rfind('{').map(|i| (b, i))) { + Some((without_brace, brace)) => { + let dir_inner = &without_brace[brace + 1..]; + if dir_inner.is_empty() || dir_inner.contains('}') { + return None; + } + (&without_brace[..brace], Some(dir_inner)) + } + None => (before, None), + }; + + let mut name_chars = name.chars(); + if let Some(first) = name_chars.next() + && (first == '.' || name_chars.any(|c| matches!(c, '{' | '}' | '[' | ']'))) + { + return None; + } + Some((name, dir_inner, since)) +} + /// Resolve a directory selector string into a [`DirectoryPattern`]. /// /// Uses [`wax::Glob::partition`] to split into an invariant base path and an @@ -1121,6 +1190,190 @@ mod tests { assert_traversal(&f, TraversalDirection::Dependencies, false); } + // ── [] changed-since selectors ────────────────────────────────── + + /// The `ChangedSince` parts of `filter`: revision, name, directory. + fn changed_since( + filter: &PackageFilter, + ) -> (&str, Option<&PackageNamePattern>, Option<&DirectoryPattern>) { + match &filter.selector { + PackageSelector::ChangedSince { since, name, directory } => { + (since.as_str(), name.as_ref(), directory.as_ref()) + } + other => panic!("expected ChangedSince, got {other:?}"), + } + } + + fn exact(name: &str) -> PackageNamePattern { + PackageNamePattern::Exact { name: name.into(), exactly_one: false } + } + + #[test] + fn since_alone() { + let f = parse_filter("[origin/main]", abs("/workspace")).unwrap(); + assert!(!f.exclude); + assert_eq!(changed_since(&f), ("origin/main", None, None)); + assert_no_traversal(&f); + } + + #[test] + fn since_with_dependents() { + let f = parse_filter("...[HEAD~1]", abs("/workspace")).unwrap(); + assert_eq!(changed_since(&f), ("HEAD~1", None, None)); + assert_traversal(&f, TraversalDirection::Dependents, false); + } + + #[test] + fn since_with_dependencies() { + let f = parse_filter("[HEAD~1]...", abs("/workspace")).unwrap(); + assert_eq!(changed_since(&f), ("HEAD~1", None, None)); + assert_traversal(&f, TraversalDirection::Dependencies, false); + } + + #[test] + fn since_with_both_directions() { + let f = parse_filter("...[HEAD~1]...", abs("/workspace")).unwrap(); + assert_eq!(changed_since(&f), ("HEAD~1", None, None)); + assert_traversal(&f, TraversalDirection::Both, false); + } + + #[test] + fn since_with_dependents_exclude_self() { + let f = parse_filter("...^[main]", abs("/workspace")).unwrap(); + assert_eq!(changed_since(&f), ("main", None, None)); + assert_traversal(&f, TraversalDirection::Dependents, true); + } + + #[test] + fn since_with_dependencies_exclude_self() { + let f = parse_filter("[main]^...", abs("/workspace")).unwrap(); + assert_eq!(changed_since(&f), ("main", None, None)); + assert_traversal(&f, TraversalDirection::Dependencies, true); + } + + #[test] + fn since_excluded() { + let f = parse_filter("!...[main]", abs("/workspace")).unwrap(); + assert!(f.exclude); + assert_eq!(changed_since(&f), ("main", None, None)); + assert_traversal(&f, TraversalDirection::Dependents, false); + } + + #[test] + fn since_with_name() { + let f = parse_filter("@test/app[main]", abs("/workspace")).unwrap(); + assert_eq!(changed_since(&f), ("main", Some(&exact("@test/app")), None)); + } + + #[test] + fn since_with_name_glob() { + let f = parse_filter("...@test/*[main]", abs("/workspace")).unwrap(); + let (since, name, directory) = changed_since(&f); + assert_eq!(since, "main"); + assert!( + matches!(name, Some(PackageNamePattern::Glob(g)) if g.to_string() == "@test/*"), + "{name:?}" + ); + assert!(directory.is_none()); + assert_traversal(&f, TraversalDirection::Dependents, false); + } + + #[test] + fn since_with_directory() { + let f = parse_filter("{./packages/app}[main]", abs("/workspace")).unwrap(); + assert_eq!( + changed_since(&f), + ("main", None, Some(&DirectoryPattern::Exact(abs("/workspace/packages/app").into()))) + ); + } + + #[test] + fn since_with_directory_glob_and_traversal() { + let f = parse_filter("...{packages/**}[origin/main]...", abs("/workspace")).unwrap(); + let (since, name, directory) = changed_since(&f); + assert_eq!(since, "origin/main"); + assert!(name.is_none()); + assert!( + matches!( + directory, + Some(DirectoryPattern::Glob { base, pattern }) + if base.as_ref() == abs("/workspace/packages") && pattern.to_string() == "**" + ), + "{directory:?}" + ); + assert_traversal(&f, TraversalDirection::Both, false); + } + + #[test] + fn since_with_name_and_directory() { + let f = parse_filter("app{./packages/app}[main]", abs("/workspace")).unwrap(); + assert_eq!( + changed_since(&f), + ( + "main", + Some(&exact("app")), + Some(&DirectoryPattern::Exact(abs("/workspace/packages/app").into())) + ) + ); + } + + #[test] + fn since_keeps_revision_syntax() { + // `^`, `~`, `@{...}` and `..` belong to the revision, not to the selector. + for since in ["HEAD^", "HEAD~2", "main@{1}", "v1.0.0", "a1b2c3d", "origin/feat/x"] { + let f = parse_filter(&vt_str::format!("...[{since}]"), abs("/workspace")).unwrap(); + assert_eq!(changed_since(&f), (since, None, None)); + } + let f = parse_filter("[HEAD^]...", abs("/workspace")).unwrap(); + assert_eq!(changed_since(&f), ("HEAD^", None, None)); + assert_traversal(&f, TraversalDirection::Dependencies, false); + } + + #[test] + fn trailing_bracket_group_is_since_not_glob_class() { + // Behavior change: `foo[ab]` was a glob matching `fooa` and `foob`. + let f = parse_filter("foo[ab]", abs("/workspace")).unwrap(); + assert_eq!(changed_since(&f), ("ab", Some(&exact("foo")), None)); + } + + #[test] + fn bracket_group_not_at_the_end_stays_glob() { + let f = parse_filter("foo[ab]*", abs("/workspace")).unwrap(); + assert_glob_name(&f, "foo[ab]*"); + let f = parse_filter("[ab]*", abs("/workspace")).unwrap(); + assert_glob_name(&f, "[ab]*"); + } + + #[test] + fn bracket_groups_pnpm_regex_rejects_stay_names() { + // Two bracket groups: the name may not contain `]` after its first character. + let f = parse_filter("[x][y]", abs("/workspace")).unwrap(); + assert_glob_name(&f, "[x][y]"); + // A `]` in the revision. + let f = parse_filter("a[b]c]", abs("/workspace")); + assert!(!matches!( + f, + Ok(PackageFilter { selector: PackageSelector::ChangedSince { .. }, .. }) + )); + } + + #[test] + fn empty_since_is_not_a_since_selector() { + // pnpm's `\[[^\]]+\]` needs at least one character. + let f = parse_filter("foo[]", abs("/workspace")); + assert!(!matches!( + f, + Ok(PackageFilter { selector: PackageSelector::ChangedSince { .. }, .. }) + )); + } + + #[test] + fn unbraced_path_with_bracket_stays_a_path() { + // As in pnpm, a `.`-prefixed selector is a location; use `{./dir}[]`. + let f = parse_filter("./foo[main]", abs("/workspace")).unwrap(); + assert!(matches!(&f.selector, PackageSelector::Directory(_)), "{:?}", f.selector); + } + // ── -w / --workspace-root flag ────────────────────────────────────────── #[test] diff --git a/crates/vt_workspace/src/package_graph.rs b/crates/vt_workspace/src/package_graph.rs index e500c1511..fbc6ff5c2 100644 --- a/crates/vt_workspace/src/package_graph.rs +++ b/crates/vt_workspace/src/package_graph.rs @@ -23,6 +23,7 @@ use vt_str::Str; use crate::{ DependencyType, PackageInfo, PackageIx, PackageNodeIndex, + changed_since::{ChangedSinceError, uses_changed_catalog, workspace_changes}, package_filter::{ DirectoryPattern, GraphTraversal, PackageFilter, PackageNamePattern, PackageSelector, TraversalDirection, @@ -124,8 +125,19 @@ pub enum PackageQueryResolveError { #[error("Package '{package_name}' not found in the workspace")] PackageNotFound { package_name: Str }, + + #[error("Failed to find the packages changed since '{since}'")] + ChangedSince { + since: Str, + #[source] + error: ChangedSinceError, + }, } +/// Packages changed since each `[]` revision, so filters that share a +/// revision run `git` once. +type ChangedSinceCache = FxHashMap>; + // ──────────────────────────────────────────────────────────────────────────── // IndexedPackageGraph // ──────────────────────────────────────────────────────────────────────────── @@ -211,6 +223,8 @@ impl IndexedPackageGraph { /// Returns [`PackageQueryResolveError::AmbiguousPackageName`] when an exact /// package name (from a `pkg#task` specifier) matches multiple packages, and /// [`PackageQueryResolveError::PackageNotFound`] when it matches none. + /// Returns [`PackageQueryResolveError::ChangedSince`] when the changes for + /// a `[]` selector can't be read from git. pub fn resolve_query( &self, query: &PackageQuery, @@ -256,6 +270,7 @@ impl IndexedPackageGraph { filters: &[PackageFilter], ) -> Result { let mut unmatched_selectors = Vec::new(); + let mut changed_since_cache = ChangedSinceCache::default(); let (inclusions, exclusions): (Vec<_>, Vec<_>) = filters.iter().partition(|f| !f.exclude); @@ -268,7 +283,8 @@ impl IndexedPackageGraph { // Apply inclusions: union each filter's resolved set into `selected`. for filter in &inclusions { - let matched = self.resolve_selector_entries(&filter.selector)?; + let matched = + self.resolve_selector_entries(&filter.selector, &mut changed_since_cache)?; let expanded = self.expand_traversal(matched, filter.traversal.as_ref()); // Track filters that contribute nothing (typo, no-match glob, or a // traversal like `^...` that collapsed an otherwise-matching seed @@ -284,7 +300,8 @@ impl IndexedPackageGraph { // Apply exclusions: subtract each filter's resolved set from `selected`. for filter in &exclusions { - let matched = self.resolve_selector_entries(&filter.selector)?; + let matched = + self.resolve_selector_entries(&filter.selector, &mut changed_since_cache)?; let to_remove = self.expand_traversal(matched, filter.traversal.as_ref()); for pkg in to_remove { selected.remove(&pkg); @@ -300,6 +317,7 @@ impl IndexedPackageGraph { fn resolve_selector_entries( &self, selector: &PackageSelector, + changed_since_cache: &mut ChangedSinceCache, ) -> Result, PackageQueryResolveError> { let mut matched = FxHashSet::default(); @@ -329,12 +347,26 @@ impl IndexedPackageGraph { } PackageSelector::WorkspaceRoot => { - // The workspace root package has an empty relative path. - for idx in self.graph.node_indices() { - if self.graph[idx].path.as_str().is_empty() { - matched.insert(idx); - break; - } + matched.extend(self.workspace_root_index()); + } + + PackageSelector::ChangedSince { since, name, directory } => { + if !changed_since_cache.contains_key(since) { + let changed = self.changed_since(since).map_err(|error| { + PackageQueryResolveError::ChangedSince { since: since.clone(), error } + })?; + changed_since_cache.insert(since.clone(), changed); + } + matched.clone_from(&changed_since_cache[since]); + if let Some(name) = name { + let mut by_name = FxHashSet::default(); + self.match_by_name_pattern(name, &mut by_name)?; + matched.retain(|idx| by_name.contains(idx)); + } + if let Some(directory) = directory { + let mut by_dir = FxHashSet::default(); + self.match_by_directory_pattern(directory, &mut by_dir); + matched.retain(|idx| by_dir.contains(idx)); } } } @@ -342,6 +374,50 @@ impl IndexedPackageGraph { Ok(matched) } + /// The workspace root package, the one with an empty relative path. + fn workspace_root_index(&self) -> Option { + self.graph.node_indices().find(|&idx| self.graph[idx].path.as_str().is_empty()) + } + + /// The packages with files changed since the git revision `since`. + /// + /// Follows pnpm's `get_changed_projects`: a changed file belongs to the + /// deepest package that contains it, which is the workspace root package + /// for files outside every other package. When `pnpm-workspace.yaml` + /// changed, packages that take a changed catalog entry (`catalog:`, + /// `catalog:`) count as changed too. + fn changed_since(&self, since: &str) -> Result, ChangedSinceError> { + let mut changed = FxHashSet::default(); + // Every workspace has a root package (see `load_package_graph`). + let Some(root) = self.workspace_root_index() else { + return Ok(changed); + }; + let workspace = workspace_changes(&self.graph[root].absolute_path, since)?; + + let indices_by_relative_path: FxHashMap<&str, PackageNodeIndex> = + self.graph.node_indices().map(|idx| (self.graph[idx].path.as_str(), idx)).collect(); + for file in &workspace.files { + let mut dir = file.as_str(); + let owner = loop { + dir = dir.rsplit_once('/').map_or("", |(parent, _)| parent); + if let Some(&idx) = indices_by_relative_path.get(dir) { + break idx; + } + if dir.is_empty() { + break root; + } + }; + changed.insert(owner); + } + + if !workspace.catalogs.is_empty() { + changed.extend(self.graph.node_indices().filter(|&idx| { + uses_changed_catalog(&self.graph[idx].package_json, &workspace.catalogs) + })); + } + Ok(changed) + } + /// Match packages by a name pattern, inserting into `out`. /// /// For `Exact` names, scoped auto-completion applies @@ -552,3 +628,291 @@ impl IndexedPackageGraph { subgraph } } + +#[cfg(test)] +mod tests { + use std::{fs, process::Command}; + + use tempfile::TempDir; + use vt_path::AbsolutePathBuf; + + use super::*; + use crate::{discover_package_graph, package_filter::parse_filter}; + + fn git(cwd: &AbsolutePath, args: &[&str]) -> std::process::Output { + Command::new("git") + .args(["-c", "user.name=test", "-c", "user.email=test@example.com"]) + .args(["-c", "commit.gpgsign=false", "-c", "core.autocrlf=false"]) + .args(args) + .current_dir(cwd.as_path()) + .output() + .unwrap() + } + + fn select( + workspace: &AbsolutePath, + filters: &[&str], + ) -> Result, PackageQueryResolveError> { + let graph = IndexedPackageGraph::index(discover_package_graph(workspace).unwrap()); + let filters = filters + .iter() + .map(|filter| parse_filter(filter, workspace).unwrap()) + .collect::>(); + let query = PackageQuery::filters(Vec1::try_from_vec(filters).unwrap()); + let resolution = graph.resolve_query(&query)?; + let mut names: Vec = resolution + .package_subgraph + .nodes() + .map(|idx| graph.package_graph()[idx].package_json.name.clone()) + .collect(); + names.sort_unstable(); + Ok(names) + } + + fn changed_since_error(workspace: &AbsolutePath, filter: &str) -> ChangedSinceError { + match select(workspace, &[filter]) { + Err(PackageQueryResolveError::ChangedSince { error, .. }) => error, + other => panic!("expected a ChangedSince error, got {other:?}"), + } + } + + /// A git repository holding a pnpm workspace in `workspace_dir` (relative + /// to the repository root): root `monorepo`, and `app` → `lib` → `utils` + /// under `packages/`. `app` takes `react` from the default catalog. + struct Repo { + _temp_dir: TempDir, + root: AbsolutePathBuf, + workspace: AbsolutePathBuf, + } + + impl Repo { + fn new() -> Self { + Self::with_workspace_dir("") + } + + fn with_workspace_dir(workspace_dir: &str) -> Self { + let temp_dir = TempDir::new().unwrap(); + let root = AbsolutePathBuf::new(temp_dir.path().canonicalize().unwrap()).unwrap(); + let workspace = root.join(workspace_dir); + let repo = Self { _temp_dir: temp_dir, root, workspace }; + repo.git(&["-c", "init.defaultBranch=main", "init", "-q"]); + repo.write( + "pnpm-workspace.yaml", + "packages:\n - packages/*\ncatalog:\n react: ^18.0.0\n lodash: ^4.0.0\n", + ); + repo.write("package.json", r#"{ "name": "monorepo" }"#); + for (name, deps) in [ + ("app", r#""lib": "workspace:*", "react": "catalog:""#), + ("lib", r#""utils": "workspace:*""#), + ("utils", ""), + ] { + repo.write( + &vt_str::format!("packages/{name}/package.json"), + &vt_str::format!(r#"{{ "name": "{name}", "dependencies": {{ {deps} }} }}"#), + ); + repo.write(&vt_str::format!("packages/{name}/src/index.js"), "export {}\n"); + } + repo.commit("init"); + repo + } + + fn write(&self, path: &str, content: &str) { + let path = self.workspace.join(path); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, content).unwrap(); + } + + fn git(&self, args: &[&str]) { + let output = git(&self.root, args); + assert!(output.status.success(), "git {args:?} failed: {output:?}"); + } + + fn commit(&self, message: &str) { + self.git(&["add", "-A"]); + self.git(&["commit", "-q", "-m", message]); + } + + fn select(&self, filters: &[&str]) -> Vec { + select(&self.workspace, filters).unwrap() + } + } + + #[test] + fn leaf_change_selects_leaf_and_dependents() { + let repo = Repo::new(); + repo.write("packages/utils/src/index.js", "export const x = 1\n"); + repo.commit("change utils"); + + assert_eq!(repo.select(&["[HEAD~1]"]), ["utils"]); + assert_eq!(repo.select(&["...[HEAD~1]"]), ["app", "lib", "utils"]); + assert_eq!(repo.select(&["...^[HEAD~1]"]), ["app", "lib"]); + assert_eq!(repo.select(&["[HEAD~1]..."]), ["utils"]); + assert!(repo.select(&["[HEAD]"]).is_empty()); + } + + #[test] + fn middle_change_with_dependencies() { + let repo = Repo::new(); + repo.write("packages/lib/src/index.js", "export const x = 1\n"); + repo.commit("change lib"); + + assert_eq!(repo.select(&["[HEAD~1]..."]), ["lib", "utils"]); + assert_eq!(repo.select(&["[HEAD~1]^..."]), ["utils"]); + assert_eq!(repo.select(&["...[HEAD~1]..."]), ["app", "lib", "utils"]); + } + + #[test] + fn files_outside_every_package_belong_to_the_root() { + let repo = Repo::new(); + repo.write("README.md", "hi\n"); + repo.write("packages/README.md", "hi\n"); + repo.commit("docs"); + + assert_eq!(repo.select(&["[HEAD~1]"]), ["monorepo"]); + } + + #[test] + fn uncommitted_tracked_changes_count_and_untracked_files_do_not() { + let repo = Repo::new(); + repo.write("packages/lib/src/index.js", "export const changed = 1\n"); + repo.write("packages/utils/src/untracked.js", "export {}\n"); + + assert_eq!(repo.select(&["[HEAD]"]), ["lib"]); + } + + #[test] + fn moved_file_changes_both_packages() { + let repo = Repo::new(); + repo.git(&["mv", "packages/lib/src/index.js", "packages/utils/src/moved.js"]); + repo.commit("move"); + + assert_eq!(repo.select(&["[HEAD~1]"]), ["lib", "utils"]); + } + + #[test] + fn compares_with_the_merge_base() { + let repo = Repo::new(); + repo.git(&["checkout", "-q", "-b", "feature"]); + repo.write("packages/lib/src/index.js", "export const feature = 1\n"); + repo.commit("feature"); + repo.git(&["checkout", "-q", "main"]); + repo.write("packages/utils/src/index.js", "export const main = 1\n"); + repo.commit("main moved on"); + repo.git(&["checkout", "-q", "feature"]); + + // `utils` changed only on `main`, after `feature` forked from it. + assert_eq!(repo.select(&["[main]"]), ["lib"]); + } + + #[test] + fn name_and_directory_narrow_the_changed_packages() { + let repo = Repo::new(); + repo.write("packages/lib/src/index.js", "export const x = 1\n"); + repo.write("packages/utils/src/index.js", "export const x = 1\n"); + repo.commit("change lib and utils"); + + assert_eq!(repo.select(&["lib[HEAD~1]"]), ["lib"]); + assert!(repo.select(&["app[HEAD~1]"]).is_empty()); + assert_eq!(repo.select(&["{./packages/utils}[HEAD~1]"]), ["utils"]); + assert_eq!(repo.select(&["{packages/*}[HEAD~1]"]), ["lib", "utils"]); + assert_eq!(repo.select(&["...utils{packages/*}[HEAD~1]"]), ["app", "lib", "utils"]); + } + + #[test] + fn exclusion_removes_changed_packages() { + let repo = Repo::new(); + repo.write("packages/utils/src/index.js", "export const x = 1\n"); + repo.commit("change utils"); + + assert_eq!(repo.select(&["!...[HEAD~1]"]), ["monorepo"]); + assert_eq!(repo.select(&["...[HEAD~1]", "!lib"]), ["app", "utils"]); + } + + #[test] + fn catalog_change_selects_the_packages_that_use_it() { + let repo = Repo::new(); + repo.write( + "pnpm-workspace.yaml", + "packages:\n - packages/*\ncatalog:\n react: ^19.0.0\n lodash: ^4.0.0\n", + ); + repo.commit("bump react"); + + // The root owns `pnpm-workspace.yaml`; `app` takes `react: catalog:`. + assert_eq!(repo.select(&["[HEAD~1]"]), ["app", "monorepo"]); + assert_eq!(repo.select(&["...{packages/*}[HEAD~1]"]), ["app"]); + } + + #[test] + fn catalog_change_to_an_unused_entry_selects_only_the_root() { + let repo = Repo::new(); + repo.write( + "pnpm-workspace.yaml", + "packages:\n - packages/*\ncatalog:\n react: ^18.0.0\n lodash: ^4.17.0\n", + ); + repo.commit("bump lodash"); + + assert_eq!(repo.select(&["[HEAD~1]"]), ["monorepo"]); + } + + #[test] + fn workspace_in_a_repository_subdirectory() { + let repo = Repo::with_workspace_dir("js"); + fs::write(repo.root.join("outside.txt"), "outside the workspace\n").unwrap(); + repo.write("packages/utils/src/index.js", "export const x = 1\n"); + repo.write( + "pnpm-workspace.yaml", + "packages:\n - packages/*\ncatalog:\n react: ^19.0.0\n lodash: ^4.0.0\n", + ); + repo.commit("change"); + + assert_eq!(repo.select(&["[HEAD~1]"]), ["app", "monorepo", "utils"]); + + fs::write(repo.root.join("outside.txt"), "changed again\n").unwrap(); + repo.commit("change outside the workspace"); + assert!(repo.select(&["[HEAD~1]"]).is_empty()); + } + + #[test] + fn unknown_revision_is_an_error() { + let repo = Repo::new(); + let error = changed_since_error(&repo.workspace, "...[no-such-branch]"); + assert!( + matches!(&error, ChangedSinceError::UnknownRevision { since } if since == "no-such-branch"), + "{error:?}" + ); + } + + #[test] + fn option_like_revision_is_an_error() { + let repo = Repo::new(); + let error = changed_since_error(&repo.workspace, "[--output=x]"); + assert!(matches!(&error, ChangedSinceError::InvalidRevision { .. }), "{error:?}"); + } + + #[test] + fn unrelated_history_is_an_error() { + let repo = Repo::new(); + repo.git(&["checkout", "-q", "--orphan", "unrelated"]); + repo.commit("unrelated root"); + + let error = changed_since_error(&repo.workspace, "[main]"); + assert!( + matches!(&error, ChangedSinceError::NoMergeBase { since, shallow: false } if since == "main"), + "{error:?}" + ); + } + + #[test] + fn not_a_repository_is_an_error() { + let temp_dir = TempDir::new().unwrap(); + let workspace = AbsolutePathBuf::new(temp_dir.path().canonicalize().unwrap()).unwrap(); + assert!( + !git(&workspace, &["rev-parse", "--git-dir"]).status.success(), + "the system temp dir must not be inside a git repository" + ); + fs::write(workspace.join("package.json"), r#"{ "name": "solo" }"#).unwrap(); + + let error = changed_since_error(&workspace, "[HEAD~1]"); + assert!(matches!(&error, ChangedSinceError::NotARepository { .. }), "{error:?}"); + } +}