Skip to content

Commit 022adfc

Browse files
committed
add tsp-client --debug and filter diagnostic errors
1 parent bc73667 commit 022adfc

File tree

3 files changed

+105
-17
lines changed

3 files changed

+105
-17
lines changed

eng/scripts/automation_init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ cat > $outputFile << EOF
5959
EOF
6060

6161
echo Install tsp-client
62-
sudo npm install -g @azure-tools/typespec-client-generator-cli@latest 2>&1
62+
sudo npm install -g @azure-tools/typespec-client-generator-cli@v0.10.0 2>&1

eng/tools/generator/cmd/v2/automation/automationCmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func (ctx *automationContext) generate(input *pipeline.GenerateInput) (*pipeline
137137
NamespaceName: module[1],
138138
SkipGenerateExample: true,
139139
GoVersion: ctx.goVersion,
140+
TspClientOptions: []string{"--debug"},
140141
})
141142
if err != nil {
142143
errorBuilder.add(err)

eng/tools/generator/cmd/v2/common/cmdProcessor.go

Lines changed: 103 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"io"
1010
"log"
11-
"os"
1211
"os/exec"
1312
"path/filepath"
1413
"regexp"
@@ -44,7 +43,7 @@ func ExecuteGoGenerate(path string) error {
4443
return err
4544
}
4645

47-
err = cmd.Wait()
46+
cmdWaitErr := cmd.Wait()
4847

4948
fmt.Println(stdoutBuffer.String())
5049
if stdoutBuffer.Len() > 0 {
@@ -58,7 +57,7 @@ func ExecuteGoGenerate(path string) error {
5857
}
5958
}
6059

61-
if err != nil || stderrBuffer.Len() > 0 {
60+
if cmdWaitErr != nil || stderrBuffer.Len() > 0 {
6261
if stderrBuffer.Len() > 0 {
6362
fmt.Println(stderrBuffer.String())
6463
// filter go downloading log
@@ -199,9 +198,13 @@ func ExecuteGoFmt(dir string, args ...string) error {
199198
func ExecuteTspClient(path string, args ...string) error {
200199
cmd := exec.Command("tsp-client", args...)
201200
cmd.Dir = path
202-
cmd.Stdout = os.Stdout
203201

204-
stderr, err := cmd.StderrPipe()
202+
stdoutPipe, err := cmd.StdoutPipe()
203+
if err != nil {
204+
return err
205+
}
206+
207+
stderrPipe, err := cmd.StderrPipe()
205208
if err != nil {
206209
return err
207210
}
@@ -210,29 +213,56 @@ func ExecuteTspClient(path string, args ...string) error {
210213
return err
211214
}
212215

213-
var buf bytes.Buffer
214-
if _, err = io.Copy(&buf, stderr); err != nil {
216+
var stdoutBuffer bytes.Buffer
217+
if _, err = io.Copy(&stdoutBuffer, stdoutPipe); err != nil {
215218
return err
216219
}
217220

218-
if err := cmd.Wait(); err != nil || buf.Len() > 0 {
219-
if buf.Len() > 0 {
220-
log.Println(buf.String())
221+
var stderrBuffer bytes.Buffer
222+
if _, err = io.Copy(&stderrBuffer, stderrPipe); err != nil {
223+
return err
224+
}
225+
226+
cmdWaitErr := cmd.Wait()
227+
fmt.Println(stdoutBuffer.String())
228+
229+
if cmdWaitErr != nil || stderrBuffer.Len() > 0 {
230+
if stderrBuffer.Len() > 0 {
231+
log.Println(stderrBuffer.String())
221232

222233
// filter npm notice log
223-
lines := strings.Split(buf.String(), "\n")
224-
newErrInfo := make([]string, 0, len(lines))
225-
for _, line := range lines {
234+
newErrMsgs := make([]string, 0)
235+
for _, line := range strings.Split(stderrBuffer.String(), "\n") {
226236
if len(strings.TrimSpace(line)) == 0 {
227237
continue
228238
}
229239
if !strings.Contains(line, "npm notice") {
230-
newErrInfo = append(newErrInfo, line)
240+
newErrMsgs = append(newErrMsgs, line)
241+
}
242+
}
243+
244+
// filter diagnostic errors
245+
if len(newErrMsgs) == 1 &&
246+
newErrMsgs[0] == "Diagnostics were reported during compilation. Use the `--debug` flag to see the diagnostic output." {
247+
newErrMsgs = FilterErrorDiagnostics(strings.Split(stdoutBuffer.String(), "\n"))
248+
249+
temp := make([]string, 0)
250+
for _, line := range newErrMsgs {
251+
line := strings.TrimSpace(line)
252+
if line == "" {
253+
continue
254+
}
255+
if strings.Contains(line, "Cleaning up temp directory") ||
256+
strings.Contains(line, "Skipping cleanup of temp directory:") {
257+
continue
258+
}
259+
temp = append(temp, line)
231260
}
261+
newErrMsgs = temp
232262
}
233263

234-
if len(newErrInfo) > 0 {
235-
return fmt.Errorf("failed to execute `tsp-client %s`\n%s", strings.Join(args, " "), strings.Join(newErrInfo, "\n"))
264+
if len(newErrMsgs) > 0 {
265+
return fmt.Errorf("failed to execute `tsp-client %s`\n%s", strings.Join(args, " "), strings.Join(newErrMsgs, "\n"))
236266
}
237267

238268
return nil
@@ -265,3 +295,60 @@ func ExecuteTypeSpecGenerate(ctx *GenerateContext, emitOptions string, tspClient
265295

266296
return ExecuteTspClient(ctx.SDKPath, args...)
267297
}
298+
299+
type diagnostic struct {
300+
// error | warning
301+
kind string
302+
start, end int
303+
}
304+
305+
// get all warning and error diagnostics
306+
func diagnostics(lines []string) []diagnostic {
307+
var kind string
308+
start := -1
309+
diagnostics := make([]diagnostic, 0)
310+
311+
// get all warning and error diagnostics
312+
for i := 0; i < len(lines); i++ {
313+
line := strings.TrimSpace(lines[i])
314+
if strings.Contains(line, "warning ") {
315+
if start != -1 {
316+
diagnostics = append(diagnostics, diagnostic{kind: kind, start: start, end: i - 1})
317+
start = i
318+
} else {
319+
start = i
320+
}
321+
kind = "warning"
322+
} else if strings.Contains(line, "error ") {
323+
if start != -1 {
324+
diagnostics = append(diagnostics, diagnostic{kind: kind, start: start, end: i - 1})
325+
start = i
326+
} else {
327+
start = i
328+
}
329+
kind = "error"
330+
}
331+
332+
if i == len(lines)-1 {
333+
diagnostics = append(diagnostics, diagnostic{kind: kind, start: start, end: i})
334+
}
335+
}
336+
337+
return diagnostics
338+
}
339+
340+
func FilterErrorDiagnostics(lines []string) []string {
341+
diags := diagnostics(lines)
342+
if len(diags) == 0 {
343+
return nil
344+
}
345+
346+
result := make([]string, 0)
347+
for _, diag := range diags {
348+
if diag.kind == "error" {
349+
result = append(result, lines[diag.start:diag.end+1]...)
350+
}
351+
}
352+
353+
return result
354+
}

0 commit comments

Comments
 (0)