Skip to content

Commit 12ffe25

Browse files
authored
Merge pull request #92 from JoelSpeed/debug-panic
Fix panic when files do not start with package
2 parents 6df69a1 + 77bd396 commit 12ffe25

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

pkg/analysis/helpers/inspector/inspector.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ limitations under the License.
1616
package inspector
1717

1818
import (
19+
"fmt"
1920
"go/ast"
2021
"go/token"
2122

2223
astinspector "golang.org/x/tools/go/ast/inspector"
2324
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2425
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
26+
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
2527
)
2628

2729
// Inspector is an interface that allows for the inspection of fields in structs.
@@ -103,6 +105,14 @@ func (i *inspector) InspectFields(inspectField func(field *ast.Field, stack []as
103105
return false
104106
}
105107

108+
defer func() {
109+
if r := recover(); r != nil {
110+
// If the inspectField function panics, we recover and log information that will help identify the issue.
111+
debug := printDebugInfo(field)
112+
panic(fmt.Sprintf("%s %v", debug, r)) // Re-panic to propagate the error.
113+
}
114+
}()
115+
106116
inspectField(field, stack, tagInfo, i.markers)
107117

108118
return true
@@ -152,3 +162,13 @@ func isItemsType(structType *ast.StructType) bool {
152162

153163
return true
154164
}
165+
166+
// printDebugInfo prints debug information about the field that caused a panic during inspection.
167+
// This function is designed to allow us to help identify which fields are causing issues during inspection.
168+
func printDebugInfo(field *ast.Field) string {
169+
var debug string
170+
171+
debug += fmt.Sprintf("Panic observed while inspecting field: %v (type: %v)\n", utils.FieldName(field), field.Type)
172+
173+
return debug
174+
}
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
116
package a
217

3-
type StructFromAnotherFile struct{}
18+
type StructFromAnotherFile struct {
19+
// OptionalStringField is a string field that is optional.
20+
// +optional
21+
OptionalStringField string `json:"optionalStringField,omitempty"`
22+
}

pkg/analysis/utils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func getFilesForType(pass *analysis.Pass, ident *ast.Ident) (*token.File, *ast.F
106106
tokenFile := pass.Fset.File(namedType.Obj().Pos())
107107

108108
for _, astFile := range pass.Files {
109-
if astFile.Package == token.Pos(tokenFile.Base()) {
109+
if astFile.FileStart == token.Pos(tokenFile.Base()) {
110110
return tokenFile, astFile
111111
}
112112
}

0 commit comments

Comments
 (0)