Skip to content

Commit 72884b6

Browse files
committed
Use a custom temp folder to make all LS business...
1 parent 248f8fd commit 72884b6

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

Diff for: handler/builder.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (handler *InoHandler) rebuildEnvironmentLoop() {
8585
}
8686
}
8787

88-
func (handler *InoHandler) generateBuildEnvironment() (*paths.Path, error) {
88+
func (handler *InoHandler) generateBuildEnvironment(buildPath *paths.Path) error {
8989
sketchDir := handler.sketchRoot
9090
fqbn := handler.config.SelectedBoard.Fqbn
9191

@@ -97,15 +97,15 @@ func (handler *InoHandler) generateBuildEnvironment() (*paths.Path, error) {
9797
for uri, trackedFile := range handler.docs {
9898
rel, err := paths.New(uri).RelFrom(handler.sketchRoot)
9999
if err != nil {
100-
return nil, errors.WithMessage(err, "dumping tracked files")
100+
return errors.WithMessage(err, "dumping tracked files")
101101
}
102102
data.Overrides[rel.String()] = trackedFile.Text
103103
}
104104
var overridesJSON *paths.Path
105105
if jsonBytes, err := json.MarshalIndent(data, "", " "); err != nil {
106-
return nil, errors.WithMessage(err, "dumping tracked files")
106+
return errors.WithMessage(err, "dumping tracked files")
107107
} else if tmpFile, err := paths.WriteToTempFile(jsonBytes, nil, ""); err != nil {
108-
return nil, errors.WithMessage(err, "dumping tracked files")
108+
return errors.WithMessage(err, "dumping tracked files")
109109
} else {
110110
overridesJSON = tmpFile
111111
defer tmpFile.Remove()
@@ -118,21 +118,23 @@ func (handler *InoHandler) generateBuildEnvironment() (*paths.Path, error) {
118118
"--only-compilation-database",
119119
"--clean",
120120
"--source-override", overridesJSON.String(),
121+
"--build-path", buildPath.String(),
121122
"--format", "json",
122123
sketchDir.String(),
123124
}
124125
cmd, err := executils.NewProcess(args...)
125126
if err != nil {
126-
return nil, errors.Errorf("running %s: %s", strings.Join(args, " "), err)
127+
return errors.Errorf("running %s: %s", strings.Join(args, " "), err)
127128
}
128129
cmdOutput := &bytes.Buffer{}
129130
cmd.RedirectStdoutTo(cmdOutput)
130131
cmd.SetDirFromPath(sketchDir)
131132
log.Println("running: ", strings.Join(args, " "))
132133
if err := cmd.Run(); err != nil {
133-
return nil, errors.Errorf("running %s: %s", strings.Join(args, " "), err)
134+
return errors.Errorf("running %s: %s", strings.Join(args, " "), err)
134135
}
135136

137+
// Currently those values are not used, keeping here for future improvements
136138
type cmdBuilderRes struct {
137139
BuildPath *paths.Path `json:"build_path"`
138140
UsedLibraries []*libraries.Library
@@ -145,9 +147,9 @@ func (handler *InoHandler) generateBuildEnvironment() (*paths.Path, error) {
145147
}
146148
var res cmdRes
147149
if err := json.Unmarshal(cmdOutput.Bytes(), &res); err != nil {
148-
return nil, errors.Errorf("parsing arduino-cli output: %s", err)
150+
return errors.Errorf("parsing arduino-cli output: %s", err)
149151
}
150-
// Return only the build path
151152
log.Println("arduino-cli output:", cmdOutput)
152-
return res.BuilderResult.BuildPath, nil
153+
154+
return nil
153155
}

Diff for: handler/handler.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,18 @@ func NewInoHandler(stdio io.ReadWriteCloser, board lsp.Board) *InoHandler {
131131
jsonrpc2.OnSend(streams.JSONRPCConnLogOnSend("IDE <-- LS CL:")),
132132
)
133133

134+
if buildPath, err := paths.MkTempDir("", "arduino-language-server"); err != nil {
135+
log.Fatalf("Could not create temp folder: %s", err)
136+
} else {
137+
handler.buildPath = buildPath.Canonical()
138+
handler.buildSketchRoot = buildPath.Join("sketch").Canonical()
139+
}
140+
134141
handler.progressHandler = NewProgressProxy(handler.StdioConn)
135142

136143
if enableLogging {
137144
log.Println("Initial board configuration:", board)
145+
log.Println("Language server build path:", handler.buildPath)
138146
}
139147

140148
go handler.rebuildEnvironmentLoop()
@@ -156,6 +164,14 @@ func (handler *InoHandler) StopClangd() {
156164
handler.ClangdConn = nil
157165
}
158166

167+
// CleanUp performs cleanup of the workspace and temp files create by the language server
168+
func (handler *InoHandler) CleanUp() {
169+
if handler.buildPath != nil {
170+
handler.buildPath.RemoveAll()
171+
handler.buildPath = nil
172+
}
173+
}
174+
159175
// HandleMessageFromIDE handles a message received from the IDE client (via stdio).
160176
func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (interface{}, error) {
161177
defer streams.CatchAndLogPanic()
@@ -507,10 +523,7 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
507523
currCppTextVersion = handler.sketchMapper.CppText.Version
508524
}
509525

510-
if buildPath, err := handler.generateBuildEnvironment(); err == nil {
511-
handler.buildPath = buildPath
512-
handler.buildSketchRoot = buildPath.Join("sketch").Canonical()
513-
} else {
526+
if err := handler.generateBuildEnvironment(handler.buildPath); err != nil {
514527
return err
515528
}
516529
handler.buildSketchCpp = handler.buildSketchRoot.Join(handler.sketchName + ".ino.cpp")

Diff for: main.go

+1
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ func main() {
5454

5555
inoHandler := handler.NewInoHandler(stdio, initialBoard)
5656
defer inoHandler.StopClangd()
57+
defer inoHandler.CleanUp()
5758
<-inoHandler.StdioConn.DisconnectNotify()
5859
}

0 commit comments

Comments
 (0)