Skip to content

Commit

Permalink
fix(generator tool): filter tsp-client diagnostics log (#23292)
Browse files Browse the repository at this point in the history
* fix(generator tool): tcgc warning incompatible-library

* add tsp-client --debug and filter diagnostic errors

* rename

* fix

* fix

* fix

* fix
  • Loading branch information
Alancere authored Aug 7, 2024
1 parent 023c88f commit 217edee
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 47 deletions.
46 changes: 23 additions & 23 deletions eng/emitter-package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions eng/emitter-package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"main": "dist/src/index.js",
"dependencies": {
"@azure-tools/typespec-go": "0.2.0"
"@azure-tools/typespec-go": "0.3.0"
},
"devDependencies": {
"@azure-tools/typespec-autorest": "0.44.0",
"@azure-tools/typespec-autorest": "0.44.1",
"@azure-tools/typespec-azure-core": "0.44.0",
"@azure-tools/typespec-azure-resource-manager": "0.44.0",
"@azure-tools/typespec-azure-rulesets": "0.44.0",
"@azure-tools/typespec-client-generator-core": "0.44.1",
"@typespec/compiler": "0.58.0",
"@azure-tools/typespec-client-generator-core": "0.44.3",
"@typespec/compiler": "0.58.1",
"@typespec/http": "0.58.0",
"@typespec/openapi": "0.58.0",
"@typespec/rest": "0.58.0",
Expand Down
2 changes: 1 addition & 1 deletion eng/pipelines/mgmt-auto-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extends:
- task: Npm@1
inputs:
command: 'custom'
customCommand: 'install -g @azure-tools/typespec-client-generator-cli@latest'
customCommand: 'install -g @azure-tools/typespec-client-generator-cli@v0.10.0'

- task: GoTool@0
inputs:
Expand Down
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
127 changes: 109 additions & 18 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,8 +57,9 @@ 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
// https://github.com/golang/go/blob/1f0c044d60211e435dc58844127544dd3ecb6a41/src/cmd/go/internal/modfetch/fetch.go#L201
lines := strings.Split(stderrBuffer.String(), "\n")
Expand All @@ -75,9 +75,7 @@ func ExecuteGoGenerate(path string) error {
}

if len(newLines) > 0 {
newErrMsg := strings.Join(newLines, "\n")
fmt.Println(newErrMsg)
return fmt.Errorf("failed to execute `go generate`:\n%s", newErrMsg)
return fmt.Errorf("failed to execute `go generate`:\n%s", strings.Join(newLines, "\n"))
}

return nil
Expand Down Expand Up @@ -200,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 @@ -211,25 +213,61 @@ 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)
}
}

return fmt.Errorf("failed to execute `tsp-client %s`\n%s", strings.Join(args, " "), strings.Join(newErrInfo, "\n"))
// filter diagnostic errors
if len(newErrMsgs) >= 1 &&
newErrMsgs[0] == "Diagnostics were reported during compilation. Use the `--debug` flag to see the diagnostic output." {
newErrMsgs = newErrMsgs[1:]

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

if len(temp) > 0 {
newErrMsgs = append(newErrMsgs, temp...)
}
}

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

return nil
}

return fmt.Errorf("failed to execute `tsp-client %s`\n%+v", strings.Join(args, " "), err)
Expand Down Expand Up @@ -259,3 +297,56 @@ 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
kind = "warning"
} else if strings.Contains(line, "error ") {
if start != -1 {
diagnostics = append(diagnostics, diagnostic{kind: kind, start: start, end: i - 1})
}
start = i
kind = "error"
}

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

return diagnostics
}

func getErrorDiagnostics(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
}

0 comments on commit 217edee

Please sign in to comment.