Skip to content

fix(workspaces): read pnpm-workspace.yaml with a YAML parser, and refuse patterns that match nothing - #17

Merged
lucasbraum merged 4 commits into
mainfrom
fix/pnpm-workspace-yaml
Oct 6, 2026
Merged

lucasbraum merged 4 commits into
mainfrom
fix/pnpm-workspace-yaml

Conversation

@lucasbraum

@lucasbraum lucasbraum commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

workspacePackagesOf read pnpm-workspace.yaml line by line and only understood the block list. The flow form, packages: ['packages/*', 'apps/*'], is valid pnpm config, but it produced no patterns, so check and fix silently looked at the root alone. Two more gaps:

  • Globs only expanded one dir/* level.
  • An exclusion like '!**/test/**' could never match.

Changes

  • Parsing: pnpm-workspace.yaml is read with the yaml package core already ships. Block and flow lists, quotes and comments all work.
  • package.json: "workspaces" is read as an array (npm, yarn) or as yarn's { packages: [...], nohoist: [...] }.
  • Matching: patterns are matched as globs against the directories that hold a package.json, skipping node_modules and dot-directories. * and ? match within one path segment, ** across segments, and ! patterns exclude. This is a small built-in matcher rather than a new dependency for the bundled CLI. ignore (already installed) was not usable: under gitignore rules, packages/* also matches everything nested below it.
  • Loud failure: a workspace file whose patterns match no package now fails with an INVALID_WORKSPACE error (CLI exit 2) that names the file and patterns. Invalid YAML gets the same error. A pnpm-workspace.yaml with settings or catalogs and no packages key is still a single-package repository.
error: pnpm-workspace.yaml declares packages (libs/*) but none of them matches a directory with a package.json; fix the patterns, or Uptide would check only the root

Fixtures (fixtures/workspaces/)

Fixture Form Result
pnpm-block block list, quotes, comments, packages/**, !**/test/** ., apps/web, packages/core
pnpm-flow the same patterns as a one-line list ., apps/web, packages/core
npm-array "workspaces": [...] ., apps/web, packages/core
yarn-object "workspaces": { "packages": [...] } ., apps/web, packages/core
pnpm-settings-only catalog and settings, no packages .
pnpm-root-only packages: ['.', '!vendor/**'] .
pnpm-build-names packages/*, tools/* with packages named build, dist, out all of them found
pnpm-globstar packages/** packages/a and packages/build; nothing inside node_modules, dist or build
pnpm-no-match packages: ['libs/*'] with no libs/ INVALID_WORKSPACE

In both pnpm fixtures, packages/core/test/fixture/package.json is excluded by !**/test/**.

Against main's implementation, 4 of the 7 new tests fail. uptide list --json on pnpm-flow now reports the three workspaces.

Tests: core 804 passed, cli 313 passed. tsc and biome are clean (public:check was not run: it needs the private denylist). A patch changeset is included.

After review

  • Only commands raise INVALID_WORKSPACE. list, check and fix keep the error. Two callers only probe: fetch/npmrc.ts (walks parent directories for the project's .npmrc) and action/detect.ts. Both now use workspacePackagesOrRoot, which answers ['.'] for a broken workspace file. Test: a parent directory whose pnpm-workspace.yaml is invalid YAML, or matches nothing, no longer breaks list in a child repository.
  • Walk. Each pattern walks only its static prefix (packages/* reads packages/ one level deep), as deep as the pattern reaches. ** has no depth limit, and a leading ** walks the whole repository. node_modules, bower_components and dot-directories are never a match and never walked: pnpm's finder ignores the first two and globs with dot: false. dist, build, coverage and out are matched like any other directory, so packages/build is found with packages/* (netlify/build has one), but the walk never goes inside them.
  • Memo. Results and errors are kept per root for the life of the process. Callers get a copy.
  • Root pattern. '.' names the root, so packages: ['.', '!x/**'] is a single-package repository. The first commit wrongly raised INVALID_WORKSPACE for this; timing against a real repository caught it.
  • One glob matcher. matchesPaths in action/detect.ts uses the shared globRegExp instead of its own copy.

Timing (median of 3 runs, Node 22, macOS)

Synthetic repository: 63,566 directories, 9,301 package.json files, 601 workspace packages under apps/*, packages/* and tooling/**. Every package has deep src/ trees, plus dist/, coverage/ and a node_modules/ with 15 packages.

first call 50 more calls (npmrc-style probing)
main (line parser, one level) 2.7 ms 64.8 ms
first commit of this PR (full walk) 1,315 ms 69,371 ms
this PR now 6.4 ms 0.3 ms

On 7 real local monorepos (3–12k directories; pnpm and npm), main and this PR resolve byte-identical package lists, rechecked after the build-name fix (1, 1, 1, 10, 3, 7 and 3 packages). The first call takes 0.3–4.8 ms, mostly loading the YAML parser, and repeat calls take 0.0–0.1 ms.

🤖 Generated with Claude Code

lucasbraum and others added 4 commits October 5, 2026 16:48
…use patterns that match nothing

workspacePackagesOf read pnpm-workspace.yaml line by line and only knew the
block list. The flow form, packages: ['packages/*', 'apps/*'], which pnpm
accepts, produced no patterns, so check and fix silently looked at the root
alone. Globs were also limited to one 'dir/*' level, and an exclusion such
as '!**/test/**' could never match.

The file is parsed with the yaml package core already ships (block and flow
lists, quotes, comments); package.json "workspaces" is read as an array or
as yarn's { packages: [...] }. Patterns are matched as globs over the
directories holding a package.json (node_modules and dot-directories
skipped): * and ? within a segment, ** across segments, ! exclusions.

A workspace file whose patterns match no package is an INVALID_WORKSPACE
error naming the file and the patterns; invalid YAML is the same error. A
pnpm-workspace.yaml with settings and no packages key stays a
single-package repository.

Fixtures for each form live in fixtures/workspaces/.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Lucas Braum <[email protected]>
…prefixes, memoized

Review of the previous commit:

- Probes no longer throw. fetch/npmrc.ts walks parent directories looking
  for the project root, and action/detect.ts reads workspaces of two
  checkouts; both use workspacePackagesOrRoot, which answers ['.'] for a
  broken workspace file. list, check and fix keep the error. A broken
  pnpm-workspace.yaml in a parent directory no longer breaks list in a child
  repository (list.test.ts, invalid YAML and patterns matching nothing).

- Each pattern walks only its static prefix (packages/* reads packages/ one
  level deep), as deep as the pattern reaches; ** is unbounded and a leading
  one walks the repository. Wildcards do not descend into dist, build,
  coverage, out, bower_components, node_modules or dot-directories; an exact
  path is taken as written. Results (and errors) are memoized per root for
  the process, and callers get a copy.

- '.' names the root: a pnpm-workspace.yaml listing only '.' (with
  exclusions) is a single-package repository. The previous commit called it
  an INVALID_WORKSPACE error, found on a real repository while timing this.

- action/detect.ts's matchesPaths uses the same globRegExp instead of its
  own copy.

On a synthetic 63,566-directory repository (601 packages), the first call
went from 1,315 ms (previous commit) to 6.4 ms, and 50 further calls from
69 s to 0.3 ms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Lucas Braum <[email protected]>
… hide a match

With packages/*, a real package at packages/build or tools/out was dropped
silently: the walk skipped dist, build, coverage, out and dot-directories
before testing the pattern (netlify/build has packages/build). Each entry is
now matched first; those names only stop the walk into them.

node_modules and bower_components are still never a match: pnpm's own
package finder ignores them, so neither is a workspace package.

Fixtures: pnpm-build-names (packages/build, packages/dist and tools/out are
found) and pnpm-globstar (packages/** reaches packages/build but not what
sits inside node_modules, dist or a build-named package).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Lucas Braum <[email protected]>
pnpm globs workspace patterns with dot: false, so a directory whose name
starts with a dot is never a workspace package, even under **. The previous
commit matched packages/.hidden; it is now excluded and not walked, like
node_modules and bower_components. Build-named directories (dist, build,
coverage, out) still match and are only not walked into.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Lucas Braum <[email protected]>
@lucasbraum
lucasbraum merged commit 2bea992 into main Oct 6, 2026
5 checks passed
@lucasbraum
lucasbraum deleted the fix/pnpm-workspace-yaml branch October 6, 2026 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant