Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tsc to be run within a node v8 snapshot #55830

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions Herebyfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -393,17 +393,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",
Expand Down
1 change: 1 addition & 0 deletions scripts/produceLKG.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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");
Expand Down
33 changes: 19 additions & 14 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1469,8 +1469,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.
Expand Down Expand Up @@ -2026,21 +2026,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());
45 changes: 38 additions & 7 deletions src/tsc/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,47 @@ ts.Debug.loggingHost = {
},
};

if (ts.Debug.isDebugging) {
ts.Debug.enableDebugInfo();
interface V8 {
startupSnapshot?: {
isBuildingSnapshot(): boolean;
setDeserializeMainFunction(fn: () => void): void;
};
}

if (ts.sys.tryEnableSourceMapsForHost && /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV"))) {
ts.sys.tryEnableSourceMapsForHost();
let v8: V8 | undefined;
try {
if (!process.versions.bun) {
v8 = require("v8");
}
}
catch {
// do nothing
}

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();
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?.startupSnapshot?.isBuildingSnapshot()) {
v8.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();
}
1 change: 1 addition & 0 deletions src/tsc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../tsconfig-base",
"compilerOptions": {
"types": ["node"]
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
},
"references": [
{ "path": "../compiler" },
Expand Down
77 changes: 77 additions & 0 deletions src/tscSnapshot/tsc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import fs = require("fs");
import path = require("path");
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
// and fall back to something from crypto.getHashes() if it does.
const hash = crypto.createHash("sha1");
const file = fs.readFileSync(path);
hash.update(file);
return hash.digest("hex");
}

const exeHash = checksumFile(exe);
const blobName = `${exe}.${process.version}.${exeHash}.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);
}
catch {
// If the rename fails, it's because another process beat us to it.
}
process.exit(0);
}

if (!fs.existsSync(blobName)) {
cp.spawn(
process.execPath,
[__filename],
{
detached: true,
stdio: "ignore",
env: { ...process.env, TYPESCRIPT_BUILD_SNAPSHOT: "true" },
},
).unref();
require(exe);
throw new Error("unreachable");
}

try {
cp.execFileSync(process.execPath, ["--snapshot-blob", blobName, "--", ...args], { stdio: "inherit" });
}
catch (e) {
process.exitCode = e.status;
}
8 changes: 8 additions & 0 deletions src/tscSnapshot/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig-base",
"compilerOptions": {
"types": ["node"]
},
"references": [],
"include": ["**/*"]
}