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 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ package-lock.json
.eslintcache
*v8.log
/lib/
*.blob
3 changes: 1 addition & 2 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
33 changes: 19 additions & 14 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
39 changes: 31 additions & 8 deletions src/tsc/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,39 @@ 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()) {
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
(v8 as any).startupSnapshot.setDeserializeMainFunction(() => {
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
// 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();
});
}
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
Loading