Skip to content

Commit

Permalink
cmd/cue: support --package flag when output is CUE
Browse files Browse the repository at this point in the history
When running def and export (the latter when --out cue is set) it is
sometimes desirable to ensure that the resulting CUE belongs to a
package. This situation arises by default when non-CUE input is consumed
by cmd/cue, but can also occur when (for example) cue def produces
self-contained output.

The --package (-p) flag is currently used to define the package name
implied by non-CUE inputs.

It seems safe and appropriate to reuse this flag for the manifestation
of CUE at least.

Fixes #2240

Signed-off-by: Paul Jolly <[email protected]>
Change-Id: Iacce853ff36526fa70b3e92e9b6f9297187568e8
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1177546
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
myitcv committed Feb 29, 2024
1 parent cc98a77 commit 93acaa3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
34 changes: 34 additions & 0 deletions cmd/cue/cmd/testdata/script/def_package.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Ensure that cue def with a --package flag outputs CUE that belongs to a
# package.

# Regular cue def
exec cue def -p blah x.cue
cmp stdout orig.golden

# Self-contained
exec cue def -p blah -e x x.cue
cmp stdout self_contained.golden


-- x.cue --
#Def: int
x: #Def & 5

-- orig.golden --
package blah

#Def: int
x: #Def & {
5
}
-- self_contained.golden --
package blah

DEF.#x & {
5
}

//cue:path: #Def
let DEF = {
#x: int
}
49 changes: 49 additions & 0 deletions cmd/cue/cmd/testdata/script/export_cue.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Verify that -p works when exporting as CUE.

# Case 1: only non-CUE inputs
exec cue export --out cue -p blah x.json y.json
cmp stdout stdout.golden

# Case 2: CUE input which does not declare a package (with JSON)
exec cue export --out cue -p blah nopkg.cue x.json y.json
cmp stdout stdout.golden

# Case 3: CUE input which does declare same package (with JSON)
exec cue export --out cue -p blah pkg.cue x.json y.json
cmp stdout stdout.golden

# Case 4: CUE input with mis-matched package (with JSON)
#
# TODO: in the future we could allow "repackaging" via --force which would
# cause this export to succeed with the package clause 'blah' in the result.
! exec cue export --out cue -p blah diffpkg.cue x.json y.json
cmp stderr stderr.golden

-- x.json --
{
"x": 5
}
-- y.json --
{
"y": 4
}
-- nopkg.cue --
x: 5
y: 4
-- pkg.cue --
package blah

x: 5
y: 4
-- diffpkg.cue --
package other

x: 5
y: 4
-- stdout.golden --
package blah

x: 5
y: 4
-- stderr.golden --
"package" flag clashes with existing package name (blah vs other)
10 changes: 9 additions & 1 deletion internal/encoding/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,15 @@ func NewEncoder(f *build.File, cfg *Config) (*Encoder, error) {

// Casting an ast.Expr to an ast.File ensures that it always ends
// with a newline.
b, err := format.Node(internal.ToFile(n), opts...)
f := internal.ToFile(n)
if e.cfg.PkgName != "" && f.PackageName() == "" {
f.Decls = append([]ast.Decl{
&ast.Package{
Name: ast.NewIdent(e.cfg.PkgName),
},
}, f.Decls...)
}
b, err := format.Node(f, opts...)
if err != nil {
return err
}
Expand Down

0 comments on commit 93acaa3

Please sign in to comment.