@@ -4,8 +4,17 @@ import * as childProcess from "child_process";
44import * as p from "vscode-languageserver-protocol" ;
55import * as path from "path" ;
66import * as t from "vscode-languageserver-types" ;
7- import * as tmp from "tmp" ;
87import 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