Skip to content

Commit 0154403

Browse files
authored
Fix generation of facts file when diagnostics are ignored (#4422)
**What type of PR is this?** Bug fix **What does this PR do? Why is it needed?** After #4402, it is no longer possible to use a custom analyzer without having to declare a new `Fact` type. The build of a Go library will fail if the library contains any external dependencies. That's the case for executing gazelle by example (if we don't use a precompiled binary): ``` $ bazel run //:gazelle ERROR: .../external/gazelle++go_deps+org_golang_x_sync/errgroup/BUILD.bazel:3:11: output 'external/gazelle++go_deps+org_golang_x_sync/errgroup/errgroup.facts' was not created ERROR: .../external/gazelle++go_deps+org_golang_x_sync/errgroup/BUILD.bazel:3:11: Running nogo on @@gazelle++go_deps+org_golang_x_sync//errgroup:errgroup failed: not all outputs were created or valid ERROR: .../external/gazelle++go_deps+com_github_bazelbuild_buildtools/labels/BUILD.bazel:3:11: output 'external/gazelle++go_deps+com_github_bazelbuild_buildtools/labels/labels.facts' was not created ERROR: .../external/gazelle++go_deps+com_github_bazelbuild_buildtools/labels/BUILD.bazel:3:11: Running nogo on @@gazelle++go_deps+com_github_bazelbuild_buildtools//labels:labels failed: not all outputs were created or valid ERROR: .../external/gazelle++go_deps+in_gopkg_yaml_v3/BUILD.bazel:5:11: output 'external/gazelle++go_deps+in_gopkg_yaml_v3/yaml_v3.facts' was not created ERROR: .../external/gazelle++go_deps+in_gopkg_yaml_v3/BUILD.bazel:5:11: Running nogo on @@gazelle++go_deps+in_gopkg_yaml_v3//:yaml_v3 failed: not all outputs were created or valid ERROR: .../external/gazelle++go_deps+org_golang_x_sys/execabs/BUILD.bazel:3:11: output 'external/gazelle++go_deps+org_golang_x_sys/execabs/execabs.facts' was not created ERROR: .../external/gazelle++go_deps+org_golang_x_sys/execabs/BUILD.bazel:3:11: Running nogo on @@gazelle++go_deps+org_golang_x_sys//execabs:execabs failed: not all outputs were created or valid ... ``` **Other notes for review** This review continue to skip analysers when ignoring diagnostics, but return a valid `pkg` to create the missing `.facts` file required by the validation action, and incidentally, fix a potential crash at line https://github.com/bazel-contrib/rules_go/blob/1f993522f463c142c5516f94852c3d6597f717c6/go/tools/builders/nogo_main.go#L98.
1 parent 1f99352 commit 0154403

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

go/tools/builders/nogo_main.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,26 @@ func run(args []string) (error, int) {
9393
if err != nil {
9494
return fmt.Errorf("error running analyzers: %v", err), nogoError
9595
}
96+
9697
// Write the facts file for downstream consumers before failing due to diagnostics.
9798
if *xPath != "" {
98-
if err := os.WriteFile(abs(*xPath), pkg.facts.Encode(), 0o666); err != nil {
99+
var factsContent []byte
100+
if pkg != nil {
101+
factsContent = pkg.facts.Encode()
102+
}
103+
104+
if err := os.WriteFile(abs(*xPath), factsContent, 0o666); err != nil {
99105
return fmt.Errorf("error writing facts: %v", err), nogoError
100106
}
101107
}
108+
109+
var fset *token.FileSet
110+
if pkg != nil {
111+
fset = pkg.fset
112+
} else if len(diagnostics) > 0 {
113+
return fmt.Errorf("pkg should not be nil with diagnostics"), nogoError
114+
}
115+
102116
exitCode := nogoSuccess
103117
var errMsg bytes.Buffer
104118
if len(diagnostics) > 0 {
@@ -110,11 +124,11 @@ func run(args []string) (error, int) {
110124
}
111125
errMsg.WriteString("errors found by nogo during build-time code analysis:")
112126
for _, d := range diagnostics {
113-
fmt.Fprintf(&errMsg, "\n%s: %s (%s)", pkg.fset.Position(d.Pos), d.Message, d.analyzerName)
127+
fmt.Fprintf(&errMsg, "\n%s: %s (%s)", fset.Position(d.Pos), d.Message, d.analyzerName)
114128
}
115129
}
116130

117-
if errs := saveSuggestedFixes(*nogoFixDir, diagnostics, pkg); len(errs) > 0 {
131+
if errs := saveSuggestedFixes(*nogoFixDir, diagnostics, fset); len(errs) > 0 {
118132
errMsg.WriteString("\nsaving suggested fixes:")
119133
for _, err := range errs {
120134
fmt.Fprintf(&errMsg, "\n%v", err)
@@ -127,12 +141,12 @@ func run(args []string) (error, int) {
127141
return nil, exitCode
128142
}
129143

130-
func saveSuggestedFixes(nogoFixDir string, diagnostics []diagnosticEntry, pkg *goPackage) []error {
144+
func saveSuggestedFixes(nogoFixDir string, diagnostics []diagnosticEntry, fset *token.FileSet) []error {
131145
if nogoFixDir == "" {
132146
return nil
133147
}
134148
var errs []error
135-
fixes, err := getFixes(diagnostics, pkg.fset)
149+
fixes, err := getFixes(diagnostics, fset)
136150
if err != nil {
137151
errs = append(errs, err)
138152
}

0 commit comments

Comments
 (0)