Skip to content

Commit bd3dd75

Browse files
committed
cue/load: rewrite pre-resolved files after injection
This also moves tag discover to load.Instances to prevent finding tags in imported packages. This "feature" slipped in when moving this functionaltiy to cue/load. Change-Id: Ib04d826d95ffebd152310aaad26d0a896991f076 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7121 Reviewed-by: Paul Jolly <[email protected]> Reviewed-by: CUE cueckoo <[email protected]>
1 parent 20c637a commit bd3dd75

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cue cmd -t greeting=hello prefix
2+
3+
-- cue.mod/module.cue --
4+
module: "mod.com"
5+
-- my.cue --
6+
package tools
7+
8+
msg: string @tag(greeting)
9+
10+
-- my_tool.cue --
11+
package tools
12+
13+
import (
14+
"tool/cli"
15+
)
16+
17+
greeting: string @tag(greeting)
18+
19+
command: prefix: {
20+
p1: cli.Print & {
21+
text: greeting
22+
}
23+
salutation: string @tag(greeting)
24+
p2: cli.Print & {
25+
text: salutation
26+
}
27+
p3: cli.Print & {
28+
text: msg @tag(greeting)
29+
}
30+
}

cue/load/loader.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"golang.org/x/xerrors"
2929

30+
"cuelang.org/go/cue/ast"
3031
"cuelang.org/go/cue/build"
3132
"cuelang.org/go/cue/errors"
3233
"cuelang.org/go/cue/token"
@@ -84,6 +85,14 @@ func Instances(args []string, c *Config) []*build.Instance {
8485
a = append(a, l.cueFilesPackage(files))
8586
}
8687

88+
for _, p := range a {
89+
tags, err := findTags(p)
90+
if err != nil {
91+
p.ReportError(err)
92+
}
93+
l.tags = append(l.tags, tags...)
94+
}
95+
8796
// TODO(api): have API call that returns an error which is the aggregate
8897
// of all build errors. Certain errors, like these, hold across builds.
8998
if err := injectTags(c.Tags, l); err != nil {
@@ -92,6 +101,22 @@ func Instances(args []string, c *Config) []*build.Instance {
92101
}
93102
}
94103

104+
if l.replacements == nil {
105+
return a
106+
}
107+
108+
for _, p := range a {
109+
for _, f := range p.Files {
110+
ast.Walk(f, nil, func(n ast.Node) {
111+
if ident, ok := n.(*ast.Ident); ok {
112+
if v, ok := l.replacements[ident.Node]; ok {
113+
ident.Node = v
114+
}
115+
}
116+
})
117+
}
118+
}
119+
95120
return a
96121
}
97122

@@ -110,10 +135,11 @@ const (
110135
)
111136

112137
type loader struct {
113-
cfg *Config
114-
stk importStack
115-
tags []tag // tags found in files
116-
buildTags map[string]bool
138+
cfg *Config
139+
stk importStack
140+
tags []tag // tags found in files
141+
buildTags map[string]bool
142+
replacements map[ast.Node]ast.Node
117143
}
118144

119145
func (l *loader) abs(filename string) string {
@@ -205,11 +231,6 @@ func (l *loader) addFiles(dir string, p *build.Instance) {
205231
}
206232
d.Close()
207233
}
208-
tags, err := findTags(p)
209-
if err != nil {
210-
p.ReportError(err)
211-
}
212-
l.tags = append(l.tags, tags...)
213234
}
214235

215236
func cleanImport(path string) string {

cue/load/tags.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,17 @@ func parseTag(pos token.Pos, body string) (t tag, err errors.Error) {
8888
return t, nil
8989
}
9090

91-
func (t *tag) inject(value string) errors.Error {
91+
func (t *tag) inject(value string, l *loader) errors.Error {
9292
e, err := cli.ParseValue(token.NoPos, t.key, value, t.kind)
9393
if err != nil {
9494
return err
9595
}
96-
t.field.Value = ast.NewBinExpr(token.AND, t.field.Value, e)
96+
injected := ast.NewBinExpr(token.AND, t.field.Value, e)
97+
if l.replacements == nil {
98+
l.replacements = map[ast.Node]ast.Node{}
99+
}
100+
l.replacements[t.field.Value] = injected
101+
t.field.Value = injected
97102
return nil
98103
}
99104

@@ -161,7 +166,7 @@ func injectTags(tags []string, l *loader) errors.Error {
161166
for _, t := range l.tags {
162167
if t.key == s[:p] {
163168
found = true
164-
if err := t.inject(s[p+1:]); err != nil {
169+
if err := t.inject(s[p+1:], l); err != nil {
165170
return err
166171
}
167172
}
@@ -174,7 +179,7 @@ func injectTags(tags []string, l *loader) errors.Error {
174179
for _, sh := range t.shorthands {
175180
if sh == s {
176181
found = true
177-
if err := t.inject(s); err != nil {
182+
if err := t.inject(s, l); err != nil {
178183
return err
179184
}
180185
}

0 commit comments

Comments
 (0)