Skip to content

Commit 7e1e277

Browse files
Propagate DuplicateProtoPathError as typed error from new compiler path (#4517)
1 parent f781a0e commit 7e1e277

3 files changed

Lines changed: 18 additions & 22 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
connectrpc.com/connect v1.19.2
1919
connectrpc.com/grpcreflect v1.3.0
2020
connectrpc.com/otelconnect v0.9.0
21-
github.com/bufbuild/protocompile v0.14.2-0.20260427235036-6b040a6b602b
21+
github.com/bufbuild/protocompile v0.14.2-0.20260428232503-17b06932e73e
2222
github.com/bufbuild/protoplugin v0.0.0-20260414125817-25d1d281b46b
2323
github.com/cli/browser v1.3.0
2424
github.com/gofrs/flock v0.13.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ github.com/bmatcuk/doublestar/v4 v4.10.0 h1:zU9WiOla1YA122oLM6i4EXvGW62DvKZVxIe6
4242
github.com/bmatcuk/doublestar/v4 v4.10.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
4343
github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4=
4444
github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs=
45-
github.com/bufbuild/protocompile v0.14.2-0.20260427235036-6b040a6b602b h1:cPYlWl0BN1bITspyWdVW5B1Z3e7WYWFeJIiIpOi3O6g=
46-
github.com/bufbuild/protocompile v0.14.2-0.20260427235036-6b040a6b602b/go.mod h1:DhgqsRznX/F0sGkUYtTQJRP+q8xMReQRQ3qr+n1opWU=
45+
github.com/bufbuild/protocompile v0.14.2-0.20260428232503-17b06932e73e h1:wwAUu3QGBzfHahrk6uEymIqvs1XwG55GtHjc/ooOTYM=
46+
github.com/bufbuild/protocompile v0.14.2-0.20260428232503-17b06932e73e/go.mod h1:DhgqsRznX/F0sGkUYtTQJRP+q8xMReQRQ3qr+n1opWU=
4747
github.com/bufbuild/protoplugin v0.0.0-20260414125817-25d1d281b46b h1:b7wvo9ZhjLzCp7tGbOUMvgtYTnd33zGSAmMxcdxMnhQ=
4848
github.com/bufbuild/protoplugin v0.0.0-20260414125817-25d1d281b46b/go.mod h1:c5D8gWRIZ2HLWO3gXYTtUfw/hbJyD8xikv2ooPxnklQ=
4949
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=

private/bufpkg/bufimage/build_image.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,19 @@ func compileImage(
128128
return nil, err
129129
}
130130

131+
// Validate that there is a single result for all files.
132+
if len(results) != 1 {
133+
return nil, fmt.Errorf("expected a single result from query, instead got: %d", len(results))
134+
}
135+
136+
// Check for fatal errors before processing diagnostics so that callers can
137+
// inspect typed errors (e.g. DuplicateProtoPathError) via errors.As. Spanless
138+
// diagnostics that accompany these fatal errors are suppressed in the loop below.
139+
if results[0].Fatal != nil {
140+
return nil, results[0].Fatal
141+
}
142+
131143
var fileAnnotations []bufanalysis.FileAnnotation
132-
var rawErrors []error
133144
for _, diagnostic := range diagnostics.Diagnostics {
134145
if diagnostic.Level() > report.Error {
135146
// We only surface [report.Error] level or more severe diagnostics as build errors.
@@ -138,12 +149,9 @@ func compileImage(
138149
}
139150
primary := diagnostic.Primary()
140151
if primary.IsZero() {
141-
// Diagnostics without a source span (e.g. file-open errors such as
142-
// DuplicateProtoPathError) cannot be represented as FileAnnotations.
143-
// Surface them as raw errors so callers see the full message.
144-
if diagnostic.File() != "" {
145-
rawErrors = append(rawErrors, errors.New(diagnostic.Message()))
146-
}
152+
// Spanless diagnostics (e.g. from file-open errors like DuplicateProtoPathError)
153+
// are companion messages to fatal errors already handled above. Suppress them here
154+
// so their typed errors are preserved for callers rather than being lost as strings.
147155
continue
148156
}
149157
start := primary.Location(primary.Start, length.Bytes)
@@ -174,21 +182,9 @@ func compileImage(
174182
),
175183
)
176184
}
177-
if len(rawErrors) > 0 {
178-
return nil, errors.Join(rawErrors...)
179-
}
180185
if len(fileAnnotations) > 0 {
181186
return nil, bufanalysis.NewFileAnnotationSet(fileAnnotations...)
182187
}
183-
184-
// Validate that there is a single result for all files
185-
if len(results) != 1 {
186-
return nil, fmt.Errorf("expected a single result from query, instead got: %d", len(results))
187-
}
188-
189-
if results[0].Fatal != nil {
190-
return nil, results[0].Fatal
191-
}
192188
fds, resolver, err := resolverForFDS(results[0].Value)
193189
if err != nil {
194190
return nil, err

0 commit comments

Comments
 (0)