cmd/cue/cmd: fix newline discrepancies

- spurious newline at start of file
- missing newline at end of def (non-eval CUE output)

Change-Id: I51c4023bda49cd0f8d1ae82d70f0f0245f9d7a7a
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5644
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/testdata/script/def_basic.txt b/cmd/cue/cmd/testdata/script/def_basic.txt
index 7878ad3..9d15658 100644
--- a/cmd/cue/cmd/testdata/script/def_basic.txt
+++ b/cmd/cue/cmd/testdata/script/def_basic.txt
@@ -1,5 +1,8 @@
 cue def
-cmp stdout expect-stdout
+cmp stdout expect-stdout1
+
+cue def data.cue
+cmp stdout expect-stdout2
 
 -- schema.cue --
 // foo
@@ -8,16 +11,19 @@
 A :: {
     // a is an integer
     a: int
-    b: int
+    b: { c: int } // TODO: should we _not_ simplify?
 }
 
--- expect-stdout --
-
+-- data.cue --
+3
+-- expect-stdout2 --
+3
+-- expect-stdout1 --
 // foo
 package foo
 
 A :: {
 	// a is an integer
 	a: int
-	b: int
+	b: c: int
 }
diff --git a/cmd/cue/cmd/testdata/script/def_openapi.txt b/cmd/cue/cmd/testdata/script/def_openapi.txt
index 20fa1f8..76a46f6 100644
--- a/cmd/cue/cmd/testdata/script/def_openapi.txt
+++ b/cmd/cue/cmd/testdata/script/def_openapi.txt
@@ -164,7 +164,6 @@
 	}
 }
 -- expect-cue --
-
 // My OpenAPI
 package foo
 
diff --git a/cmd/cue/cmd/testdata/script/import_proto.txt b/cmd/cue/cmd/testdata/script/import_proto.txt
index d900034..7ea596f 100644
--- a/cmd/cue/cmd/testdata/script/import_proto.txt
+++ b/cmd/cue/cmd/testdata/script/import_proto.txt
@@ -210,7 +210,6 @@
 	}
 }
 -- expect-client_config_proto_gen.cue --
-
 // Describes the configuration state for the Mixer client library that's built into Envoy.
 package client
 
diff --git a/cue/format/format.go b/cue/format/format.go
index 7a4d557..b3c3c69 100644
--- a/cue/format/format.go
+++ b/cue/format/format.go
@@ -319,7 +319,7 @@
 	f.Print(cg)
 
 	printBlank := false
-	if cg.Doc {
+	if cg.Doc && len(f.output) > 0 {
 		f.Print(newline)
 		printBlank = true
 	}
diff --git a/encoding/protobuf/testdata/attributes.proto.out.cue b/encoding/protobuf/testdata/attributes.proto.out.cue
index d311b1b..e9fd95d 100644
--- a/encoding/protobuf/testdata/attributes.proto.out.cue
+++ b/encoding/protobuf/testdata/attributes.proto.out.cue
@@ -1,4 +1,3 @@
-
 // Copyright 2016 Istio Authors
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
diff --git a/encoding/protobuf/testdata/client_config.proto.out.cue b/encoding/protobuf/testdata/client_config.proto.out.cue
index 64c256f..8e12f61 100644
--- a/encoding/protobuf/testdata/client_config.proto.out.cue
+++ b/encoding/protobuf/testdata/client_config.proto.out.cue
@@ -1,4 +1,3 @@
-
 // Copyright 2017 Istio Authors
 //
 //   Licensed under the Apache License, Version 2.0 (the "License");
diff --git a/encoding/protobuf/testdata/gateway.proto.out.cue b/encoding/protobuf/testdata/gateway.proto.out.cue
index 2f1e5f2..25d80c5 100644
--- a/encoding/protobuf/testdata/gateway.proto.out.cue
+++ b/encoding/protobuf/testdata/gateway.proto.out.cue
@@ -1,4 +1,3 @@
-
 // Copyright 2019 CUE Authors
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
diff --git a/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/github.com/gogo/protobuf/gogoproto/gogo_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/github.com/gogo/protobuf/gogoproto/gogo_proto_gen.cue
index 1896f90..b0d8000 100644
--- a/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/github.com/gogo/protobuf/gogoproto/gogo_proto_gen.cue
+++ b/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/github.com/gogo/protobuf/gogoproto/gogo_proto_gen.cue
@@ -1,4 +1,3 @@
-
 // Protocol Buffers for Go with Gadgets
 //
 // Copyright (c) 2013, The GoGo Authors. All rights reserved.
diff --git a/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/google.golang.org/genproto/googleapis/rpc/status/status_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/google.golang.org/genproto/googleapis/rpc/status/status_proto_gen.cue
index baa5f6d..2a660eb 100644
--- a/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/google.golang.org/genproto/googleapis/rpc/status/status_proto_gen.cue
+++ b/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/google.golang.org/genproto/googleapis/rpc/status/status_proto_gen.cue
@@ -1,4 +1,3 @@
-
 // Copyright 2017 Google Inc.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
diff --git a/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue
index d311b1b..e9fd95d 100644
--- a/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue
+++ b/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue
@@ -1,4 +1,3 @@
-
 // Copyright 2016 Istio Authors
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
diff --git a/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/api_spec_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/api_spec_proto_gen.cue
index 810ffe5..b3aadd3 100644
--- a/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/api_spec_proto_gen.cue
+++ b/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/api_spec_proto_gen.cue
@@ -1,4 +1,3 @@
-
 // Copyright 2017 Istio Authors
 //
 //   Licensed under the Apache License, Version 2.0 (the "License");
diff --git a/internal/encoding/encoder.go b/internal/encoding/encoder.go
index 2098102..fbb019f 100644
--- a/internal/encoding/encoder.go
+++ b/internal/encoding/encoder.go
@@ -28,6 +28,7 @@
 	"cuelang.org/go/cue/format"
 	"cuelang.org/go/cue/token"
 	"cuelang.org/go/encoding/openapi"
+	"cuelang.org/go/internal"
 	"cuelang.org/go/internal/filetypes"
 	"cuelang.org/go/pkg/encoding/yaml"
 )
@@ -119,7 +120,9 @@
 				opts = append(opts, format.Simplify())
 			}
 
-			b, err := format.Node(n, opts...)
+			// Casting an ast.Expr to an ast.File ensures that it always ends
+			// with a newline.
+			b, err := format.Node(internal.ToFile(n), opts...)
 			if err != nil {
 				return err
 			}