Skip to content

Commit 187fb1a

Browse files
committed
cue/interpreter/embed: forbid ** via pkg/path.Match
Our pkg/path.Match func mimics Go's own path.Match in that it can be used as a validator via `Match(pattern, "")`. This is better than a `strings.Contains(pattern, "**")` not only because we get consistent error messages, but also because Contains gives false positives on valid patterns such as `\**`, which escapes the first star, or `[x**y]`, where the stars are just part of a character set. Fix the code, and add more tests that demonstrate that embedding does support a glob with `\**` now. This brings embed in line with pkg/path.Match and tool/file.Glob in terms of what pattern matching syntax and features it supports. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I0ed6fcab9426c6392fb18491a3f656f29c56b41e Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198692 Reviewed-by: Roger Peppe <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 987a85e commit 187fb1a

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

cmd/cue/cmd/testdata/script/embed_err.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ language: version: "v0.9.0"
106106
./test.cue:19:15
107107
@embed: only relative files are allowed:
108108
./test.cue:21:12
109-
@embed: double star not (yet) supported in glob:
109+
@embed: invalid glob pattern "**/*.json": '**' is not supported in patterns as of yet:
110110
./test.cue:23:15
111111
@embed: streaming not implemented: found more than one value in file:
112112
./test.cue:25:11
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
env CUE_EXPERIMENT=embed
2+
3+
[!windows] cp reuse.json 'star/*.json'
4+
[!windows] exec cue export --out cue
5+
[!windows] cmp stdout out/export-unix
6+
7+
[windows] exec cue export --out cue
8+
[windows] cmp stdout out/export-windows
9+
10+
-- test.cue --
11+
@extern(embed)
12+
13+
package foo
14+
15+
// Unix OSes can have a file containing a star character, and we can match it.
16+
// Windows can still use these file paths and glob patterns, but they can't match
17+
// a file containing a star character, as such filenames are not allowed on Windows.
18+
19+
globStar: _ @embed(glob="star/*.json")
20+
globEscapeStar: _ @embed(glob="star/\\**", type=json)
21+
-- reuse.json --
22+
{"x": "to be reused for more names"}
23+
-- star/simple.json --
24+
{"x": "does not contain a star character"}
25+
-- out/export-unix --
26+
globStar: {
27+
"star/*.json": x: "to be reused for more names"
28+
"star/simple.json": x: "does not contain a star character"
29+
}
30+
globEscapeStar: "star/*.json": x: "to be reused for more names"
31+
-- out/export-windows --
32+
globStar: "star/simple.json": x: "does not contain a star character"
33+
globEscapeStar: {}

cue/interpreter/embed/embed.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ import (
100100
"cuelang.org/go/internal/encoding"
101101
"cuelang.org/go/internal/filetypes"
102102
"cuelang.org/go/internal/value"
103+
pkgpath "cuelang.org/go/pkg/path"
103104
)
104105

105106
// TODO: obtain a fs.FS from load or something similar
@@ -218,8 +219,10 @@ func (c *compiler) processGlob(glob, scope string, schema adt.Value) (adt.Expr,
218219
return nil, ce
219220
}
220221

221-
if strings.Contains(glob, "**") {
222-
return nil, errors.Newf(c.pos, "double star not (yet) supported in glob")
222+
// Validate that the glob pattern is valid per [pkgpath.Match].
223+
// Note that we use Unix match semantics because all embed paths are Unix-like.
224+
if _, err := pkgpath.Match(glob, "", pkgpath.Unix); err != nil {
225+
return nil, errors.Wrapf(err, c.pos, "invalid glob pattern %q", glob)
223226
}
224227

225228
// If we do not have a type, ensure the extension of the base is fully

0 commit comments

Comments
 (0)