Print diagnostics very similar to the -json flag in Go.
(err error, jsonDiagnostics bool)
| 1576 | |
| 1577 | // Print diagnostics very similar to the -json flag in Go. |
| 1578 | func printBuildOutput(err error, jsonDiagnostics bool) { |
| 1579 | if err == nil { |
| 1580 | return // nothing to report |
| 1581 | } |
| 1582 | |
| 1583 | if jsonDiagnostics { |
| 1584 | workingDir, getwdErr := os.Getwd() |
| 1585 | if getwdErr != nil { |
| 1586 | workingDir = "" |
| 1587 | } |
| 1588 | |
| 1589 | type jsonDiagnosticOutput struct { |
| 1590 | ImportPath string |
| 1591 | Action string |
| 1592 | Output string `json:",omitempty"` |
| 1593 | StartPos string `json:",omitempty"` // non-standard |
| 1594 | EndPos string `json:",omitempty"` // non-standard |
| 1595 | } |
| 1596 | |
| 1597 | for _, diags := range diagnostics.CreateDiagnostics(err) { |
| 1598 | if diags.ImportPath != "" { |
| 1599 | output, _ := json.Marshal(jsonDiagnosticOutput{ |
| 1600 | ImportPath: diags.ImportPath, |
| 1601 | Action: "build-output", |
| 1602 | Output: "# " + diags.ImportPath + "\n", |
| 1603 | }) |
| 1604 | os.Stdout.Write(append(output, '\n')) |
| 1605 | } |
| 1606 | for _, diag := range diags.Diagnostics { |
| 1607 | w := &bytes.Buffer{} |
| 1608 | diag.WriteTo(w, workingDir) |
| 1609 | data := jsonDiagnosticOutput{ |
| 1610 | ImportPath: diags.ImportPath, |
| 1611 | Action: "build-output", |
| 1612 | Output: w.String(), |
| 1613 | } |
| 1614 | if diag.StartPos.IsValid() && diag.EndPos.IsValid() { |
| 1615 | // Include the non-standard StartPos/EndPos values. These |
| 1616 | // are useful for the TinyGo Playground to show better error |
| 1617 | // messages. |
| 1618 | data.StartPos = diagnostics.RelativePosition(diag.StartPos, workingDir).String() |
| 1619 | data.EndPos = diagnostics.RelativePosition(diag.EndPos, workingDir).String() |
| 1620 | } |
| 1621 | output, _ := json.Marshal(data) |
| 1622 | os.Stdout.Write(append(output, '\n')) |
| 1623 | } |
| 1624 | |
| 1625 | // Emit the "Action":"build-fail" JSON. |
| 1626 | output, _ := json.Marshal(jsonDiagnosticOutput{ |
| 1627 | ImportPath: diags.ImportPath, |
| 1628 | Action: "build-fail", |
| 1629 | }) |
| 1630 | os.Stdout.Write(append(output, '\n')) |
| 1631 | } |
| 1632 | os.Exit(1) |
| 1633 | } |
| 1634 | |
| 1635 | // Regular diagnostic handling. |
no test coverage detected