Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(generator tool): filter tsp-client diagnostics log #23292

Merged
merged 7 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/scripts/automation_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ cat > $outputFile << EOF
EOF

echo Install tsp-client
sudo npm install -g @azure-tools/typespec-client-generator-cli@latest 2>&1
sudo npm install -g @azure-tools/typespec-client-generator-cli@v0.10.0 2>&1
1 change: 1 addition & 0 deletions eng/tools/generator/cmd/v2/automation/automationCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (ctx *automationContext) generate(input *pipeline.GenerateInput) (*pipeline
NamespaceName: module[1],
SkipGenerateExample: true,
GoVersion: ctx.goVersion,
TspClientOptions: []string{"--debug"},
})
if err != nil {
errorBuilder.add(err)
Expand Down
119 changes: 103 additions & 16 deletions eng/tools/generator/cmd/v2/common/cmdProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -44,7 +43,7 @@ func ExecuteGoGenerate(path string) error {
return err
}

err = cmd.Wait()
cmdWaitErr := cmd.Wait()

fmt.Println(stdoutBuffer.String())
if stdoutBuffer.Len() > 0 {
Expand All @@ -58,7 +57,7 @@ func ExecuteGoGenerate(path string) error {
}
}

if err != nil || stderrBuffer.Len() > 0 {
if cmdWaitErr != nil || stderrBuffer.Len() > 0 {
if stderrBuffer.Len() > 0 {
fmt.Println(stderrBuffer.String())
// filter go downloading log
Expand Down Expand Up @@ -199,9 +198,13 @@ func ExecuteGoFmt(dir string, args ...string) error {
func ExecuteTspClient(path string, args ...string) error {
cmd := exec.Command("tsp-client", args...)
cmd.Dir = path
cmd.Stdout = os.Stdout

stderr, err := cmd.StderrPipe()
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return err
}

stderrPipe, err := cmd.StderrPipe()
if err != nil {
return err
}
Expand All @@ -210,29 +213,56 @@ func ExecuteTspClient(path string, args ...string) error {
return err
}

var buf bytes.Buffer
if _, err = io.Copy(&buf, stderr); err != nil {
var stdoutBuffer bytes.Buffer
if _, err = io.Copy(&stdoutBuffer, stdoutPipe); err != nil {
return err
}

if err := cmd.Wait(); err != nil || buf.Len() > 0 {
if buf.Len() > 0 {
log.Println(buf.String())
var stderrBuffer bytes.Buffer
if _, err = io.Copy(&stderrBuffer, stderrPipe); err != nil {
return err
}

cmdWaitErr := cmd.Wait()
fmt.Println(stdoutBuffer.String())

if cmdWaitErr != nil || stderrBuffer.Len() > 0 {
if stderrBuffer.Len() > 0 {
log.Println(stderrBuffer.String())

// filter npm notice log
lines := strings.Split(buf.String(), "\n")
newErrInfo := make([]string, 0, len(lines))
for _, line := range lines {
newErrMsgs := make([]string, 0)
for _, line := range strings.Split(stderrBuffer.String(), "\n") {
if len(strings.TrimSpace(line)) == 0 {
continue
}
if !strings.Contains(line, "npm notice") {
newErrInfo = append(newErrInfo, line)
newErrMsgs = append(newErrMsgs, line)
}
}

// filter diagnostic errors
if len(newErrMsgs) == 1 &&
newErrMsgs[0] == "Diagnostics were reported during compilation. Use the `--debug` flag to see the diagnostic output." {
newErrMsgs = FilterErrorDiagnostics(strings.Split(stdoutBuffer.String(), "\n"))

temp := make([]string, 0)
for _, line := range newErrMsgs {
line := strings.TrimSpace(line)
if line == "" {
continue
}
if strings.Contains(line, "Cleaning up temp directory") ||
strings.Contains(line, "Skipping cleanup of temp directory:") {
continue
}
temp = append(temp, line)
}
newErrMsgs = temp
}

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

return nil
Expand Down Expand Up @@ -265,3 +295,60 @@ func ExecuteTypeSpecGenerate(ctx *GenerateContext, emitOptions string, tspClient

return ExecuteTspClient(ctx.SDKPath, args...)
}

type diagnostic struct {
// error | warning
kind string
start, end int
}

// get all warning and error diagnostics
func diagnostics(lines []string) []diagnostic {
var kind string
start := -1
diagnostics := make([]diagnostic, 0)

// get all warning and error diagnostics
for i := 0; i < len(lines); i++ {
line := strings.TrimSpace(lines[i])
if strings.Contains(line, "warning ") {
if start != -1 {
diagnostics = append(diagnostics, diagnostic{kind: kind, start: start, end: i - 1})
start = i
} else {
start = i
}
kind = "warning"
} else if strings.Contains(line, "error ") {
if start != -1 {
diagnostics = append(diagnostics, diagnostic{kind: kind, start: start, end: i - 1})
start = i
} else {
start = i
}
kind = "error"
}

if i == len(lines)-1 {
diagnostics = append(diagnostics, diagnostic{kind: kind, start: start, end: i})
}
}

return diagnostics
}

func FilterErrorDiagnostics(lines []string) []string {
diags := diagnostics(lines)
if len(diags) == 0 {
return nil
}

result := make([]string, 0)
for _, diag := range diags {
if diag.kind == "error" {
result = append(result, lines[diag.start:diag.end+1]...)
}
}

return result
}