-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Allow tsc to be run within a node v8 snapshot #55830
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
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a4f19b0
Allow tsc to be run within a node v8 snapshot
jakebailey 502961d
Another TODO
jakebailey a8e7b8b
Merge branch 'main' into node-snapshotting
jakebailey c6c6377
Add snapshotify script for testing
jakebailey 88f31b9
Merge branch 'main' into node-snapshotting
jakebailey 351b2d7
Fix in bun whose api is present but throws
jakebailey 92f29f0
Merge branch 'main' into node-snapshotting
jakebailey 69c81ad
Hashing is ok
jakebailey 13720a0
Cleanup, fix
jakebailey 6d14cce
Leave todo about fallback
jakebailey 944b399
Push this into src
jakebailey 94bb856
Detect non-v8 snapshotable systems
jakebailey 14e36f0
Remove unused
jakebailey 75c9404
Revert gitignore
jakebailey e43ced9
Fix smoke test
jakebailey 0b20495
Update src/tsc/tsc.ts
jakebailey 69fcecb
Merge branch 'main' into node-snapshotting
jakebailey 27c10eb
reduce any
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../tsconfig-base", | ||
"compilerOptions": { | ||
"types": ["node"] | ||
}, | ||
"references": [], | ||
"include": ["**/*"] | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.