internal/core/runtime: check non-existing builtin packages earlier

This used to be done during evaluation instead.

Fixes #1457

Signed-off-by: Marcel van Lohuizen <mpvl@golang.org>

Change-Id: Iaf9a1740ffe480a8c99bba0b0906bffdcc9908e7
Signed-off-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/530899
Reviewed-by: Paul Jolly <paul@myitcv.io>
diff --git a/cue/load/config.go b/cue/load/config.go
index 517346e..dc4effc 100644
--- a/cue/load/config.go
+++ b/cue/load/config.go
@@ -124,6 +124,9 @@
 
 // A Config configures load behavior.
 type Config struct {
+	// TODO: allow passing a cuecontext to be able to lookup and verify builtin
+	// packages at loading time.
+
 	// Context specifies the context for the load operation.
 	// If the context is cancelled, the loader may stop early
 	// and return an ErrCancelled error.
diff --git a/cue/testdata/packages/builtin.txtar b/cue/testdata/packages/builtin.txtar
index ac5f516..16f4f0f 100644
--- a/cue/testdata/packages/builtin.txtar
+++ b/cue/testdata/packages/builtin.txtar
@@ -5,21 +5,8 @@
 	y,
 ]
 -- out/eval --
-Errors:
-foo.0: cannot find package "x/y":
-    ./x.cue:4:2
-
-Result:
-(_|_){
-  // [eval]
-  foo: (_|_){
-    // [eval]
-    0: (_|_){
-      // [eval] foo.0: cannot find package "x/y":
-      //     ./x.cue:4:2
-    }
-  }
-}
+builtin package "x/y" undefined:
+    ./x.cue:1:8
 -- out/compile --
 --- x.cue
 {
diff --git a/internal/core/runtime/build.go b/internal/core/runtime/build.go
index da5634b..d3ecac7 100644
--- a/internal/core/runtime/build.go
+++ b/internal/core/runtime/build.go
@@ -131,8 +131,11 @@
 			return errors.Newf(spec.Pos(),
 				"package %q imported but not defined in %s",
 				info.ID, b.ImportPath)
+		} else if x.index.builtinPaths[info.ID] == nil {
+			return errors.Newf(spec.Pos(),
+				"builtin package %q undefined", info.ID)
 		}
-		return nil // TODO: check the builtin package exists here.
+		return nil
 	}
 
 	if v := x.getNodeFromInstance(pkg); v != nil {