Skip to content

Commit ca130b9

Browse files
committed
Remove dependency on tmpfile
We can make our own pseudo-unique temp file by just bumping an id
1 parent 073bd7d commit ca130b9

File tree

4 files changed

+59
-80
lines changed

4 files changed

+59
-80
lines changed

package-lock.json

+36-66
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@
9797
"typescript": "^3.9.4"
9898
},
9999
"dependencies": {
100-
"@types/tmp": "^0.2.0",
101-
"chokidar": "^3.4.2",
102-
"tmp": "^0.2.1"
100+
"chokidar": "^3.4.2"
103101
}
104-
}
102+
}

server/src/RescriptEditorSupport.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { RequestMessage } from "vscode-languageserver";
33
import * as utils from "./utils";
44
import * as path from "path";
55
import { exec } from "child_process";
6-
import * as tmp from "tmp";
76
import fs from "fs";
87

98
let binaryPath = path.join(
@@ -62,8 +61,7 @@ export function runCompletionCommand(
6261
if (executable == null) {
6362
onResult(null);
6463
} else {
65-
let tmpobj = tmp.fileSync();
66-
let tmpname = tmpobj.name;
64+
let tmpname = utils.createFileInTempDir();
6765
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });
6866

6967
let command =
@@ -78,7 +76,8 @@ export function runCompletionCommand(
7876
tmpname;
7977

8078
exec(command, { cwd: executable.cwd }, function (_error, stdout, _stderr) {
81-
tmpobj.removeCallback();
79+
// async close is fine. We don't use this file name again
80+
fs.unlink(tmpname, () => null);
8281
let result = JSON.parse(stdout);
8382
if (result && result[0]) {
8483
onResult(result);

server/src/utils.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ import * as childProcess from "child_process";
44
import * as p from "vscode-languageserver-protocol";
55
import * as path from "path";
66
import * as t from "vscode-languageserver-types";
7-
import * as tmp from "tmp";
87
import fs from "fs";
8+
import * as os from "os";
9+
10+
let tempFilePrefix = "rescript_format_file_" + process.pid + "_";
11+
let tempFileId = 0;
12+
13+
export let createFileInTempDir = (extension = "") => {
14+
let tempFileName = tempFilePrefix + tempFileId + extension;
15+
tempFileId = tempFileId + 1;
16+
return path.join(os.tmpdir(), tempFileName);
17+
};
918

1019
// TODO: races here?
1120
// TODO: this doesn't handle file:/// scheme
@@ -39,15 +48,15 @@ export let formatUsingValidBscPath = (
3948
bscPath: p.DocumentUri,
4049
isInterface: boolean
4150
): execResult => {
42-
// library cleans up after itself. No need to manually remove temp file
43-
let tmpobj = tmp.fileSync();
4451
let extension = isInterface ? c.resiExt : c.resExt;
45-
let fileToFormat = tmpobj.name + extension;
46-
fs.writeFileSync(fileToFormat, code, { encoding: "utf-8" });
52+
let formatTempFileFullPath = createFileInTempDir(extension);
53+
fs.writeFileSync(formatTempFileFullPath, code, {
54+
encoding: "utf-8",
55+
});
4756
try {
4857
let result = childProcess.execFileSync(
4958
bscPath,
50-
["-color", "never", "-format", fileToFormat],
59+
["-color", "never", "-format", formatTempFileFullPath],
5160
{ stdio: "pipe" }
5261
);
5362
return {
@@ -59,6 +68,9 @@ export let formatUsingValidBscPath = (
5968
kind: "error",
6069
error: e.message,
6170
};
71+
} finally {
72+
// async close is fine. We don't use this file name again
73+
fs.unlink(formatTempFileFullPath, () => null);
6274
}
6375
};
6476

0 commit comments

Comments
 (0)