-
-
Notifications
You must be signed in to change notification settings - Fork 460
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
feat: allow prettier to use custom node
#3061
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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,147 @@ | ||
import { fileURLToPath, URL } from "url"; | ||
import { ChildProcess, fork, ForkOptions } from "child_process"; | ||
import { EventEmitter } from "events"; | ||
|
||
const alive = (child: ChildProcess): boolean => { | ||
try { | ||
return !!process.kill(child.pid!, 0); | ||
} catch (e: any) { | ||
if (e.code === "EPERM" || e.code === "ESRCH") { | ||
return false; | ||
} | ||
throw e; | ||
} | ||
}; | ||
|
||
const kill = async (child: ChildProcess) => { | ||
if (!alive(child)) { | ||
return; | ||
} | ||
return new Promise((resolve) => { | ||
let timeout: NodeJS.Timeout | null = null; | ||
child.once("exit", () => { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
} | ||
resolve(undefined); | ||
}); | ||
child.kill("SIGINT"); | ||
timeout = setTimeout(() => { | ||
child.kill("SIGTERM"); | ||
timeout = null; | ||
}, 3000); | ||
}); | ||
}; | ||
|
||
export class ChildProcessWorker { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class mimics |
||
#process: ChildProcess | null = null; | ||
#url: URL; | ||
#processOptions: ForkOptions; | ||
#events: EventEmitter; | ||
#queue: any[]; | ||
#exitCode: number | null = null; | ||
|
||
constructor(url: URL, processOptions: ForkOptions) { | ||
this.#url = url; | ||
this.#processOptions = processOptions; | ||
this.#events = new EventEmitter(); | ||
this.#queue = []; | ||
void Promise.resolve().then(() => this.startProcess()); | ||
} | ||
|
||
startProcess() { | ||
try { | ||
const stderr: Buffer[] = []; | ||
const stdout: Buffer[] = []; | ||
this.#process = fork(fileURLToPath(this.#url), [], { | ||
...this.#processOptions, | ||
stdio: ["pipe", "pipe", "pipe", "ipc"], | ||
}); | ||
this.#process.stderr?.on("data", (chunk) => { | ||
stderr.push(chunk); | ||
}); | ||
this.#process.stdout?.on("data", (chunk) => { | ||
stdout.push(chunk); | ||
}); | ||
this.#process | ||
.on("error", (err) => { | ||
this.#process = null; | ||
this.#exitCode = -1; | ||
this.#events.emit("error", err); | ||
}) | ||
.on("exit", (code) => { | ||
this.#process = null; | ||
this.#exitCode = code; | ||
const stdoutResult = Buffer.concat(stdout).toString("utf8"); | ||
const stderrResult = Buffer.concat(stderr).toString("utf8"); | ||
if (code !== 0) { | ||
this.#events.emit( | ||
"error", | ||
new Error( | ||
`Process crashed with code ${code}: ${stdoutResult} ${stderrResult}` | ||
) | ||
); | ||
} else { | ||
this.#events.emit( | ||
"error", | ||
new Error( | ||
`Process unexpectedly exit: ${stdoutResult} ${stderrResult}` | ||
) | ||
); | ||
} | ||
}) | ||
.on("message", (msg) => { | ||
this.#events.emit("message", msg); | ||
}); | ||
this.flushQueue(); | ||
} catch (err) { | ||
this.#process = null; | ||
this.#events.emit("error", err); | ||
} | ||
} | ||
|
||
on(evt: string, fn: (payload: any) => void) { | ||
if (evt === "message" || evt === "error") { | ||
this.#events.on(evt, fn); | ||
return; | ||
} | ||
throw new Error(`Unsupported event ${evt}.`); | ||
} | ||
|
||
async terminate(): Promise<number> { | ||
if (!this.#process) { | ||
return this.#exitCode ?? -1; | ||
} | ||
await kill(this.#process); | ||
return this.#exitCode ?? -1; | ||
} | ||
|
||
flushQueue() { | ||
if (!this.#process) { | ||
return; | ||
} | ||
let items = 0; | ||
for (const entry of this.#queue) { | ||
if (!this.#process.send(entry)) { | ||
break; | ||
} | ||
items++; | ||
} | ||
if (items > 0) { | ||
this.#queue.splice(0, items); | ||
} | ||
} | ||
|
||
postMessage(data: any) { | ||
this.flushQueue(); | ||
if (this.#process) { | ||
if (this.#process.send(data)) { | ||
return true; | ||
} else { | ||
this.#queue.push(data); | ||
} | ||
} | ||
this.#queue.push(data); | ||
return false; | ||
} | ||
} |
This file contains 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 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 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 |
---|---|---|
|
@@ -8,14 +8,19 @@ import { | |
PrettierPlugin, | ||
PrettierSupportLanguage, | ||
} from "./types"; | ||
import { ChildProcessWorker } from "./ChildProcessWorker"; | ||
import { | ||
PrettierInstance, | ||
PrettierInstanceConstructor, | ||
PrettierInstanceContext, | ||
} from "./PrettierInstance"; | ||
import { ResolveConfigOptions, Options } from "prettier"; | ||
|
||
const worker = new Worker( | ||
url.pathToFileURL(path.join(__dirname, "/worker/prettier-instance-worker.js")) | ||
const processWorker = url.pathToFileURL( | ||
path.join(__dirname, "/worker/prettier-instance-worker-process.js") | ||
); | ||
const threadWorker = url.pathToFileURL( | ||
path.join(__dirname, "/worker/prettier-instance-worker-process-thread.js") | ||
); | ||
|
||
export const PrettierWorkerInstance: PrettierInstanceConstructor = class PrettierWorkerInstance | ||
|
@@ -34,11 +39,25 @@ export const PrettierWorkerInstance: PrettierInstanceConstructor = class Prettie | |
} | ||
> = new Map(); | ||
|
||
private worker: ChildProcessWorker | Worker | null = null; | ||
private currentCallMethodId = 0; | ||
|
||
public version: string | null = null; | ||
|
||
constructor(private modulePath: string) { | ||
constructor( | ||
private modulePath: string, | ||
private context: PrettierInstanceContext | ||
) { | ||
this.createWorker(); | ||
} | ||
|
||
private createWorker() { | ||
const worker = this.context.config.runtime | ||
? new ChildProcessWorker(processWorker, { | ||
execPath: this.context.config.runtime, | ||
}) | ||
: new Worker(threadWorker); | ||
|
||
worker.on("message", ({ type, payload }) => { | ||
switch (type) { | ||
case "import": { | ||
|
@@ -60,13 +79,27 @@ export const PrettierWorkerInstance: PrettierInstanceConstructor = class Prettie | |
} | ||
} | ||
}); | ||
worker.on("error", async (err) => { | ||
this.worker = null; | ||
this.context.loggingService.logInfo( | ||
`Worker error ${err.message}`, | ||
err.stack | ||
); | ||
await worker.terminate(); | ||
this.createWorker(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: Consider throttling this. |
||
}); | ||
|
||
this.worker = worker; | ||
} | ||
|
||
public async import(): Promise</* version of imported prettier */ string> { | ||
if (!this.worker) { | ||
throw new Error("Worker not available."); | ||
} | ||
const promise = new Promise<string>((resolve, reject) => { | ||
this.importResolver = { resolve, reject }; | ||
}); | ||
worker.postMessage({ | ||
this.worker.postMessage({ | ||
type: "import", | ||
payload: { modulePath: this.modulePath }, | ||
}); | ||
|
@@ -123,11 +156,14 @@ export const PrettierWorkerInstance: PrettierInstanceConstructor = class Prettie | |
} | ||
|
||
private callMethod(methodName: string, methodArgs: unknown[]): Promise<any> { | ||
if (!this.worker) { | ||
throw new Error("Worker not available."); | ||
} | ||
const callMethodId = this.currentCallMethodId++; | ||
const promise = new Promise((resolve, reject) => { | ||
this.callMethodResolvers.set(callMethodId, { resolve, reject }); | ||
}); | ||
worker.postMessage({ | ||
this.worker.postMessage({ | ||
type: "callMethod", | ||
payload: { | ||
id: callMethodId, | ||
|
This file contains 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
待办事项 翻译我