Skip to content

Commit 73d6b77

Browse files
committed
Externalize spdlog .node module
1 parent 670003c commit 73d6b77

9 files changed

Lines changed: 38 additions & 35 deletions

File tree

build/tasks.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,14 @@ const buildServerBinaryCopy = register("build:server:binary:copy", async (runner
8282
const webOutputPath = path.join(pkgsPath, "web", "out");
8383
const browserAppOutputPath = path.join(pkgsPath, "app", "browser", "out");
8484
const nodePtyModule = path.join(pkgsPath, "protocol", "node_modules", "node-pty", "build", "Release", "pty.node");
85+
const spdlogModule = path.join(pkgsPath, "server", "node_modules", "spdlog", "build", "Release", "spdlog.node");
8586

8687
if (!fs.existsSync(nodePtyModule)) {
8788
throw new Error("Could not find pty.node. Ensure all packages have been installed");
8889
}
90+
if (!fs.existsSync(spdlogModule)) {
91+
throw new Error("Could not find spdlog.node. Ensure all packages have been installed");
92+
}
8993
if (!fs.existsSync(webOutputPath)) {
9094
throw new Error("Web bundle must be built");
9195
}
@@ -114,6 +118,7 @@ const buildServerBinaryCopy = register("build:server:binary:copy", async (runner
114118
cpDir(browserAppOutputPath, "unauth", browserAppOutputPath);
115119
fse.mkdirpSync(path.join(cliBuildPath, "modules"));
116120
fse.copySync(nodePtyModule, path.join(cliBuildPath, "modules", "pty.node"));
121+
fse.copySync(spdlogModule, path.join(cliBuildPath, "modules", "spdlog.node"));
117122
});
118123

119124
const buildServerBundle = register("build:server:bundle", async (runner) => {

packages/server/scripts/nexe.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ nexe.compile({
99
input: path.join(__dirname, "../out/cli.js"),
1010
output: `cli-${process.env.TRAVIS_OS_NAME || os.platform()}`,
1111
targets: [os.platform()],
12-
native: {
13-
spdlog: {
14-
additionalFiles: [
15-
'spdlog.node'
16-
],
17-
},
18-
},
1912
/**
2013
* To include native extensions, do NOT install node_modules for each one. They
2114
* are not required as each extension is built using webpack.

packages/server/src/cli.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,15 @@ export class Entry extends Command {
3838
}];
3939

4040
public async run(): Promise<void> {
41-
try {
42-
/**
43-
* Suuuper janky
44-
* Comes from - https://github.com/nexe/nexe/issues/524
45-
* Seems to cleanup by removing this path immediately
46-
* If any native module is added its assumed this pathname
47-
* will change.
48-
*/
49-
require("spdlog");
50-
const nodePath = path.join(process.cwd(), "e91a410b");
51-
fs.unlinkSync(path.join(nodePath, "spdlog.node"));
52-
fs.rmdirSync(nodePath);
53-
} catch (ex) {
54-
logger.warn("Failed to remove extracted dependency.", field("dependency", "spdlog"), field("error", ex.message));
55-
}
56-
5741
if (isCli) {
5842
fillFs();
5943
}
6044

6145
const { args, flags } = this.parse(Entry);
46+
const dataDir = flags["data-dir"] || path.join(os.homedir(), ".vscode-remote");
47+
const workingDir = args["workdir"];
6248

49+
setupNativeModules(dataDir);
6350
const builtInExtensionsDir = path.join(buildDir || path.join(__dirname, ".."), "build/extensions");
6451
if (flags["bootstrap-fork"]) {
6552
const modulePath = flags["bootstrap-fork"];
@@ -75,7 +62,7 @@ export class Entry extends Command {
7562
process.argv[i + 2] = arg;
7663
});
7764

78-
return requireModule(modulePath, builtInExtensionsDir);
65+
return requireModule(modulePath, dataDir, builtInExtensionsDir);
7966
}
8067

8168
if (flags["fork"]) {
@@ -84,9 +71,6 @@ export class Entry extends Command {
8471
return requireFork(modulePath, JSON.parse(flags.args!), builtInExtensionsDir);
8572
}
8673

87-
const dataDir = flags["data-dir"] || path.join(os.homedir(), ".vscode-remote");
88-
const workingDir = args["workdir"];
89-
9074
if (buildDir && buildDir.startsWith(workingDir)) {
9175
logger.error("Cannot run binary inside of BUILD_DIR", field("build_dir", buildDir), field("cwd", process.cwd()));
9276
process.exit(1);
@@ -95,7 +79,7 @@ export class Entry extends Command {
9579
if (!fs.existsSync(dataDir)) {
9680
fs.mkdirSync(dataDir);
9781
}
98-
setupNativeModules(dataDir);
82+
require("spdlog");
9983

10084
const logDir = path.join(dataDir, "logs", new Date().toISOString().replace(/[-:.TZ]/g, ""));
10185
process.env.VSCODE_LOGS = logDir;

packages/server/src/modules.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ export const setup = (dataDirectory: string): void => {
3434
* for this is unknown ATM, but this patch works around it.
3535
*/
3636
unpackModule("pty");
37+
unpackModule("spdlog");
3738
const nodePtyUtils = require("../../protocol/node_modules/node-pty/lib/utils") as typeof import("../../protocol/node_modules/node-pty/src/utils");
3839
// tslint:disable-next-line:no-any
3940
nodePtyUtils.loadNative = (modName: string): any => {
4041
return __non_webpack_require__(path.join(dataDirectory, "modules", modName + ".node"));
4142
};
43+
// tslint:disable-next-line:no-any
44+
(<any>global).SPDLOG_LOCATION = path.join(dataDirectory, "modules", "spdlog.node");
4245
// tslint:disable-next-line:no-unused-expression
4346
require("../../protocol/node_modules/node-pty/lib/index") as typeof import("../../protocol/node_modules/node-pty/src/index");
4447
};

packages/server/src/vscode/bootstrapFork.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const requireFork = (modulePath: string, args: string[], builtInExtension
7171
}
7272
};
7373

74-
export const requireModule = (modulePath: string, builtInExtensionsDir: string): void => {
74+
export const requireModule = (modulePath: string, dataDir: string, builtInExtensionsDir: string): void => {
7575
process.env.AMD_ENTRYPOINT = modulePath;
7676
const xml = require("xhr2");
7777
xml.XMLHttpRequest.prototype._restrictedHeaders["user-agent"] = false;
@@ -96,7 +96,7 @@ export const requireModule = (modulePath: string, builtInExtensionsDir: string):
9696
*/
9797
// tslint:disable-next-line:no-any
9898
(<any>cp).fork = (modulePath: string, args: ReadonlyArray<string> = [], options?: cp.ForkOptions): cp.ChildProcess => {
99-
return cp.spawn(process.execPath, ["--fork", modulePath, "--args", JSON.stringify(args)], {
99+
return cp.spawn(process.execPath, ["--fork", modulePath, "--args", JSON.stringify(args), "--data-dir", dataDir], {
100100
...options,
101101
stdio: [null, null, null, "ipc"],
102102
});
@@ -123,7 +123,7 @@ export const requireModule = (modulePath: string, builtInExtensionsDir: string):
123123
* cp.stderr.on("data", (data) => console.log(data.toString("utf8")));
124124
* @param modulePath Path of the VS Code module to load.
125125
*/
126-
export const forkModule = (modulePath: string, args: string[], options: cp.ForkOptions): cp.ChildProcess => {
126+
export const forkModule = (modulePath: string, args: string[], options: cp.ForkOptions, dataDir?: string): cp.ChildProcess => {
127127
let proc: cp.ChildProcess;
128128
const forkArgs = ["--bootstrap-fork", modulePath];
129129
if (args) {
@@ -134,6 +134,9 @@ export const forkModule = (modulePath: string, args: string[], options: cp.ForkO
134134
delete options.env.ELECTRON_RUN_AS_NODE;
135135
forkArgs.push("--env", JSON.stringify(options.env));
136136
}
137+
if (dataDir) {
138+
forkArgs.push("--data-dir", dataDir);
139+
}
137140
const forkOptions: cp.ForkOptions = {
138141
stdio: [null, null, null, "ipc"],
139142
};

packages/server/src/vscode/sharedProcess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class SharedProcess {
8484
VSCODE_ALLOW_IO: "true",
8585
VSCODE_LOGS: process.env.VSCODE_LOGS,
8686
},
87-
});
87+
}, this.userDataDir);
8888
if (this.logger.level <= Level.Trace) {
8989
this.activeProcess.stdout.on("data", (data) => {
9090
this.logger.trace(() => ["stdout", field("data", data.toString())]);

packages/server/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module.exports = merge({
4848
__dirname: false,
4949
setImmediate: false
5050
},
51-
externals: ["spdlog", "tslib", "trash"],
51+
externals: ["tslib", "trash"],
5252
entry: "./packages/server/src/cli.ts",
5353
target: "node",
5454
plugins: [

packages/vscode/webpack.config.bootstrap.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ module.exports = (env) => {
1616
entry: path.join(root, "lib/vscode/src/bootstrap-fork.js"),
1717
mode: "development",
1818
target: "node",
19-
externals: ["spdlog"],
2019
output: {
2120
chunkFilename: "[name].bundle.js",
2221
path: path.resolve(__dirname, "./bin"),

scripts/webpack.general.config.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,23 @@ module.exports = (options = {}) => ({
8383
}, {
8484
test: /\.wasm$/,
8585
type: "javascript/auto",
86-
}],
86+
}, {
87+
/**
88+
* Fixes spdlog
89+
*/
90+
test: /spdlog\/index\.js/,
91+
loader: "string-replace-loader",
92+
options: {
93+
multiple: [{
94+
// These will be handled by file-loader. We need the location because
95+
// they are parsed as URIs and will throw errors if not fully formed.
96+
// The !! prefix causes it to ignore other loaders (doesn't work).
97+
search: "const spdlog.*;",
98+
replace: "const spdlog = __non_webpack_require__(global.SPDLOG_LOCATION);",
99+
flags: "g",
100+
}],
101+
},
102+
},],
87103
noParse: /\/test\/|\.test\.jsx?|\.test\.tsx?|tsconfig.+\.json$/,
88104
},
89105
resolve: {

0 commit comments

Comments
 (0)