Skip to content

Commit d1dc096

Browse files
committed
Properly fix windows' binary invocations
See the comment in diff
1 parent b0bff0a commit d1dc096

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

server/src/server.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,12 @@ process.on("message", (msg: m.Message) => {
428428
process.send!(fakeSuccessResponse);
429429
process.send!(response);
430430
} else {
431-
let resolvedBscPath = path.join(bscExeDir, c.bscExePartialPath);
431+
let resolvedBscExePath = path.join(bscExeDir, c.bscExePartialPath);
432432
// code will always be defined here, even though technically it can be undefined
433433
let code = getOpenedFileContent(params.textDocument.uri);
434-
let formattedResult = utils.formatUsingValidBscPath(
434+
let formattedResult = utils.formatUsingValidBscExePath(
435435
code,
436-
resolvedBscPath,
436+
resolvedBscExePath,
437437
extension === c.resiExt
438438
);
439439
if (formattedResult.kind === "success") {
@@ -489,14 +489,14 @@ process.on("message", (msg: m.Message) => {
489489
) {
490490
let msg_ = msg.result as clientSentBuildAction;
491491
let projectRootPath = msg_.projectRootPath;
492-
let bsbPath = path.join(projectRootPath, c.bsbNodePartialPath);
492+
let bsbNodePath = path.join(projectRootPath, c.bsbNodePartialPath);
493493
// TODO: sometime stale .bsb.lock dangling
494494
// TODO: close watcher when lang-server shuts down. However, by Node's
495495
// default, these subprocesses are automatically killed when this
496496
// language-server process exits
497-
if (fs.existsSync(bsbPath)) {
498-
let bsbProcess = utils.runBsbWatcherUsingValidBsbPath(
499-
bsbPath,
497+
if (fs.existsSync(bsbNodePath)) {
498+
let bsbProcess = utils.runBsbWatcherUsingValidBsbNodePath(
499+
bsbNodePath,
500500
projectRootPath
501501
);
502502
let root = projectsFiles.get(projectRootPath)!;

server/src/utils.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ type execResult =
6969
kind: "error";
7070
error: string;
7171
};
72-
export let formatUsingValidBscPath = (
72+
export let formatUsingValidBscExePath = (
7373
code: string,
74-
bscPath: p.DocumentUri,
74+
bscExePath: p.DocumentUri,
7575
isInterface: boolean
7676
): execResult => {
7777
let extension = isInterface ? c.resiExt : c.resExt;
@@ -81,7 +81,7 @@ export let formatUsingValidBscPath = (
8181
});
8282
try {
8383
let result = childProcess.execFileSync(
84-
bscPath,
84+
bscExePath,
8585
["-color", "never", "-format", formatTempFileFullPath],
8686
);
8787
return {
@@ -99,16 +99,28 @@ export let formatUsingValidBscPath = (
9999
}
100100
};
101101

102-
export let runBsbWatcherUsingValidBsbPath = (
103-
bsbPath: p.DocumentUri,
102+
export let runBsbWatcherUsingValidBsbNodePath = (
103+
bsbNodePath: p.DocumentUri,
104104
projectRootPath: p.DocumentUri
105105
) => {
106106
if (process.platform === "win32") {
107-
return childProcess.exec(`${bsbPath}.cmd -w`, {
107+
/*
108+
- a node.js script in node_modules/.bin on windows is wrapped in a
109+
batch script wrapper (there's also a regular binary of the same name on
110+
windows, but that one's a shell script wrapper for cygwin). More info:
111+
https://github.com/npm/cmd-shim/blob/c5118da34126e6639361fe9706a5ff07e726ed45/index.js#L1
112+
- a batch script adds the suffix .cmd to the script
113+
- you can't call batch scripts through the regular `execFile`:
114+
https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows
115+
- So you have to use `exec` instead, and make sure you quote the path
116+
(since the path might have spaces), which `execFile` would have done
117+
for you under the hood
118+
*/
119+
return childProcess.exec(`"${bsbNodePath}".cmd -w`, {
108120
cwd: projectRootPath,
109121
});
110122
} else {
111-
return childProcess.execFile(bsbPath, ["-w"], {
123+
return childProcess.execFile(bsbNodePath, ["-w"], {
112124
cwd: projectRootPath,
113125
});
114126
}

0 commit comments

Comments
 (0)