Skip to content

Commit 2d826f4

Browse files
committed
Show error message when preprocessing fails
1 parent c32ed19 commit 2d826f4

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.12
44

55
require (
66
github.com/gorilla/websocket v1.4.0 // indirect
7+
github.com/pkg/errors v0.8.1
78
github.com/sourcegraph/go-lsp v0.0.0-20181119182933-0c7d621186c1
89
github.com/sourcegraph/jsonrpc2 v0.0.0-20190106185902-35a74f039c6a
910
)

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
22
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
3+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
4+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
35
github.com/sourcegraph/go-lsp v0.0.0-20181119182933-0c7d621186c1 h1:O1d7nVzpGmP5pGAZBSlp9TSpjNwwI0xThxhPd9oVJuU=
46
github.com/sourcegraph/go-lsp v0.0.0-20181119182933-0c7d621186c1/go.mod h1:tpps84QRlOVVLYk5QpKYX8Tr289D1v/UTWDLqeguiqM=
57
github.com/sourcegraph/jsonrpc2 v0.0.0-20190106185902-35a74f039c6a h1:jTZwOlrDhmk4Ez2vhWh7kA0eKUahp1lCO2uyM4fi/Qk=

Diff for: handler/builder.go

+35-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ import (
99
"os/exec"
1010
"path/filepath"
1111
"strings"
12+
13+
"github.com/pkg/errors"
1214
)
1315

1416
func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []byte, err error) {
1517
rawTempDir, err := ioutil.TempDir("", "ino2cpp-")
1618
if err != nil {
19+
err = errors.Wrap(err, "Error while creating temporary directory.")
1720
return
1821
}
1922
tempDir, err := filepath.EvalSymlinks(rawTempDir)
2023
if err != nil {
24+
err = errors.Wrap(err, "Error while resolving symbolic links of temporary directory.")
2125
return
2226
}
2327

@@ -28,6 +32,7 @@ func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []b
2832
inoPath := filepath.Join(tempDir, name)
2933
err = ioutil.WriteFile(inoPath, inoCode, 0600)
3034
if err != nil {
35+
err = errors.Wrap(err, "Error while writing source file to temporary directory.")
3136
return
3237
}
3338
if enableLogging {
@@ -47,14 +52,16 @@ func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []b
4752
preprocessCmd := exec.Command(globalCliPath, "compile", "--fqbn", fqbn, "--preprocess", inoPath)
4853
cppCode, err = preprocessCmd.Output()
4954
if err != nil {
50-
logCommandErr(globalCliPath, cppCode, err)
55+
err = logCommandErr(globalCliPath, cppCode, err, errMsgFilter(tempDir))
5156
return
5257
}
5358

5459
// Write target file to temp dir
5560
cppPath = filepath.Join(tempDir, name+".cpp")
5661
err = ioutil.WriteFile(cppPath, cppCode, 0600)
57-
if err == nil && enableLogging {
62+
if err != nil {
63+
err = errors.Wrap(err, "Error while writing target file to temporary directory.")
64+
} else if enableLogging {
5865
log.Println("Target file written to", cppPath)
5966
}
6067
return
@@ -65,37 +72,41 @@ func updateCpp(inoCode []byte, fqbn, cppPath string) (cppCode []byte, err error)
6572
inoPath := strings.TrimSuffix(cppPath, ".cpp")
6673
err = ioutil.WriteFile(inoPath, inoCode, 0600)
6774
if err != nil {
75+
err = errors.Wrap(err, "Error while writing source file to temporary directory.")
6876
return
6977
}
7078

7179
// Generate target file
7280
preprocessCmd := exec.Command(globalCliPath, "compile", "--fqbn", fqbn, "--preprocess", inoPath)
7381
cppCode, err = preprocessCmd.Output()
7482
if err != nil {
75-
logCommandErr(globalCliPath, cppCode, err)
83+
err = logCommandErr(globalCliPath, cppCode, err, errMsgFilter(filepath.Dir(inoPath)))
7684
return
7785
}
7886

7987
// Write target file to temp dir
8088
err = ioutil.WriteFile(cppPath, cppCode, 0600)
89+
if err != nil {
90+
err = errors.Wrap(err, "Error while writing target file to temporary directory.")
91+
}
8192
return
8293
}
8394

8495
func generateCompileFlags(tempDir, inoPath, fqbn string) (string, error) {
8596
propertiesCmd := exec.Command(globalCliPath, "compile", "--fqbn", fqbn, "--show-properties", inoPath)
8697
output, err := propertiesCmd.Output()
8798
if err != nil {
88-
logCommandErr(globalCliPath, output, err)
99+
err = logCommandErr(globalCliPath, output, err, errMsgFilter(tempDir))
89100
return "", err
90101
}
91102
properties, err := readProperties(bytes.NewReader(output))
92103
if err != nil {
93-
return "", err
104+
return "", errors.Wrap(err, "Error while reading build properties.")
94105
}
95106
flagsPath := filepath.Join(tempDir, "compile_flags.txt")
96107
outFile, err := os.OpenFile(flagsPath, os.O_WRONLY|os.O_CREATE, 0600)
97108
if err != nil {
98-
return flagsPath, err
109+
return flagsPath, errors.Wrap(err, "Error while creating output file for compile flags.")
99110
}
100111
defer outFile.Close()
101112
writer := bufio.NewWriter(outFile)
@@ -125,15 +136,30 @@ func generateCompileFlags(tempDir, inoPath, fqbn string) (string, error) {
125136
return flagsPath, nil
126137
}
127138

128-
func logCommandErr(command string, stdout []byte, err error) {
139+
func logCommandErr(command string, stdout []byte, err error, filter func(string) string) error {
140+
message := ""
129141
log.Println("Command error:", command, err)
130142
if len(stdout) > 0 {
131-
log.Println("------------------------------BEGIN STDOUT\n", string(stdout), "\n------------------------------END STDOUT")
143+
stdoutStr := string(stdout)
144+
log.Println("------------------------------BEGIN STDOUT\n", stdoutStr, "\n------------------------------END STDOUT")
145+
message += filter(stdoutStr)
132146
}
133147
if exitErr, ok := err.(*exec.ExitError); ok {
134148
stderr := exitErr.Stderr
135149
if len(stderr) > 0 {
136-
log.Println("------------------------------BEGIN STDERR\n", string(stderr), "\n------------------------------END STDERR")
150+
stderrStr := string(stderr)
151+
log.Println("------------------------------BEGIN STDERR\n", stderrStr, "\n------------------------------END STDERR")
152+
message += filter(stderrStr)
137153
}
138154
}
155+
return errors.Wrap(err, message)
156+
}
157+
158+
func errMsgFilter(tempDir string) func(string) string {
159+
if !strings.HasSuffix(tempDir, string(filepath.Separator)) {
160+
tempDir += string(filepath.Separator)
161+
}
162+
return func(s string) string {
163+
return strings.ReplaceAll(s, tempDir, "")
164+
}
139165
}

Diff for: handler/handler.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type FileData struct {
5656

5757
// FromStdio handles a message received from the client (via stdio).
5858
func (handler *InoHandler) FromStdio(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (interface{}, error) {
59-
params, uri, err := handler.transformClangdParams(req.Method, req.Params)
59+
params, uri, err := handler.transformClangdParams(ctx, req.Method, req.Params)
6060
if err != nil {
6161
log.Println("From stdio: Method:", req.Method, "Error:", err)
6262
return nil, err
@@ -80,7 +80,7 @@ func (handler *InoHandler) FromStdio(ctx context.Context, conn *jsonrpc2.Conn, r
8080
return result, err
8181
}
8282

83-
func (handler *InoHandler) transformClangdParams(method string, raw *json.RawMessage) (params interface{}, uri lsp.DocumentURI, err error) {
83+
func (handler *InoHandler) transformClangdParams(ctx context.Context, method string, raw *json.RawMessage) (params interface{}, uri lsp.DocumentURI, err error) {
8484
params, err = readParams(method, raw)
8585
if err != nil {
8686
return
@@ -92,7 +92,7 @@ func (handler *InoHandler) transformClangdParams(method string, raw *json.RawMes
9292
case "textDocument/didOpen":
9393
p := params.(*lsp.DidOpenTextDocumentParams)
9494
uri = p.TextDocument.URI
95-
err = handler.ino2cppTextDocumentItem(&p.TextDocument)
95+
err = handler.ino2cppTextDocumentItem(ctx, &p.TextDocument)
9696
case "textDocument/didChange":
9797
p := params.(*lsp.DidChangeTextDocumentParams)
9898
uri = p.TextDocument.URI
@@ -156,12 +156,14 @@ func (handler *InoHandler) transformClangdParams(method string, raw *json.RawMes
156156
return
157157
}
158158

159-
func (handler *InoHandler) createFileData(sourceURI lsp.DocumentURI, sourceText string) (*FileData, []byte, error) {
159+
func (handler *InoHandler) createFileData(ctx context.Context, sourceURI lsp.DocumentURI, sourceText string) (*FileData, []byte, error) {
160160
sourcePath := uriToPath(sourceURI)
161161
// TODO get board from sketch config
162162
fqbn := "arduino:avr:uno"
163163
targetPath, targetBytes, err := generateCpp([]byte(sourceText), filepath.Base(sourcePath), fqbn)
164164
if err != nil {
165+
message := "Could not start editor support:\n" + err.Error()
166+
go handler.showMessage(ctx, lsp.MTError, message)
165167
return nil, nil, err
166168
}
167169

@@ -230,9 +232,9 @@ func (handler *InoHandler) ino2cppTextDocumentIdentifier(doc *lsp.TextDocumentId
230232
return nil
231233
}
232234

233-
func (handler *InoHandler) ino2cppTextDocumentItem(doc *lsp.TextDocumentItem) error {
235+
func (handler *InoHandler) ino2cppTextDocumentItem(ctx context.Context, doc *lsp.TextDocumentItem) error {
234236
if strings.HasSuffix(string(doc.URI), ".ino") {
235-
data, targetBytes, err := handler.createFileData(doc.URI, doc.Text)
237+
data, targetBytes, err := handler.createFileData(ctx, doc.URI, doc.Text)
236238
if err != nil {
237239
return err
238240
}
@@ -517,6 +519,14 @@ func (handler *InoHandler) cpp2inoPublishDiagnosticsParams(params *lsp.PublishDi
517519
return nil
518520
}
519521

522+
func (handler *InoHandler) showMessage(ctx context.Context, msgType lsp.MessageType, message string) {
523+
params := lsp.ShowMessageParams{
524+
Type: msgType,
525+
Message: strings.ReplaceAll(message, "\n", "<br>"),
526+
}
527+
handler.StdioConn.Notify(ctx, "window/showMessage", &params)
528+
}
529+
520530
func uriToPath(uri lsp.DocumentURI) string {
521531
url, err := url.Parse(string(uri))
522532
if err != nil {

0 commit comments

Comments
 (0)