Skip to content

Commit ab9ac54

Browse files
committed
cue/load: include module file in build.Instance
This makes the module file, including dependency and language version info, available to `cue/load.Instances` callers. Signed-off-by: Roger Peppe <[email protected]> Change-Id: I4f9a955dd30c1564ae0d73d91f9282ebff1c4c34 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1217063 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 1a2d2e7 commit ab9ac54

File tree

5 files changed

+14
-0
lines changed

5 files changed

+14
-0
lines changed

cue/build/instance.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"cuelang.org/go/cue/errors"
2727
"cuelang.org/go/cue/parser"
2828
"cuelang.org/go/cue/token"
29+
"cuelang.org/go/internal/mod/modfiledata"
2930
)
3031

3132
// An Instance describes the collection of files, and its imports, necessary
@@ -80,6 +81,9 @@ type Instance struct {
8081
// imported by other packages, including those within the module.
8182
Module string
8283

84+
// ModuleFile holds the actual module file data, if available.
85+
ModuleFile *modfiledata.File
86+
8387
// Root is the root of the directory hierarchy, it may be "" if this an
8488
// instance has no imports.
8589
// If Module != "", this corresponds to the module root.

cue/load/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ func (c *Config) newErrInstance(err error) *build.Instance {
472472
i := c.Context.NewInstance("", nil)
473473
i.Root = c.ModuleRoot
474474
i.Module = c.Module
475+
i.ModuleFile = c.modFile
475476
i.Err = errors.Promote(err, "")
476477
return i
477478
}

cue/load/import.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ func (l *loader) newRelInstance(pos token.Pos, path, pkgName string) *build.Inst
317317
// p.ImportPath = string(dir) // compute unique ID.
318318
p.Root = l.cfg.ModuleRoot
319319
p.Module = l.cfg.Module
320+
p.ModuleFile = l.cfg.modFile
320321

321322
var err errors.Error
322323
if path != cleanImport(path) {
@@ -402,6 +403,7 @@ func (l *loader) newInstance(pos token.Pos, p importPath) *build.Instance {
402403
i.ImportPath = string(p)
403404
i.Root = l.cfg.ModuleRoot
404405
i.Module = modPath
406+
i.ModuleFile = l.cfg.modFile
405407

406408
return i
407409
}

cue/load/loader_common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ func (fp *fileProcessor) add(root string, file *build.File, mode importMode) {
248248
q.ImportPath = p.ImportPath + ":" + pkg
249249
q.Root = p.Root
250250
q.Module = p.Module
251+
q.ModuleFile = p.ModuleFile
251252
fp.pkgs[pkg] = q
252253
}
253254
p = q

cue/load/module_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ func TestModuleFetch(t *testing.T) {
102102
})
103103
return
104104
}
105+
if inst.Module != "" {
106+
// Sanity check that the module file is present in the instance.
107+
qt.Assert(t, qt.Not(qt.IsNil(inst.ModuleFile)))
108+
qt.Assert(t, qt.Equals(inst.ModuleFile.Module, inst.Module))
109+
qt.Assert(t, qt.Equals(inst.ModuleFile.Language.Version, "v0.8.0"))
110+
}
105111
v := ctx.BuildInstance(inst)
106112
if err := v.Validate(); err != nil {
107113
t.Fatal(err)

0 commit comments

Comments
 (0)