From a9c51cdc77a561b2def4b5c92cb1a19d5554a816 Mon Sep 17 00:00:00 2001 From: SAY-5 Date: Tue, 5 May 2026 03:06:25 -0700 Subject: [PATCH] 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. --- internal/codegen/golang/opts/go_type.go | 7 ++++++- internal/codegen/golang/opts/override_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/internal/codegen/golang/opts/go_type.go b/internal/codegen/golang/opts/go_type.go index 5dcb3e8af0..2eae097702 100644 --- a/internal/codegen/golang/opts/go_type.go +++ b/internal/codegen/golang/opts/go_type.go @@ -146,7 +146,12 @@ func (gt GoType) parse() (*ParsedGoType, error) { o.BasicType = true } else { // assume the type lives in a Go package - if lastDot == -1 { + if lastDot == -1 || lastDot < lastSlash { + // Either no dot at all, or every dot is part of the import path + // (e.g. "github.com/foo/pkg/Type"). Without a dot separating + // package from type after the last slash, we cannot tell where + // the import path ends and the type name begins, and silently + // guessing produces a broken import like "github". 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) } typename = input[lastSlash+1:] diff --git a/internal/codegen/golang/opts/override_test.go b/internal/codegen/golang/opts/override_test.go index 8405666f36..ab6c74a4c5 100644 --- a/internal/codegen/golang/opts/override_test.go +++ b/internal/codegen/golang/opts/override_test.go @@ -86,6 +86,19 @@ func TestTypeOverrides(t *testing.T) { }, "Package override `go_type` specifier \"untyped rune\" is not a Go basic type e.g. 'string'", }, + { + // Regression for sqlc-dev/sqlc#4192: when the user writes the + // type name with a slash separator instead of a dot (i.e. + // "path/to/pkg/Type" rather than "path/to/pkg.Type"), the + // previous parser silently used the dot inside "github.com" and + // emitted a broken import like `"github"`. Reject these specs + // with a clear error instead. + Override{ + DBType: "text", + GoType: GoType{Spec: "github.com/example/pkg/SomeType"}, + }, + "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'", + }, } { tt := test t.Run(tt.override.GoType.Spec, func(t *testing.T) {