Unverified Commit 6fa344b8 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbopack-ecmascript-runtime: type-check every runtime file (#99670)

## Summary

Most of the Turbopack runtime in `turbopack-ecmascript-runtime/js/src`
was never type-checked, and editors showed hundreds of bogus "Cannot
find name" errors in it.

- `src/nodejs/tsconfig.json` had `"include": ["*.ts"]`, which only
matched `nodejs/hmr-types.d.ts`. So `nodejs/runtime/*` and
`nodejs/dev/*` were in no project, and `pnpm check` never covered them.
- `browser/runtime/base/tsconfig.json` included only `runtime-base.ts`
and `dummy.ts`. `build-base.ts`, `dev-base.ts` and `dev-dummy.ts` were
left out. `dev-base.ts` was checked indirectly through the `dom`
project.
- tsserver then fell back to `turbopack/tsconfig.json`, which uses
`"module": "NodeNext"`. That implies `moduleDetection: force`, so these
global-script files were treated as modules and their shared `///
<reference>` declarations disappeared.

`build-base.ts` and `dev-base.ts` are alternative entry points that both
define `instantiateModule`, so they can't share a project. In both
`nodejs/` and `browser/runtime/base/`, `tsconfig.json` is now a
solution-style config that references a `tsconfig.build.json` and a
`tsconfig.dev.json`. tsserver follows these references, so every file
opens in the right project. Files that are only pulled in through `///
<reference>` are listed in `include` explicitly, because tsserver
doesn't otherwise count them as part of a project. The `check:` scripts
are split the same way.

`tsconfig.base.json` now sets `moduleDetection: "auto"` explicitly.
That's already the default for `module: CommonJS`, but it documents that
these files have to stay scripts.

This also fixes the errors in files that were never checked before:

- `nodejs-globals.d.ts` referenced `shared/runtime-types.d.ts`, which
doesn't exist. The file is `shared/runtime/runtime-types.d.ts`.
- In `nodejs/runtime/build-base.ts`, `moduleCache.get(...)` returned
`Module` where `ModuleWithDirection` was expected. This matches the
existing cast on `nodeContextPrototype.c`.
- `nodejs/dev/dev-nodejs.ts` read `.merged` on an instruction union
where only `ChunkListUpdate` has that field.
- `nodejs/runtime/dev-base.ts` had an unused parameter.

## Verification

- `pnpm check` in `turbopack/crates/turbopack-ecmascript-runtime/js`
passes.
- Opened all 31 `.ts` files in a real tsserver. Each resolves to one of
this package's tsconfigs, with 0 errors.
- Not run: Turbopack snapshot tests. The `dev-nodejs.ts` change affects
emitted runtime code, so snapshots may need regenerating.

<!-- NEXT_JS_LLM -->
parent bc335b88
......@@ -7,9 +7,11 @@
"scripts": {
"typescript": "pnpm check",
"check": "run-p check:*",
"check:nodejs": "tsc -p src/nodejs",
"check:nodejs-build": "tsc -p src/nodejs/tsconfig.build.json",
"check:nodejs-dev": "tsc -p src/nodejs/tsconfig.dev.json",
"check:browser-dev-client": "tsc -p src/browser/dev/hmr-client",
"check:browser-runtime-base": "tsc -p src/browser/runtime/base",
"check:browser-runtime-base-build": "tsc -p src/browser/runtime/base/tsconfig.build.json",
"check:browser-runtime-base-dev": "tsc -p src/browser/runtime/base/tsconfig.dev.json",
"check:browser-runtime-dom": "tsc -p src/browser/runtime/dom",
"check:browser-runtime-self-contained": "tsc -p src/browser/runtime/self-contained"
},
......
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
// environment, we need WebWorker for WebAssembly types
"lib": ["ESNext", "WebWorker"]
},
"include": ["globals.d.ts", "runtime-base.ts", "dummy.ts", "build-base.ts"]
}
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
// environment, we need WebWorker for WebAssembly types
"lib": ["ESNext", "WebWorker"]
},
"include": [
"globals.d.ts",
"runtime-base.ts",
"dummy.ts",
"dev-dummy.ts",
"dev-base.ts"
]
}
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
// environment, we need WebWorker for WebAssembly types
"lib": ["ESNext", "WebWorker"]
},
"include": ["runtime-base.ts", "dummy.ts"]
"files": [],
"references": [
{ "path": "./tsconfig.build.json" },
{ "path": "./tsconfig.dev.json" }
]
}
......@@ -62,11 +62,13 @@ if (handlers.size === 0) {
// updates) or nested inside `merged` entries (chunks covered by a
// merger). Collect both so routing isn't skipped just because a mergeable
// chunk's update only reports its paths inside `merged`.
const instruction = update.instruction
const updateChunkPaths = new Set<string>([
...Object.keys(update.instruction?.chunks ?? {}),
...(update.instruction?.merged ?? []).flatMap((merged) =>
Object.keys(merged.chunks ?? {})
),
...Object.keys(instruction?.chunks ?? {}),
...(
(instruction?.type === 'ChunkListUpdate' && instruction.merged) ||
[]
).flatMap((merged) => Object.keys(merged.chunks ?? {})),
])
const toCall: HmrHandlerEntry[] = []
......
......@@ -76,7 +76,7 @@ function getOrInstantiateModuleFromParent(
id: ModuleId,
sourceModule: ModuleWithDirection
): ModuleWithDirection {
const module = moduleCache.get(id)
const module = moduleCache.get(id) as ModuleWithDirection | undefined
if (module) {
if (module.error) {
......@@ -107,7 +107,7 @@ function getOrInstantiateRuntimeModule(
chunkPath: ChunkPath,
moduleId: ModuleId
): ModuleWithDirection {
const module = moduleCache.get(moduleId)
const module = moduleCache.get(moduleId) as ModuleWithDirection | undefined
if (module) {
if (module.error) {
......
......@@ -89,7 +89,7 @@ function instantiateModule(
}
// Node.js: no hooks wrapper, just execute directly
const runWithHooks = (module: HotModule, exec: (refresh: any) => void) => {
const runWithHooks = (_module: HotModule, exec: (refresh: any) => void) => {
exec(undefined) // no refresh context
}
......
/// <reference path="../../shared/runtime-types.d.ts" />
/// <reference path="../../shared/runtime/runtime-types.d.ts" />
/**
* Global type definitions for Node.js Turbopack runtime.
......
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
// environment, we need WebWorker for WebAssembly types (not part of @types/node yet)
"lib": ["ESNext", "WebWorker"],
"types": ["node"]
},
"include": [
"runtime/nodejs-globals.d.ts",
"runtime/runtime-base.ts",
"runtime/build-base.ts"
]
}
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
// environment, we need WebWorker for WebAssembly types (not part of @types/node yet)
"lib": ["ESNext", "WebWorker"],
"types": ["node"]
},
"include": [
"hmr-types.d.ts",
"runtime/nodejs-globals.d.ts",
"runtime/runtime-base.ts",
"runtime/dev-base.ts",
"dev/*.ts"
]
}
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
// environment, we need WebWorker for WebAssembly types (not part of @types/node yet)
"lib": ["ESNext", "WebWorker"],
"types": ["node"]
},
"include": ["*.ts"]
"files": [],
"references": [
{ "path": "./tsconfig.build.json" },
{ "path": "./tsconfig.dev.json" }
]
}
......@@ -13,6 +13,9 @@
// modules
"module": "CommonJS",
// The runtime is written as global scripts that share declarations through
// `/// <reference>`, so files must not be implicitly treated as modules.
"moduleDetection": "auto",
"types": [], // disable implicit types.
// emit
......
......@@ -1586,7 +1586,7 @@ if (globalThis.__turbopack_ensure_chunk__ !== undefined) {
return new Context(module1, exports);
};
// Node.js: no hooks wrapper, just execute directly
const runWithHooks = (module1, exec)=>{
const runWithHooks = (_module, exec)=>{
exec(undefined); // no refresh context
};
// Use shared instantiation logic (includes hot API setup)
......@@ -1775,9 +1775,10 @@ if (handlers.size === 0) {
// updates) or nested inside `merged` entries (chunks covered by a
// merger). Collect both so routing isn't skipped just because a mergeable
// chunk's update only reports its paths inside `merged`.
const instruction = update.instruction;
const updateChunkPaths = new Set([
...Object.keys(update.instruction?.chunks ?? {}),
...(update.instruction?.merged ?? []).flatMap((merged)=>Object.keys(merged.chunks ?? {}))
...Object.keys(instruction?.chunks ?? {}),
...(instruction?.type === 'ChunkListUpdate' && instruction.merged || []).flatMap((merged)=>Object.keys(merged.chunks ?? {}))
]);
const toCall = [];
if (updateChunkPaths.size === 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment