@@ -4,8 +4,17 @@ import * as childProcess from "child_process";
4
4
import * as p from "vscode-languageserver-protocol" ;
5
5
import * as path from "path" ;
6
6
import * as t from "vscode-languageserver-types" ;
7
- import * as tmp from "tmp" ;
8
7
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
+ } ;
9
18
10
19
// TODO: races here?
11
20
// TODO: this doesn't handle file:/// scheme
@@ -39,15 +48,15 @@ export let formatUsingValidBscPath = (
39
48
bscPath : p . DocumentUri ,
40
49
isInterface : boolean
41
50
) : execResult => {
42
- // library cleans up after itself. No need to manually remove temp file
43
- let tmpobj = tmp . fileSync ( ) ;
44
51
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
+ } ) ;
47
56
try {
48
57
let result = childProcess . execFileSync (
49
58
bscPath ,
50
- [ "-color" , "never" , "-format" , fileToFormat ] ,
59
+ [ "-color" , "never" , "-format" , formatTempFileFullPath ] ,
51
60
{ stdio : "pipe" }
52
61
) ;
53
62
return {
@@ -59,6 +68,9 @@ export let formatUsingValidBscPath = (
59
68
kind : "error" ,
60
69
error : e . message ,
61
70
} ;
71
+ } finally {
72
+ // async close is fine. We don't use this file name again
73
+ fs . unlink ( formatTempFileFullPath , ( ) => null ) ;
62
74
}
63
75
} ;
64
76
0 commit comments