From a4f19b04b11bebdb166c91a24efabf1c08481b4f Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Fri, 22 Sep 2023 08:13:55 -0700 Subject: [PATCH 01/14] Allow tsc to be run within a node v8 snapshot --- .gitignore | 1 + src/compiler/core.ts | 3 +-- src/compiler/sys.ts | 33 +++++++++++++++++++-------------- src/tsc/tsc.ts | 38 ++++++++++++++++++++++++++++++-------- src/tsc/tsconfig.json | 1 + 5 files changed, 52 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index b084ac14ecc28..0f92f4dbef4bf 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,4 @@ package-lock.json .eslintcache *v8.log /lib/ +*.blob diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 21eae040ba922..30cebcc4e3c3d 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -2744,6 +2744,5 @@ export function isNodeLikeSystem(): boolean { // and definitely exist. return typeof process !== "undefined" && !!process.nextTick - && !(process as any).browser - && typeof module === "object"; + && !(process as any).browser; // TODO(jakebailey): figure out a better "is node" check } diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index d888a3d599bb3..cdf10a215e6a3 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -1454,8 +1454,8 @@ interface DirectoryWatcher extends FileWatcher { referenceCount: number; } -// TODO: GH#18217 this is used as if it's certainly defined in many places. -export let sys: System = (() => { +/** @internal */ +export function createSystem(): System { // NodeJS detects "\uFEFF" at the start of the string and *replaces* it with the actual // byte order mark from the specified encoding. Using any other byte order mark does // not actually work. @@ -2008,21 +2008,26 @@ export let sys: System = (() => { patchWriteFileEnsuringDirectory(sys); } return sys!; -})(); +} + +// TODO: GH#18217 this is used as if it's certainly defined in many places. +export let sys: System; /** @internal */ export function setSys(s: System) { sys = s; -} -if (sys && sys.getEnvironmentVariable) { - setCustomPollingValues(sys); - Debug.setAssertionLevel( - /^development$/i.test(sys.getEnvironmentVariable("NODE_ENV")) - ? AssertionLevel.Normal - : AssertionLevel.None, - ); -} -if (sys && sys.debugMode) { - Debug.isDebugging = true; + if (sys && sys.getEnvironmentVariable) { + setCustomPollingValues(sys); + Debug.setAssertionLevel( + /^development$/i.test(sys.getEnvironmentVariable("NODE_ENV")) + ? AssertionLevel.Normal + : AssertionLevel.None, + ); + } + if (sys && sys.debugMode) { + Debug.isDebugging = true; + } } + +setSys(createSystem()); diff --git a/src/tsc/tsc.ts b/src/tsc/tsc.ts index d7c3c42a01f61..f5ee5cbd9467c 100644 --- a/src/tsc/tsc.ts +++ b/src/tsc/tsc.ts @@ -9,16 +9,38 @@ ts.Debug.loggingHost = { }, }; -if (ts.Debug.isDebugging) { - ts.Debug.enableDebugInfo(); +let v8: typeof import("v8") | undefined; +try { + v8 = require("v8"); } - -if (ts.sys.tryEnableSourceMapsForHost && /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV"))) { - ts.sys.tryEnableSourceMapsForHost(); +catch { + // do nothing } -if (ts.sys.setBlocking) { - ts.sys.setBlocking(); +function main() { + if (ts.Debug.isDebugging) { + ts.Debug.enableDebugInfo(); + } + + if (ts.sys.tryEnableSourceMapsForHost && /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV"))) { + ts.sys.tryEnableSourceMapsForHost(); + } + + if (ts.sys.setBlocking) { + ts.sys.setBlocking(); + } + + ts.executeCommandLine(ts.sys, ts.noop, ts.sys.args); } -ts.executeCommandLine(ts.sys, ts.noop, ts.sys.args); +if ((v8 as any)?.startupSnapshot.isBuildingSnapshot()) { + (v8 as any).startupSnapshot.setDeserializeMainFunction(() => { + // When we're executed as a snapshot, argv won't contain the js file anymore. + process.argv.splice(1, 0, __filename); + ts.setSys(ts.createSystem()); + main(); + }); +} +else { + main(); +} diff --git a/src/tsc/tsconfig.json b/src/tsc/tsconfig.json index 21e193ad5d70c..46f22c372f8ed 100644 --- a/src/tsc/tsconfig.json +++ b/src/tsc/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "../tsconfig-base", "compilerOptions": { + "types": ["node"] }, "references": [ { "path": "../compiler" }, From 502961d3037d6378344ecd53a6886d41ec7b1d1c Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Fri, 22 Sep 2023 08:40:27 -0700 Subject: [PATCH 02/14] Another TODO --- src/tsc/tsc.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tsc/tsc.ts b/src/tsc/tsc.ts index f5ee5cbd9467c..6b0c3c49067ba 100644 --- a/src/tsc/tsc.ts +++ b/src/tsc/tsc.ts @@ -36,6 +36,7 @@ function main() { if ((v8 as any)?.startupSnapshot.isBuildingSnapshot()) { (v8 as any).startupSnapshot.setDeserializeMainFunction(() => { // When we're executed as a snapshot, argv won't contain the js file anymore. + // TODO(jakebailey): if we need to fork TS, we probably need to know the snapshot name and exec that... process.argv.splice(1, 0, __filename); ts.setSys(ts.createSystem()); main(); From c6c637794fa9d48b4e893da430ecb14726d0b843 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Sat, 30 Sep 2023 23:28:20 -0700 Subject: [PATCH 03/14] Add snapshotify script for testing --- snapshotify.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 snapshotify.js diff --git a/snapshotify.js b/snapshotify.js new file mode 100644 index 0000000000000..23849bb1fe94e --- /dev/null +++ b/snapshotify.js @@ -0,0 +1,47 @@ +const fs = require("fs"); +const path = require("path"); +const crypto = require("crypto"); +const cp = require("child_process"); + +const [exeArg, ...args] = process.argv.slice(2); + +const doBuildSnapshot = process.env.BUILD_SNAPSHOT === "true"; + +function checksumFile(hashName, path) { + return new Promise((resolve, reject) => { + const hash = crypto.createHash(hashName); + const stream = fs.createReadStream(path); + stream.on("error", err => reject(err)); + stream.on("data", chunk => hash.update(chunk)); + stream.on("end", () => resolve(hash.digest("hex"))); + }); +} + +async function main() { + const exe = path.resolve(exeArg); + // const exeHash = await checksumFile("md5", exe); + // const blobName = `${exe}.${exeHash}.blob`; + const blobName = `${exe}.${process.version}.blob`; + + if (doBuildSnapshot) { + const tmpName = `${blobName}.tmp`; + cp.execFileSync(process.execPath, ["--snapshot-blob", tmpName, "--build-snapshot", exe], { stdio: "ignore" }); + fs.renameSync(tmpName, blobName); + return; + } + + if (!fs.existsSync(blobName)) { + cp.spawn(process.execPath, [__filename, exeArg], { detached: true, stdio: "ignore", env: { ...process.env, BUILD_SNAPSHOT: true } }).unref(); + require(exe); + return; + } + + try { + cp.execFileSync(process.execPath, ["--snapshot-blob", blobName, "--", ...args], { stdio: "inherit" }); + } + catch (e) { + process.exitCode = e.status; + } +} + +main(); From 351b2d7b71891810349c71563981aeea7c8480b3 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 19 Oct 2023 15:52:19 -0700 Subject: [PATCH 04/14] Fix in bun whose api is present but throws --- src/tsc/tsc.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/tsc/tsc.ts b/src/tsc/tsc.ts index 6b0c3c49067ba..c19f8d81e597b 100644 --- a/src/tsc/tsc.ts +++ b/src/tsc/tsc.ts @@ -11,7 +11,9 @@ ts.Debug.loggingHost = { let v8: typeof import("v8") | undefined; try { - v8 = require("v8"); + if (!process.versions.bun) { + v8 = require("v8"); + } } catch { // do nothing From 69c81add8d3789b0f08de25959d43138cc976956 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 9 Jan 2024 11:44:21 -0800 Subject: [PATCH 05/14] Hashing is ok --- snapshotify.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/snapshotify.js b/snapshotify.js index 23849bb1fe94e..45f66842e05cd 100644 --- a/snapshotify.js +++ b/snapshotify.js @@ -7,21 +7,20 @@ const [exeArg, ...args] = process.argv.slice(2); const doBuildSnapshot = process.env.BUILD_SNAPSHOT === "true"; -function checksumFile(hashName, path) { - return new Promise((resolve, reject) => { - const hash = crypto.createHash(hashName); - const stream = fs.createReadStream(path); - stream.on("error", err => reject(err)); - stream.on("data", chunk => hash.update(chunk)); - stream.on("end", () => resolve(hash.digest("hex"))); - }); +function checksumFile(path) { + // Benchmarking shows that sha1 is the fastest hash. + // It is theoretically insecure, but we're just using it to detect file mismatches. + const hash = crypto.createHash("sha1"); + const file = fs.readFileSync(path); + hash.update(file); + return hash.digest("hex"); } -async function main() { +function main() { const exe = path.resolve(exeArg); - // const exeHash = await checksumFile("md5", exe); - // const blobName = `${exe}.${exeHash}.blob`; - const blobName = `${exe}.${process.version}.blob`; + const exeHash = checksumFile(exe); + const blobName = `${exe}.${process.version}.${exeHash}.blob`; + // const blobName = `${exe}.${process.version}.blob`; if (doBuildSnapshot) { const tmpName = `${blobName}.tmp`; From 13720a04e83e32262f4ff3aaee7bcc3ef9bb5d27 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:21:46 -0800 Subject: [PATCH 06/14] Cleanup, fix --- snapshotify.js | 60 +++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/snapshotify.js b/snapshotify.js index 45f66842e05cd..c0fca759299e0 100644 --- a/snapshotify.js +++ b/snapshotify.js @@ -5,7 +5,7 @@ const cp = require("child_process"); const [exeArg, ...args] = process.argv.slice(2); -const doBuildSnapshot = process.env.BUILD_SNAPSHOT === "true"; +const doBuildSnapshot = process.env.TYPESCRIPT_BUILD_SNAPSHOT === "true"; function checksumFile(path) { // Benchmarking shows that sha1 is the fastest hash. @@ -16,31 +16,45 @@ function checksumFile(path) { return hash.digest("hex"); } -function main() { - const exe = path.resolve(exeArg); - const exeHash = checksumFile(exe); - const blobName = `${exe}.${process.version}.${exeHash}.blob`; - // const blobName = `${exe}.${process.version}.blob`; - - if (doBuildSnapshot) { - const tmpName = `${blobName}.tmp`; - cp.execFileSync(process.execPath, ["--snapshot-blob", tmpName, "--build-snapshot", exe], { stdio: "ignore" }); +const exe = path.resolve(exeArg); +const exeHash = checksumFile(exe); +const blobName = `${exe}.${process.version}.${exeHash}.blob`; +// const blobName = `${exe}.${process.version}.blob`; + +if (doBuildSnapshot) { + // Build and atomic rename. + const tmpName = `${blobName}.${process.pid}.tmp`; + cp.execFileSync( + process.execPath, + ["--snapshot-blob", tmpName, "--build-snapshot", exe], + { stdio: "ignore" }, + ); + try { fs.renameSync(tmpName, blobName); - return; } - - if (!fs.existsSync(blobName)) { - cp.spawn(process.execPath, [__filename, exeArg], { detached: true, stdio: "ignore", env: { ...process.env, BUILD_SNAPSHOT: true } }).unref(); - require(exe); - return; + catch { + // If the rename fails, it's because another process beat us to it. } + return; +} - try { - cp.execFileSync(process.execPath, ["--snapshot-blob", blobName, "--", ...args], { stdio: "inherit" }); - } - catch (e) { - process.exitCode = e.status; - } +if (!fs.existsSync(blobName)) { + cp.spawn( + process.execPath, + [__filename, exeArg], + { + detached: true, + stdio: "ignore", + env: { ...process.env, TYPESCRIPT_BUILD_SNAPSHOT: true }, + }, + ).unref(); + require(exe); + return; } -main(); +try { + cp.execFileSync(process.execPath, ["--snapshot-blob", blobName, "--", ...args], { stdio: "inherit" }); +} +catch (e) { + process.exitCode = e.status; +} From 6d14cce763830240f9b84d4759b57bb77889938f Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:23:36 -0800 Subject: [PATCH 07/14] Leave todo about fallback --- snapshotify.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/snapshotify.js b/snapshotify.js index c0fca759299e0..f9315b9f9265c 100644 --- a/snapshotify.js +++ b/snapshotify.js @@ -10,6 +10,8 @@ const doBuildSnapshot = process.env.TYPESCRIPT_BUILD_SNAPSHOT === "true"; function checksumFile(path) { // Benchmarking shows that sha1 is the fastest hash. // It is theoretically insecure, but we're just using it to detect file mismatches. + // TODO(jakebailey): If sha1 is ever removed, this will fail; we should try catch + // and fall back to something from crypto.getHashes() if it does. const hash = crypto.createHash("sha1"); const file = fs.readFileSync(path); hash.update(file); From 944b399107382439e35b6ba39bef4edf91bc7495 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:46:32 -0800 Subject: [PATCH 08/14] Push this into src --- Herebyfile.mjs | 32 +++++++++++++++++++++--- snapshotify.js => src/tscSnapshot/tsc.ts | 22 ++++++++-------- src/tscSnapshot/tsconfig.json | 8 ++++++ 3 files changed, 47 insertions(+), 15 deletions(-) rename snapshotify.js => src/tscSnapshot/tsc.ts (75%) create mode 100644 src/tscSnapshot/tsconfig.json diff --git a/Herebyfile.mjs b/Herebyfile.mjs index 9e437c9f68bc8..6bde82b5c5aa9 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -8,7 +8,9 @@ import esbuild from "esbuild"; import { EventEmitter, } from "events"; -import fs from "fs"; +import fs, { + watch, +} from "fs"; import _glob from "glob"; import { task, @@ -363,17 +365,39 @@ function entrypointBuildTask(options) { return { build, bundle, shim, main, watch }; } -const { main: tsc, watch: watchTsc } = entrypointBuildTask({ - name: "tsc", +const { main: tscReal, watch: watchTscReal } = entrypointBuildTask({ + name: "tscReal", description: "Builds the command-line compiler", buildDeps: [generateDiagnostics], project: "src/tsc", srcEntrypoint: "./src/tsc/tsc.ts", builtEntrypoint: "./built/local/tsc/tsc.js", + output: "./built/local/tscReal.js", + mainDeps: [generateLibs], +}); + +const { main: tscSnapshot, watch: watchTscSnapshot } = entrypointBuildTask({ + name: "tscSnapshot", + description: "Builds the command-line compiler", + buildDeps: [generateDiagnostics], + project: "src/tsc", + srcEntrypoint: "./src/tscSnapshot/tsc.ts", + builtEntrypoint: "./built/local/tscSnapshot/tsc.js", output: "./built/local/tsc.js", mainDeps: [generateLibs], }); -export { tsc, watchTsc }; + +export const tsc = task({ + name: "tsc", + description: "Builds the command-line compiler", + dependencies: [tscReal, tscSnapshot], +}); + +export const watchTsc = task({ + name: "watchTsc", + description: "Builds the command-line compiler", + dependencies: [watchTscReal, watchTscSnapshot], +}); const { main: services, build: buildServices, watch: watchServices } = entrypointBuildTask({ name: "services", diff --git a/snapshotify.js b/src/tscSnapshot/tsc.ts similarity index 75% rename from snapshotify.js rename to src/tscSnapshot/tsc.ts index f9315b9f9265c..004c8cfb047fd 100644 --- a/snapshotify.js +++ b/src/tscSnapshot/tsc.ts @@ -1,13 +1,13 @@ -const fs = require("fs"); -const path = require("path"); -const crypto = require("crypto"); -const cp = require("child_process"); +import fs = require("fs"); +import path = require("path"); +import crypto = require("crypto"); +import cp = require("child_process"); -const [exeArg, ...args] = process.argv.slice(2); +const args = process.argv.slice(2); const doBuildSnapshot = process.env.TYPESCRIPT_BUILD_SNAPSHOT === "true"; -function checksumFile(path) { +function checksumFile(path: string) { // Benchmarking shows that sha1 is the fastest hash. // It is theoretically insecure, but we're just using it to detect file mismatches. // TODO(jakebailey): If sha1 is ever removed, this will fail; we should try catch @@ -18,7 +18,7 @@ function checksumFile(path) { return hash.digest("hex"); } -const exe = path.resolve(exeArg); +const exe = path.join(__dirname, "tscReal.js"); const exeHash = checksumFile(exe); const blobName = `${exe}.${process.version}.${exeHash}.blob`; // const blobName = `${exe}.${process.version}.blob`; @@ -37,21 +37,21 @@ if (doBuildSnapshot) { catch { // If the rename fails, it's because another process beat us to it. } - return; + process.exit(0); } if (!fs.existsSync(blobName)) { cp.spawn( process.execPath, - [__filename, exeArg], + [__filename], { detached: true, stdio: "ignore", - env: { ...process.env, TYPESCRIPT_BUILD_SNAPSHOT: true }, + env: { ...process.env, TYPESCRIPT_BUILD_SNAPSHOT: "true" }, }, ).unref(); require(exe); - return; + throw new Error("unreachable"); } try { diff --git a/src/tscSnapshot/tsconfig.json b/src/tscSnapshot/tsconfig.json new file mode 100644 index 0000000000000..e39519532fd80 --- /dev/null +++ b/src/tscSnapshot/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../tsconfig-base", + "compilerOptions": { + "types": ["node"] + }, + "references": [], + "include": ["**/*"] +} From 94bb8568ddee499b6090e34b14d3882fd4f44818 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:58:24 -0800 Subject: [PATCH 09/14] Detect non-v8 snapshotable systems --- src/tscSnapshot/tsc.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/tscSnapshot/tsc.ts b/src/tscSnapshot/tsc.ts index 004c8cfb047fd..b805594cfa2f9 100644 --- a/src/tscSnapshot/tsc.ts +++ b/src/tscSnapshot/tsc.ts @@ -1,13 +1,30 @@ import fs = require("fs"); import path = require("path"); -import crypto = require("crypto"); import cp = require("child_process"); +let v8: typeof import("v8") | undefined; +try { + if (!process.versions.bun) { + v8 = require("v8"); + } +} +catch { + // do nothing +} + +const exe = path.join(__dirname, "tscReal.js"); + +if (!(v8 as any)?.startupSnapshot) { + require(exe); + throw new Error("unreachable"); +} + const args = process.argv.slice(2); const doBuildSnapshot = process.env.TYPESCRIPT_BUILD_SNAPSHOT === "true"; function checksumFile(path: string) { + const crypto = require("crypto") as typeof import("crypto"); // Benchmarking shows that sha1 is the fastest hash. // It is theoretically insecure, but we're just using it to detect file mismatches. // TODO(jakebailey): If sha1 is ever removed, this will fail; we should try catch @@ -18,10 +35,8 @@ function checksumFile(path: string) { return hash.digest("hex"); } -const exe = path.join(__dirname, "tscReal.js"); const exeHash = checksumFile(exe); const blobName = `${exe}.${process.version}.${exeHash}.blob`; -// const blobName = `${exe}.${process.version}.blob`; if (doBuildSnapshot) { // Build and atomic rename. From 14e36f03a13d307fa3c3a2e9cbb5e05f64fd7c5c Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:59:12 -0800 Subject: [PATCH 10/14] Remove unused --- Herebyfile.mjs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index 6bde82b5c5aa9..41540fb59da64 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -8,9 +8,7 @@ import esbuild from "esbuild"; import { EventEmitter, } from "events"; -import fs, { - watch, -} from "fs"; +import fs from "fs"; import _glob from "glob"; import { task, From 75c9404a22d182b17c532fde69d85aa25baed615 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 9 Jan 2024 15:07:16 -0800 Subject: [PATCH 11/14] Revert gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0f92f4dbef4bf..b084ac14ecc28 100644 --- a/.gitignore +++ b/.gitignore @@ -63,4 +63,3 @@ package-lock.json .eslintcache *v8.log /lib/ -*.blob From e43ced9e1e03798564a1221cbaeb1c9814577c72 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 9 Jan 2024 15:11:57 -0800 Subject: [PATCH 12/14] Fix smoke test --- scripts/produceLKG.mjs | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/produceLKG.mjs b/scripts/produceLKG.mjs index 7e13fc7c78e82..cb8f91d5639d5 100644 --- a/scripts/produceLKG.mjs +++ b/scripts/produceLKG.mjs @@ -52,6 +52,7 @@ async function copyTypesMap() { async function copyScriptOutputs() { await copyFromBuiltLocal("cancellationToken.js"); await copyFromBuiltLocal("tsc.js"); + await copyFromBuiltLocal("tscReal.js"); await copyFromBuiltLocal("tsserver.js"); await copyFromBuiltLocal("tsserverlibrary.js"); await copyFromBuiltLocal("typescript.js"); From 0b20495cf09e44bd418ff2b79d21523e438d665c Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 9 Jan 2024 20:37:48 -0800 Subject: [PATCH 13/14] Update src/tsc/tsc.ts --- src/tsc/tsc.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tsc/tsc.ts b/src/tsc/tsc.ts index c19f8d81e597b..bc30614d8bf36 100644 --- a/src/tsc/tsc.ts +++ b/src/tsc/tsc.ts @@ -35,7 +35,7 @@ function main() { ts.executeCommandLine(ts.sys, ts.noop, ts.sys.args); } -if ((v8 as any)?.startupSnapshot.isBuildingSnapshot()) { +if ((v8 as any)?.startupSnapshot?.isBuildingSnapshot()) { (v8 as any).startupSnapshot.setDeserializeMainFunction(() => { // When we're executed as a snapshot, argv won't contain the js file anymore. // TODO(jakebailey): if we need to fork TS, we probably need to know the snapshot name and exec that... From 27c10ebfcc2f6d48ed10f4f1935cab04c324db77 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:16:50 -0700 Subject: [PATCH 14/14] reduce any --- src/tsc/tsc.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/tsc/tsc.ts b/src/tsc/tsc.ts index bc30614d8bf36..9fa8b4a4568ea 100644 --- a/src/tsc/tsc.ts +++ b/src/tsc/tsc.ts @@ -9,7 +9,14 @@ ts.Debug.loggingHost = { }, }; -let v8: typeof import("v8") | undefined; +interface V8 { + startupSnapshot?: { + isBuildingSnapshot(): boolean; + setDeserializeMainFunction(fn: () => void): void; + }; +} + +let v8: V8 | undefined; try { if (!process.versions.bun) { v8 = require("v8"); @@ -35,10 +42,9 @@ function main() { ts.executeCommandLine(ts.sys, ts.noop, ts.sys.args); } -if ((v8 as any)?.startupSnapshot?.isBuildingSnapshot()) { - (v8 as any).startupSnapshot.setDeserializeMainFunction(() => { +if (v8?.startupSnapshot?.isBuildingSnapshot()) { + v8.startupSnapshot.setDeserializeMainFunction(() => { // When we're executed as a snapshot, argv won't contain the js file anymore. - // TODO(jakebailey): if we need to fork TS, we probably need to know the snapshot name and exec that... process.argv.splice(1, 0, __filename); ts.setSys(ts.createSystem()); main();