diff --git a/src/go/cmd/api_view_test.go b/src/go/cmd/api_view_test.go index e21350deb795..1522164a0cea 100644 --- a/src/go/cmd/api_view_test.go +++ b/src/go/cmd/api_view_test.go @@ -264,3 +264,34 @@ func TestRecursiveAliasDefinitions(t *testing.T) { }) } } + +func TestAliasDiagnostics(t *testing.T) { + review, err := createReview(filepath.Clean("testdata/test_alias_diagnostics")) + require.NoError(t, err) + require.Equal(t, "Go", review.Language) + require.Equal(t, "test_alias_diagnostics", review.Name) + require.Equal(t, 6, len(review.Diagnostics)) + for _, diagnostic := range review.Diagnostics { + if diagnostic.TargetID == "test_alias_diagnostics.WidgetValue" { + require.Equal(t, DiagnosticLevelInfo, diagnostic.Level) + require.Equal(t, aliasFor+"internal.WidgetValue", diagnostic.Text) + } else { + require.Equal(t, "test_alias_diagnostics.Widget", diagnostic.TargetID) + switch diagnostic.Level { + case DiagnosticLevelInfo: + require.Equal(t, aliasFor+"internal.Widget", diagnostic.Text) + case DiagnosticLevelError: + switch txt := diagnostic.Text; txt { + case missingAliasFor + "WidgetProperties": + case missingAliasFor + "WidgetPropertiesP": + case missingAliasFor + "WidgetThings": + case missingAliasFor + "WidgetThingsP": + default: + t.Fatalf("unexpected diagnostic text %s", txt) + } + default: + t.Fatalf("unexpected diagnostic level %d", diagnostic.Level) + } + } + } +} diff --git a/src/go/cmd/module.go b/src/go/cmd/module.go index 2ee8200873a1..5272cac6e2d0 100644 --- a/src/go/cmd/module.go +++ b/src/go/cmd/module.go @@ -48,6 +48,11 @@ func NewModule(dir string) (*Module, error) { m := Module{Name: filepath.Base(dir), packages: map[string]*Pkg{}} baseImportPath := path.Dir(mf.Module.Mod.Path) + "/" + if baseImportPath == "./" { + // this is a relative path in the tests, so remove this prefix. + // if not, then the package name added below won't match the imported packages. + baseImportPath = "" + } err = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { if d.IsDir() { if !indexTestdata && strings.Contains(path, "testdata") { diff --git a/src/go/cmd/pkg.go b/src/go/cmd/pkg.go index bb28e78a8cc6..632e1f15afdb 100644 --- a/src/go/cmd/pkg.go +++ b/src/go/cmd/pkg.go @@ -18,6 +18,7 @@ import ( // diagnostic messages const ( aliasFor = "Alias for " + missingAliasFor = "missing alias for nested type " embedsUnexportedStruct = "Anonymously embeds unexported struct " sealedInterface = "Applications can't implement this interface" ) diff --git a/src/go/cmd/testdata/test_alias_diagnostics/internal/internal.go b/src/go/cmd/testdata/test_alias_diagnostics/internal/internal.go new file mode 100644 index 000000000000..7a238227a083 --- /dev/null +++ b/src/go/cmd/testdata/test_alias_diagnostics/internal/internal.go @@ -0,0 +1,20 @@ +package internal + +type Widget struct { + OK bool + Value WidgetValue + MissingScalar WidgetProperties + MissingScalarP *WidgetPropertiesP + MissingSlice []WidgetThings + MissingSliceP []*WidgetThingsP +} + +type WidgetProperties struct{} + +type WidgetPropertiesP struct{} + +type WidgetThings struct{} + +type WidgetThingsP struct{} + +type WidgetValue struct{} diff --git a/src/go/cmd/testdata/test_alias_diagnostics/test.go b/src/go/cmd/testdata/test_alias_diagnostics/test.go new file mode 100644 index 000000000000..55bdf4f31888 --- /dev/null +++ b/src/go/cmd/testdata/test_alias_diagnostics/test.go @@ -0,0 +1,9 @@ +package test_alias_diagnostics + +import ( + "test_alias_diagnostics/internal" +) + +type Widget = internal.Widget + +type WidgetValue = internal.WidgetValue