cue/export: evaluate templates when exporting

Change-Id: I22d30dbe3a5525e50aeacdb1ced1eeea6c701797
Reviewed-on: https://cue-review.googlesource.com/c/1541
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cue/export.go b/cue/export.go
index f42d36c..c76ea8f 100644
--- a/cue/export.go
+++ b/cue/export.go
@@ -252,11 +252,19 @@
 				})
 			} // TODO: else record error
 		}
-		for _, a := range x.arcs {
-			obj.Elts = append(obj.Elts, &ast.Field{
+		for i, a := range x.arcs {
+			f := &ast.Field{
 				Label: p.label(a.feature),
-				Value: p.expr(a.v),
-			})
+			}
+			if p.mode != exportEval {
+				f.Value = p.expr(a.v)
+			} else if v := p.ctx.manifest(x.at(p.ctx, i)); isIncomplete(v) {
+				p := &exporter{p.ctx, exportRaw, p.stack}
+				f.Value = p.expr(a.v)
+			} else {
+				f.Value = p.expr(v)
+			}
+			obj.Elts = append(obj.Elts, f)
 		}
 
 		for _, c := range x.comprehensions {
diff --git a/cue/export_test.go b/cue/export_test.go
index d47e61c..7ac8ea4 100644
--- a/cue/export_test.go
+++ b/cue/export_test.go
@@ -197,24 +197,55 @@
 		raw:  true,
 		mode: exportEval,
 		in: `{
-			b: [{
-				<X>: int
-				f: 4 if a > 4
-			}][a]
-			a: int
-			c: *1 | 2
+			job <Name>: {
+				name:     Name
+				replicas: uint | *1
+				command:  string
+			}
+			
+			job list command: "ls"
+
+			job nginx: {
+				command:  "nginx"
+				replicas: 2
+			}
 		}`,
-		// reference to a must be redirected to outer a through alias
 		out: unindent(`
 		{
-			b: [{
-				<X>: int
-				"f": 4 if a > 4
-			}][a]
-			a: int
-			c: 1
+			job: {
+				list: {
+					name:     "list"
+					replicas: 1
+					command:  "ls"
+				}
+				nginx: {
+					name:     "nginx"
+					replicas: 2
+					command:  "nginx"
+				}
+			}
 		}`),
-	}}
+	}, {
+		raw:  true,
+		mode: exportEval,
+		in: `{
+				b: [{
+					<X>: int
+					f: 4 if a > 4
+				}][a]
+				a: int
+				c: *1 | 2
+			}`,
+		// reference to a must be redirected to outer a through alias
+		out: unindent(`
+			{
+				b: [{
+					<X>: int
+					"f": 4 if a > 4
+				}][a]
+				a: int
+				c: 1
+			}`)}}
 	for _, tc := range testCases {
 		t.Run("", func(t *testing.T) {
 			body := fmt.Sprintf("Test: %s", tc.in)