Skip to content

Commit

Permalink
Fix import of server-main.js
Browse files Browse the repository at this point in the history
  • Loading branch information
code-asher committed Oct 16, 2024
1 parent 184800a commit 4744383
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 14 deletions.
96 changes: 88 additions & 8 deletions patches/integration.diff
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,98 @@ Index: code-server/lib/vscode/src/vs/server/node/webClientServer.ts
embedderIdentifier: 'server-distro',
extensionsGallery: this._webExtensionResourceUrlTemplate && this._productService.extensionsGallery ? {
...this._productService.extensionsGallery,
Index: code-server/lib/vscode/src/server-main.js
Index: code-server/lib/vscode/src/code-server.js
===================================================================
--- code-server.orig/lib/vscode/src/server-main.js
+++ code-server/lib/vscode/src/server-main.js
@@ -339,4 +339,9 @@ function prompt(question) {
});
}

-start();
--- /dev/null
+++ code-server/lib/vscode/src/code-server.js
@@ -0,0 +1,66 @@
+// This is a copy of server-main.js (the parts we use), before it was converted
+// to ESM. Directly importing it in code-server has proven difficult. We will
+// need to to consider some new options going forward.
+//
+// 1. Convert code-server to ESM (best option?).
+// 2. Do not import, fork a process, and communicate via IPC.
+
+// @ts-check
+'use strict';
+
+/**
+ * @import { INLSConfiguration } from './vs/nls'
+ */
+
+// Keep bootstrap-amd.js from redefining 'fs'.
+delete process.env['ELECTRON_RUN_AS_NODE'];
+
+const path = require('path');
+const performance = require('perf_hooks').performance;
+const bootstrapNode = require('./bootstrap-node');
+const bootstrapAmd = require('./bootstrap-amd');
+const { resolveNLSConfiguration } = require('./vs/base/node/nls');
+const product = require('./bootstrap-meta').product;
+const perf = require(`./vs/base/common/performance`);
+
+perf.mark('code/server/start');
+// @ts-ignore
+global.vscodeServerStartTime = performance.now();
+
+/**
+ * @param {INLSConfiguration} nlsConfiguration
+ * @returns { Promise<typeof import('./vs/server/node/server.main')> }
+ */
+function loadCode(nlsConfiguration) {
+ return new Promise((resolve, reject) => {
+
+ /** @type {INLSConfiguration} */
+ process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfiguration); // required for `bootstrap-amd` to pick up NLS messages
+
+ // See https://github.com/microsoft/vscode-remote-release/issues/6543
+ // We would normally install a SIGPIPE listener in bootstrap-node.js
+ // But in certain situations, the console itself can be in a broken pipe state
+ // so logging SIGPIPE to the console will cause an infinite async loop
+ process.env['VSCODE_HANDLES_SIGPIPE'] = 'true';
+
+ if (process.env['VSCODE_DEV']) {
+ // When running out of sources, we need to load node modules from remote/node_modules,
+ // which are compiled against nodejs, not electron
+ process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'] = process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'] || path.join(__dirname, '..', 'remote', 'node_modules');
+ bootstrapNode.devInjectNodeModuleLookupPath(process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH']);
+ } else {
+ delete process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'];
+ }
+ bootstrapAmd.load('vs/server/node/server.main', resolve, reject);
+ });
+}
+
+/**
+ * @returns { Promise<typeof import('./vs/server/node/server.main')> }
+ */
+async function loadCodeWithNls() {
+ const nlsConfiguration = await resolveNLSConfiguration({ userLocale: 'en', osLocale: 'en', commit: product.commit, userDataPath: '', nlsMetadataPath: __dirname });
+ return loadCode(nlsConfiguration);
+}
+
+module.exports.loadCodeWithNls = loadCodeWithNls;
Index: code-server/lib/vscode/build/gulpfile.reh.js
===================================================================
--- code-server.orig/lib/vscode/build/gulpfile.reh.js
+++ code-server/lib/vscode/build/gulpfile.reh.js
@@ -164,6 +164,7 @@ const serverWithWebEntryPoints = [

const commonJSEntryPoints = [
'out-build/server-main.js',
+ 'out-build/code-server.js',
'out-build/server-cli.js',
'out-build/bootstrap-fork.js',
];
Index: code-server/lib/vscode/src/tsconfig.json
===================================================================
--- code-server.orig/lib/vscode/src/tsconfig.json
+++ code-server/lib/vscode/src/tsconfig.json
@@ -36,6 +36,7 @@
"./bootstrap-window.js",
"./cli.js",
"./main.js",
+ "./code-server.js",
"./server-main.js",
"./server-cli.js",
"./vs/base/common/jsonc.js",
2 changes: 1 addition & 1 deletion src/node/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface OpenCommandPipeArgs {
export const runCodeCli = async (args: DefaultedArgs): Promise<void> => {
logger.debug("Running Code CLI")
try {
const mod = require(path.join(vsRootPath, "out/server-main")) as VSCodeModule
const mod = require(path.join(vsRootPath, "out/code-server")) as VSCodeModule
const serverModule = await mod.loadCodeWithNls()
await serverModule.spawnCli(await toCodeArgs(args))
// Rather than have the caller handle errors and exit, spawnCli will exit
Expand Down
6 changes: 3 additions & 3 deletions src/node/routes/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ export interface IVSCodeServerAPI {
*/
export type VSCodeModule = {
// See ../../../lib/vscode/src/server-main.js:339.
loadCodeWithNls(): {
loadCodeWithNls(): Promise<{
// See ../../../lib/vscode/src/vs/server/node/server.main.ts:72.
createServer(address: string | net.AddressInfo | null, args: CodeArgs): Promise<IVSCodeServerAPI>
// See ../../../lib/vscode/src/vs/server/node/server.main.ts:65.
spawnCli(args: CodeArgs): Promise<void>
}
}>
}

/**
* Load then create the VS Code server.
*/
async function loadVSCode(req: express.Request): Promise<IVSCodeServerAPI> {
const mod = require(path.join(vsRootPath, "out/server-main")) as VSCodeModule
const mod = require(path.join(vsRootPath, "out/code-server")) as VSCodeModule
const serverModule = await mod.loadCodeWithNls()
return serverModule.createServer(null, {
...(await toCodeArgs(req.args)),
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es6",
"lib": ["es2020", "dom", "dom.iterable"],
"target": "es2022",
"lib": ["es2022", "dom", "dom.iterable"],
"module": "commonjs",
"moduleResolution": "node",
"strict": true,
Expand Down

0 comments on commit 4744383

Please sign in to comment.