fix(jest): rename __dirname shadow in build scripts; revert #116413 workaround#116455
Merged
Conversation
`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>
Contributor
📊 Type Coverage Diff✅ No new type safety issues introduced. Coverage: 93.59% |
ryan953
approved these changes
May 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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`):
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
Verified on
🤖 Generated with Claude Code