@@ -22,6 +22,7 @@ import (
22
22
"io"
23
23
"log"
24
24
"os"
25
+ "os/exec"
25
26
"strconv"
26
27
"strings"
27
28
"sync"
@@ -50,8 +51,10 @@ type INOLanguageServer struct {
50
51
51
52
progressHandler * progressProxyHandler
52
53
closing chan bool
54
+ removeTempMutex sync.Mutex
53
55
clangdStarted * sync.Cond
54
56
dataMux sync.RWMutex
57
+ tempDir * paths.Path
55
58
buildPath * paths.Path
56
59
buildSketchRoot * paths.Path
57
60
buildSketchCpp * paths.Path
@@ -144,18 +147,21 @@ func NewINOLanguageServer(stdin io.Reader, stdout io.Writer, config *Config) *IN
144
147
if tmp , err := paths .MkTempDir ("" , "arduino-language-server" ); err != nil {
145
148
log .Fatalf ("Could not create temp folder: %s" , err )
146
149
} else {
147
- ls .buildPath = tmp .Canonical ()
148
- ls .buildSketchRoot = ls .buildPath .Join ("sketch" )
150
+ ls .tempDir = tmp .Canonical ()
149
151
}
150
-
151
- if tmp , err := paths .MkTempDir ("" , "arduino-language-server" ); err != nil {
152
+ ls .buildPath = ls .tempDir .Join ("build" )
153
+ ls .buildSketchRoot = ls .buildPath .Join ("sketch" )
154
+ if err := ls .buildPath .MkdirAll (); err != nil {
155
+ log .Fatalf ("Could not create temp folder: %s" , err )
156
+ }
157
+ ls .fullBuildPath = ls .tempDir .Join ("fullbuild" )
158
+ if err := ls .fullBuildPath .MkdirAll (); err != nil {
152
159
log .Fatalf ("Could not create temp folder: %s" , err )
153
- } else {
154
- ls .fullBuildPath = tmp .Canonical ()
155
160
}
156
161
157
162
logger .Logf ("Initial board configuration: %s" , ls .config .Fqbn )
158
163
logger .Logf ("%s" , globals .VersionInfo .String ())
164
+ logger .Logf ("Language server temp directory: %s" , ls .tempDir )
159
165
logger .Logf ("Language server build path: %s" , ls .buildPath )
160
166
logger .Logf ("Language server build sketch root: %s" , ls .buildSketchRoot )
161
167
logger .Logf ("Language server FULL build path: %s" , ls .fullBuildPath )
@@ -387,6 +393,7 @@ func (ls *INOLanguageServer) shutdownReqFromIDE(ctx context.Context, logger json
387
393
close (done )
388
394
}()
389
395
_ , _ = ls .Clangd .conn .Shutdown (context .Background ())
396
+ ls .removeTemporaryFiles (logger )
390
397
<- done
391
398
return nil
392
399
}
@@ -1371,6 +1378,38 @@ func (ls *INOLanguageServer) setTraceNotifFromIDE(logger jsonrpc.FunctionLogger,
1371
1378
ls .Clangd .conn .SetTrace (params )
1372
1379
}
1373
1380
1381
+ func (ls * INOLanguageServer ) removeTemporaryFiles (logger jsonrpc.FunctionLogger ) {
1382
+ ls .removeTempMutex .Lock ()
1383
+ defer ls .removeTempMutex .Unlock ()
1384
+
1385
+ if ls .tempDir == nil {
1386
+ // Nothing to remove
1387
+ return
1388
+ }
1389
+
1390
+ // Start a detached process to remove the temp files
1391
+ cwd , err := os .Getwd ()
1392
+ if err != nil {
1393
+ logger .Logf ("Error getting current working directory: %s" , err )
1394
+ return
1395
+ }
1396
+ cmd := exec .Command (os .Args [0 ], "remove-temp-files" , ls .tempDir .String ())
1397
+ cmd .Dir = cwd
1398
+ if err := cmd .Start (); err != nil {
1399
+ logger .Logf ("Error starting remove-temp-files process: %s" , err )
1400
+ return
1401
+ }
1402
+
1403
+ // The process is now started, we can reset the paths
1404
+ ls .buildPath , ls .fullBuildPath , ls .buildSketchRoot , ls .tempDir = nil , nil , nil , nil
1405
+
1406
+ // Detach the process so it can continue running even if the parent process exits
1407
+ if err := cmd .Process .Release (); err != nil {
1408
+ logger .Logf ("Error detaching remove-temp-files process: %s" , err )
1409
+ return
1410
+ }
1411
+ }
1412
+
1374
1413
// Close closes all the json-rpc connections and clean-up temp folders.
1375
1414
func (ls * INOLanguageServer ) Close () {
1376
1415
if ls .Clangd != nil {
@@ -1381,14 +1420,6 @@ func (ls *INOLanguageServer) Close() {
1381
1420
close (ls .closing )
1382
1421
ls .closing = nil
1383
1422
}
1384
- if ls .buildPath != nil {
1385
- ls .buildPath .RemoveAll ()
1386
- ls .buildPath = nil
1387
- }
1388
- if ls .fullBuildPath != nil {
1389
- ls .fullBuildPath .RemoveAll ()
1390
- ls .fullBuildPath = nil
1391
- }
1392
1423
}
1393
1424
1394
1425
// CloseNotify returns a channel that is closed when the InoHandler is closed
0 commit comments