package compiler
import (
"errors"
"fmt"
"io/fs"
"path"
"path/filepath"
cueerrors "cuelang.org/go/cue/errors"
cueload "cuelang.org/go/cue/load"
)
// Build a cue configuration tree from the files in fs.
func Build(sources map[string]fs.FS, args ...string) (*Value, error) {
c := DefaultCompiler
buildConfig := &cueload.Config{
// The CUE overlay needs to be prefixed by a non-conflicting path with the
// local filesystem, otherwise Cue will merge the Overlay with whatever Cue
// files it finds locally.
Dir: "/config",
Overlay: map[string]cueload.Source{},
}
// Map the source files into the overlay
for mnt, f := range sources {
f := f
mnt := mnt
err := fs.WalkDir(f, ".", func(p string, entry fs.DirEntry, err error) error {
if err != nil {
return err
if !entry.Type().IsRegular() {
return nil
if filepath.Ext(entry.Name()) != ".cue" {
contents, err := fs.ReadFile(f, p)
return fmt.Errorf("%s: %w", p, err)
overlayPath := path.Join(buildConfig.Dir, mnt, p)
buildConfig.Overlay[overlayPath] = cueload.FromBytes(contents)
})
return nil, err
instances := cueload.Instances(args, buildConfig)
if len(instances) != 1 {
return nil, errors.New("only one package is supported at a time")
for _, value := range instances {
if value.Err != nil {
return nil, value.Err
v, err := c.Context.BuildInstances(instances)
return nil, errors.New(cueerrors.Details(err, &cueerrors.Config{}))
for _, value := range v {
if value.Err() != nil {
return nil, value.Err()
if len(v) != 1 {
return nil, errors.New("internal: wrong number of values")
return Wrap(v[0]), nil