Skip to content

fix(jest): rename __dirname shadow in build scripts; revert #116413 workaround#116455

Merged
armcknight merged 2 commits into
masterfrom
andrewmcknight/jest-rename-dirname-shadow
May 29, 2026
Merged

fix(jest): rename __dirname shadow in build scripts; revert #116413 workaround#116455
armcknight merged 2 commits into
masterfrom
andrewmcknight/jest-rename-dirname-shadow

Conversation

@armcknight
Copy link
Copy Markdown
Member

Problem

PR #116413 added `/scripts/` to `testPathIgnorePatterns` and `modulePathIgnorePatterns` to work around a Jest+SWC cascade:

```
/scripts/genPlatformProductInfo.ts:51
const __dirname = _nodepath.dirname((0, _nodeurl.fileURLToPath)(require("url").pathToFileURL(__filename).toString()));
^
SyntaxError: Identifier '__dirname' has already been declared
```

That fix appeared to work on the original REVENG-86 PR (#116269) and was merged, but a later PR (getsentry/sentry#116420) reproduced the exact same cascade despite the ignore patterns being present.

Reason: `testPathIgnorePatterns` and `modulePathIgnorePatterns` only govern Jest's test-discovery and runtime-environment `require`. They don't stop Jest's pre-test transform during `--listTests --changedSince` from invoking SWC on files in `scripts/`. So the parse error still happens whenever `--changedSince` traversal reaches one of these scripts.

Solution

Fix at the source. Two files use `fileURLToPath(import.meta.url)` to recover ESM-equivalent `__dirname` (and one of them `__filename`):

  • `scripts/genPlatformProductInfo.ts:29`
  • `scripts/extractFormFields.ts:14–15`

When SWC compiles these to CommonJS for Jest, the generated `const __dirname = ...` collides with the binding Node already provides via the module wrapper, producing the SyntaxError. Renaming the locals to `THIS_DIR` / `THIS_FILE` removes the collision without changing behavior — those identifiers were only used as locals within their own modules.

Commits

  1. `fix(scripts): rename local __dirname shadow that breaks Jest+SWC` — the actual root-cause fix.
  2. `revert(jest): remove scripts/ ignore patterns now that root cause is fixed` — removes the fix(jest): exclude scripts/ from discovery and module resolution #116413 workaround that was dead config and misleadingly documented.

Verified on

🤖 Generated with Claude Code

Andrew McKnight and others added 2 commits May 28, 2026 15:27
`scripts/genPlatformProductInfo.ts` and `scripts/extractFormFields.ts`
both reconstruct `__dirname` (and one of them `__filename`) under ESM
via `path.dirname(fileURLToPath(import.meta.url))`. When Jest's
`@swc/jest` transformer compiles these files to CommonJS for
dependency analysis (e.g. during `--listTests --changedSince` on a
PR), SWC emits the local `const __dirname = ...` verbatim. That
collides with the `__dirname` Node already provides via the module
wrapper:

```
/scripts/genPlatformProductInfo.ts:51
const __dirname = _nodepath.dirname((0, _nodeurl.fileURLToPath)(
    require("url").pathToFileURL(__filename).toString()));
      ^
SyntaxError: Identifier '__dirname' has already been declared
```

The crash cascades through Jest's worker pool and surfaces as a wave
of "Your test suite must contain at least one test" on unrelated
files, ultimately timing out the shards at 30 min.

The earlier fix in #116413 (testPathIgnorePatterns +
modulePathIgnorePatterns for `<rootDir>/scripts/`) only governs
test discovery and the in-test-environment require; it doesn't stop
Jest's pre-test dependency analysis from invoking the transformer on
these scripts. Renaming the shadowing identifiers removes the
collision at the source.

Behavior-equivalent at runtime: the original locals only shadowed
the wrapper-provided names inside the module, and the renamed
THIS_DIR / THIS_FILE are used identically (for path.resolve /
path.join in the same module).

Verified: `pnpm exec jest --listTests --json --changedSince=$(git
rev-parse origin/master) --passWithNoTests` returns `[]` locally
with no parse errors.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…fixed

PR #116413 added `<rootDir>/scripts/` to `testPathIgnorePatterns` and
`modulePathIgnorePatterns` as a workaround for the SWC `__dirname`
SyntaxError cascade. Investigation showed those patterns don't actually
prevent the failure (they only govern test discovery and the runtime
test environment, not Jest's pre-test transform during
`--listTests --changedSince`).

The real fix is in the preceding commit (`fix(scripts): rename local
__dirname shadow ...`), which removes the collision at the source by
renaming the local helpers to `THIS_DIR` / `THIS_FILE`. With that in
place, the ignore patterns are dead config that misleadingly claim to
address the SWC issue. Remove them.

Verified: `pnpm exec jest --listTests --json --changedSince=$(git
rev-parse origin/master) --passWithNoTests` still returns `[]`
locally after the revert.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@armcknight armcknight requested review from a team as code owners May 28, 2026 23:28
@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label May 28, 2026
@github-actions
Copy link
Copy Markdown
Contributor

📊 Type Coverage Diff

✅ No new type safety issues introduced. Coverage: 93.59%

@armcknight armcknight enabled auto-merge (squash) May 28, 2026 23:47
@armcknight armcknight merged commit 5118a2e into master May 29, 2026
79 checks passed
@armcknight armcknight deleted the andrewmcknight/jest-rename-dirname-shadow branch May 29, 2026 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants