Skip to content

Commit 80ef0f1

Browse files
committed
fix: handle duplicate alias/package names
Signed-off-by: Piaras Hoban <[email protected]>
1 parent a236553 commit 80ef0f1

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# reproduction for https://github.com/cue-lang/cue/issues/3974
2+
exec cue exp gengotypes
3+
cmp cue_types_main_gen.go expect-gengo
4+
5+
-- cue.mod/module.cue --
6+
module: "mod.test"
7+
language: version: "v0.13.0"
8+
9+
-- pkg/same/same.cue --
10+
package same
11+
12+
#Same: int
13+
14+
-- pkg/foo/foo.cue --
15+
package foo
16+
17+
#Bar: string
18+
19+
-- f1.cue --
20+
package main
21+
22+
import (
23+
"mod.test/pkg/same"
24+
)
25+
26+
#A: {
27+
value: same.#Same
28+
}
29+
30+
-- f2.cue --
31+
package main
32+
33+
import (
34+
same "mod.test/pkg/foo"
35+
)
36+
37+
#B: {
38+
bar: same.#Bar
39+
}
40+
41+
-- expect-gengo --
42+
// Code generated by "cue exp gengotypes"; DO NOT EDIT.
43+
44+
package main
45+
46+
import (
47+
same1 "mod.test/pkg/foo"
48+
"mod.test/pkg/same"
49+
)
50+
51+
type A struct {
52+
Value same.Same `json:"value"`
53+
}
54+
55+
type B struct {
56+
Bar same1.Bar `json:"bar"`
57+
}

internal/encoding/gotypes/generate.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -799,23 +799,41 @@ func emitDocs(printf func(string, ...any), name string, groups []*ast.CommentGro
799799
// gatherImportAliases collects the aliases from imports across the instance.
800800
func gatherImportAliases(inst *build.Instance) (map[string]string, error) {
801801
fileAliases := make(map[string]string)
802-
trackedAliases := make(map[string]int32)
802+
tracked := make(map[string]int)
803+
804+
type pair struct{ path, alias string }
805+
var explicit []pair
806+
803807
for _, f := range inst.Files {
804808
for _, s := range f.Imports {
805-
if s.Path == nil || s.Name == nil {
809+
if s.Path == nil {
806810
continue
807811
}
808-
alias := s.Name.Name
809-
path, err := strconv.Unquote(s.Path.Value)
812+
pkgPath, err := strconv.Unquote(s.Path.Value)
810813
if err != nil {
811814
return nil, err
812815
}
813-
if count, found := trackedAliases[alias]; found {
814-
alias = fmt.Sprintf("%s%d", alias, count)
816+
// Unaliased import: reserve its base name
817+
if s.Name == nil {
818+
base := filepath.Base(pkgPath)
819+
tracked[base]++
820+
continue
815821
}
816-
fileAliases[path] = alias
817-
trackedAliases[alias]++
822+
// Explicit alias: queue for resolution
823+
alias := s.Name.Name
824+
explicit = append(explicit, pair{path: pkgPath, alias: alias})
818825
}
819826
}
827+
828+
// Resolve explicit aliases with conflict suffixing.
829+
for _, e := range explicit {
830+
alias := e.alias
831+
if count, ok := tracked[alias]; ok {
832+
alias = fmt.Sprintf("%s%d", alias, count)
833+
}
834+
fileAliases[e.path] = alias
835+
tracked[e.alias]++ // track the alias name (unsuffixed) for future conflicts
836+
}
837+
820838
return fileAliases, nil
821839
}

0 commit comments

Comments
 (0)