Skip to content

Commit

Permalink
cue/interpreter/embed: error when embed is not in a module
Browse files Browse the repository at this point in the history
The embed proposal is designed for CUE files within a module. Standalone
CUE files should not be able to read arbitrary files inside the current
directory unless they are being interpreted in the context of a module.

We also need to fix the `encoding/jsonschema/internal/externaltest`
package because it was using `ModuleRoot` inappropriately. When it's a
relative path, that field is treated relative to `Dir`, but we were
using it as if it were relative to the current directory.

We also need to fix a few tests that were using embedding with
CUE code that wasn't inside a module.

Fixes #3530

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I2f06be29e6a3b15182c1b9c4a849e9e0c35ba0ff
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1203264
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe committed Oct 29, 2024
1 parent 06d0c73 commit edef548
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/cue/cmd/testdata/script/embed.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ cmp stdout out/export
exec cue vet
cmp stdout out/vet

-- cue.mod/module.cue --
module: "cue.example"
language: version: "v0.11.0"

-- test.cue --
@extern(embed)

Expand Down
4 changes: 4 additions & 0 deletions cmd/cue/cmd/testdata/script/embed_err.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ symlink symlink-dir -> y
exec cue eval .
cmp stdout out/std

-- cue.mod/module.cue --
module: "cue.example"
language: version: "v0.11.0"

-- test.cue --
@extern(embed)

Expand Down
4 changes: 4 additions & 0 deletions cmd/cue/cmd/testdata/script/embed_windows.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ env CUE_EXPERIMENT=embed
[windows] exec cue export --out cue
[windows] cmp stdout out/export-windows

-- cue.mod/module.cue --
module: "cue.test"
language: version: "v0.11.0"

-- test.cue --
@extern(embed)

Expand Down
3 changes: 1 addition & 2 deletions cue/embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ func TestEmbedFailsWhenNotInModule(t *testing.T) {
package foo
x: _ @embed(file="testdata/readme.md",type=text)
`)
qt.Assert(t, qt.IsNil(v.Err()))
// TODO qt.Assert(t, qt.ErrorMatches(v.Err(), `xxx`))
qt.Assert(t, qt.ErrorMatches(v.Err(), `cannot embed files when not in a module`))
}
6 changes: 6 additions & 0 deletions cue/interpreter/embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ func (i *interpreter) Kind() string {
// NewCompiler returns a compiler that can decode and embed files that exist
// within a CUE module.
func (i *interpreter) NewCompiler(b *build.Instance, r *runtime.Runtime) (runtime.Compiler, errors.Error) {
if b.Module == "" {
return nil, errors.Newf(token.Pos{}, "cannot embed files when not in a module")
}
if b.Root == "" {
return nil, errors.Newf(token.Pos{}, "cannot embed files: no module root found")
}
return &compiler{
b: b,
runtime: (*cue.Context)(r),
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/internal/externaltest/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func ReadTestDir(dir string) (tests map[string][]*Schema, err error) {
Dir: dir,
// Just like in the cue/load tests, prevent Go tests from walking up to the root
// directory of the git repository, as that almost always causes test cache misses.
ModuleRoot: dir,
ModuleRoot: ".",
})[0]
if err := inst.Err; err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions encoding/jsonschema/testdata/external/cue.mod/module.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module: "cue.example"
language: {
version: "v0.11.0"
}

0 comments on commit edef548

Please sign in to comment.