Skip to content

fix: scope default wildcard glob tracing to workspace and node_modules - #10

Open
byte-lyh wants to merge 1 commit into
web-infra-dev:mainfrom
byte-lyh:fix/default-trace-glob-scope
Open

byte-lyh wants to merge 1 commit into
web-infra-dev:mainfrom
byte-lyh:fix/default-trace-glob-scope

Conversation

@byte-lyh

Copy link
Copy Markdown

Problem

nodeDepEmit invokes @vercel/nft's nodeFileTrace with base: "/" so symlinked dependencies outside appDir — in particular shared external pnpm stores (e.g. ~/.local/share/pnpm/store/...) and framework CLI includeEntries — do not resolve to ../ paths after realpath() and get dropped by @vercel/nft's built-in path.startsWith(".." + path.sep) check.

However, setting base: "/" disables @vercel/nft's directory boundary guard completely. Whenever traced server bundles or dependencies contain a dynamic filesystem path expression rooted at / or above the project directory (for example fs.readFileSync(path.resolve("/", dynamicSegment))), @vercel/nft's emitAssetDirectory / emitWildcardRequire emits an unguarded wildcard glob (glob("/**/*")):

  1. Permission failures (EACCES): With @vercel/nft 0.27 (glob 7), hitting any unreadable host directory such as /root or isolated container sibling directories throws EACCES and aborts deployment builds.
  2. Full-disk crawl & sibling package leakage: With @vercel/nft 0.29+ (glob 10+), glob swallows EACCES and walks every readable directory across the entire host filesystem, hurting build performance and potentially copying files from unrelated sibling repositories on the host that happen to sit within path.resolve(appDir, "../../../../../../").

Why keeping base: "/" + scoping wildcard globs is preferable to changing base (cf. #7)

Setting base to a workspace directory (traceRoot) causes @vercel/nft to resolve package symlinks via realpath() before computing path.relative(base, realPath), which turns external shared pnpm store packages and external includeEntries into ../ paths and either fails validation or silently drops dependencies, while also requiring callers (@modern-js/app-tools, etc.) to opt in manually.

Scoping wildcard globs via ignore while keeping base: "/" works out of the box with zero caller changes and preserves external pnpm store symlinks.

Changes

  • src/utils.ts:
    • Automatically discover allowed trace roots from appDir, sourceDir, includeEntries, and symlinked local packages (link: / file: / pnpm link in node_modules whose realpath points outside node_modules), walking up to the outermost workspace root (pnpm-workspace.yaml, rush.json, eden.monorepo.json, lerna.json, or package.json#workspaces, falling back to .git or nearest package.json) and expanding realpath.
    • Add createDefaultTraceIgnore in traceFiles:
      • Concrete resolved files (no [*?[] wildcards) are never blocked, avoiding regressions for explicit relative require() or custom resolution paths.
      • Wildcard globs (emitAssetDirectory / emitWildcardRequire) are allowed only when their directory prefix prior to the first wildcard is inside allowedRoots or inside a node_modules/<pkg> package directory (including external shared pnpm stores). Host-wide globs (/**/*, /root/**/*, sibling host directories) are ignored before glob() runs.
    • Compose caller-supplied traceOptions.ignore (both function and string | string[] glob patterns) on top of the default boundary.
  • src/index.ts:
    • Reuse allowedRoots alongside dependencySearchRoot when matching workspace package.json manifests so deeply nested monorepos (> 6 directory levels) and symlinked paths also resolve cleanly.
  • .changeset/scope-default-trace-globs.md: Add patch changeset for ndepe.

Validation

  • pnpm exec biome check .
  • pnpm build (ESM, CJS, and .d.ts output; verified CJS require and ESM import smoke checks)
  • pnpm exec vitest --run (11 unit/integration tests passed, including a new test covering an unreadable 0o000 host directory, an unrelated outside-workspace host package, nested monorepo workspaces > 6 levels deep, a link: external repository with dynamic assets, an external shared pnpm store with dynamic assets, and caller traceOptions.ignore composition)
  • node --test scripts/*.node-test.mjs (8 release utility tests passed)

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