Skip to content

Commit a9c51cd

Browse files
author
SAY-5
committed
fix(golang): reject go_type specs with slash before type name
Specs like "github.com/foo/pkg/Type" used to silently emit a broken "github" import; require the conventional dot separator. Fixes #4192.
1 parent b84b1d6 commit a9c51cd

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

internal/codegen/golang/opts/go_type.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ func (gt GoType) parse() (*ParsedGoType, error) {
146146
o.BasicType = true
147147
} else {
148148
// assume the type lives in a Go package
149-
if lastDot == -1 {
149+
if lastDot == -1 || lastDot < lastSlash {
150+
// Either no dot at all, or every dot is part of the import path
151+
// (e.g. "github.com/foo/pkg/Type"). Without a dot separating
152+
// package from type after the last slash, we cannot tell where
153+
// the import path ends and the type name begins, and silently
154+
// guessing produces a broken import like "github".
150155
return nil, fmt.Errorf("Package override `go_type` specifier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", input)
151156
}
152157
typename = input[lastSlash+1:]

internal/codegen/golang/opts/override_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ func TestTypeOverrides(t *testing.T) {
8686
},
8787
"Package override `go_type` specifier \"untyped rune\" is not a Go basic type e.g. 'string'",
8888
},
89+
{
90+
// Regression for sqlc-dev/sqlc#4192: when the user writes the
91+
// type name with a slash separator instead of a dot (i.e.
92+
// "path/to/pkg/Type" rather than "path/to/pkg.Type"), the
93+
// previous parser silently used the dot inside "github.com" and
94+
// emitted a broken import like `"github"`. Reject these specs
95+
// with a clear error instead.
96+
Override{
97+
DBType: "text",
98+
GoType: GoType{Spec: "github.com/example/pkg/SomeType"},
99+
},
100+
"Package override `go_type` specifier \"github.com/example/pkg/SomeType\" is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'",
101+
},
89102
} {
90103
tt := test
91104
t.Run(tt.override.GoType.Spec, func(t *testing.T) {

0 commit comments

Comments
 (0)