Repository navigation
fix(build): model each script as its own module, as Node already does - #975
Merged
Merged
Conversation
tsc's default moduleDetection treats a file without import/export as a global script. Our operational scripts use `const x = require(...)`, so they were modelled as sharing one scope — a scope they never share at runtime, where Node and ts-node give each file its own module scope. So backfill-byo-host-stamp.ts had claimed `mongoose`, `APPLY` and `main` repo-wide, and the next script written the same way failed TS2451. #953 and #962 each added one and each went red on the other's names, which reads as 'my PR is broken' rather than 'the model is wrong'. scripts/ is in tsconfig.build.json as well, so the next merge would have broken `npm run build` and the deploy — not only the CI gate. Verified before landing: 7 files repo-wide are modelled as global scripts, all standalone scripts plus jest.setup.js, no server runtime file. The sole emit change is an added "use strict"; shebangs stay on line 1. None of the affected .js scripts use sloppy-mode constructs. Full typecheck and a full build both ran clean with the flag.
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.
What
One line in
backend/tsconfig.json:"moduleDetection": "force".Why
Two open PRs — #953 (unlist internal agents from the public catalog) and #962
(backfill explicit runtime identity) — are both red, with what looks like each
one's own bug:
Neither PR is broken. They collide with a file already on
main.tsc's defaultmoduleDetection: "auto"treats any file with no top-levelimport/exportas a global script. Our operational scripts are writtenwith
const mongoose = require('mongoose')— the house style — so tsc modelsthem as sharing one global scope.
They never share that scope. Node and ts-node execute each file as a CommonJS
module, where a top-level
constis file-scoped. The type model was simplywrong about how these files run.
The practical consequence:
backfill-byo-host-stamp.tslanded onmainandclaimed the names
mongoose,APPLYandmainrepo-wide. The next scriptwritten the same way fails, and the failure is attributed to the innocent PR.
Two authors hit it independently this week.
This was about to break the deploy, not just CI
scripts/is not excluded fromtsconfig.build.json, andnpm run buildistsc -p tsconfig.build.json. Merging either PR would have gone red on theproduction build, not only the typecheck gate.
Why not the alternatives
export {};in each script — works, but is a per-file reminder. The nextauthor must know the trap exists. Three PRs would already have needed it.
scripts/from the typecheck config — that config's own headersays exclusions are "the debt ledger, not an escape hatch", and it would not
have fixed the build path anyway.
Blast radius, measured before landing
Files in the repo currently modelled as global scripts — i.e. every file whose
emit this flag changes:
Seven files. All standalone scripts plus the jest setup — no server runtime
file is affected.
The only emit change is an added
"use strict";, and the shebang stays online 1:
The four
.jsscripts were scanned for constructs that behave differentlyunder strict mode (implicit-global assignment,
with,arguments.callee,legacy octal) — none present.
Verification
the flag, and disappears with it.
tsc --noEmitover the whole backend with the flag: byte-identicaloutput to the baseline run (both show only two pre-existing
instrument.tserrors from a local
@sentry/nodegap, which CI does not have).tsc -p tsconfig.build.jsonwith the flag, emitting to a scratch dir:compiles,
server.jsemitted.After this merges
#953 and #962 should both go green on a re-run with no changes of their own.