@@ -21,6 +21,7 @@ import (
21
21
22
22
"cuelang.org/go/cue/build"
23
23
"cuelang.org/go/cue/errors"
24
+ "cuelang.org/go/cue/token"
24
25
)
25
26
26
27
const (
@@ -113,6 +114,16 @@ type Config struct {
113
114
// This is mostly used for bootstrapping.
114
115
StdRoot string
115
116
117
+ // Overlay provides a mapping of absolute file paths to file contents.
118
+ // If the file with the given path already exists, the parser will use the
119
+ // alternative file contents provided by the map.
120
+ //
121
+ // Overlays provide incomplete support for when a given file doesn't
122
+ // already exist on disk. See the package doc above for more details.
123
+ //
124
+ // If the value must be of type string, []byte, io.Reader, or *ast.File.
125
+ Overlay map [string ]Source
126
+
116
127
fileSystem
117
128
}
118
129
@@ -122,10 +133,19 @@ func (c Config) newInstance(path string) *build.Instance {
122
133
return i
123
134
}
124
135
125
- func (c Config ) newErrInstance (m * match , path string , err errors. Error ) * build.Instance {
136
+ func (c Config ) newErrInstance (m * match , path string , err error ) * build.Instance {
126
137
i := c .Context .NewInstance (path , nil )
127
138
i .DisplayPath = path
128
- i .ReportError (err )
139
+ switch x := err .(type ) {
140
+ case errors.Error :
141
+ i .ReportError (x )
142
+ case errors.List :
143
+ for _ , e := range x {
144
+ i .ReportError (e )
145
+ }
146
+ default :
147
+ i .ReportError (errors .Wrapf (err , token .NoPos , "instance" ))
148
+ }
129
149
return i
130
150
}
131
151
@@ -144,11 +164,17 @@ func (c Config) complete() (cfg *Config, err error) {
144
164
}
145
165
}
146
166
167
+ // TODO: we could populate this already with absolute file paths,
168
+ // but relative paths cannot be added. Consider what is reasonable.
169
+ if err := c .fileSystem .init (& c ); err != nil {
170
+ return nil , err
171
+ }
172
+
147
173
// TODO: determine root on a package basis. Maybe we even need a
148
174
// pkgname.cue.mod
149
175
// Look to see if there is a cue.mod.
150
176
if c .modRoot == "" {
151
- abs , err := findRoot (c .Dir )
177
+ abs , err := c . findRoot (c .Dir )
152
178
if err != nil {
153
179
// Not using modules: only consider the current directory.
154
180
c .modRoot = c .Dir
@@ -165,20 +191,21 @@ func (c Config) complete() (cfg *Config, err error) {
165
191
166
192
if c .cache == "" {
167
193
c .cache = filepath .Join (home (), defaultDir )
168
- // os.MkdirAll(c.Cache, 0755) // TODO: tools task
169
194
}
170
195
171
196
return & c , nil
172
197
}
173
198
174
- func findRoot (dir string ) (string , error ) {
199
+ func (c Config ) findRoot (dir string ) (string , error ) {
200
+ fs := & c .fileSystem
201
+
175
202
absDir , err := filepath .Abs (dir )
176
203
if err != nil {
177
204
return "" , err
178
205
}
179
206
abs := absDir
180
207
for {
181
- info , err := os . Stat (filepath .Join (abs , modFile ))
208
+ info , err := fs . stat (filepath .Join (abs , modFile ))
182
209
if err == nil && ! info .IsDir () {
183
210
return abs , nil
184
211
}
@@ -190,7 +217,7 @@ func findRoot(dir string) (string, error) {
190
217
}
191
218
abs = absDir
192
219
for {
193
- info , err := os . Stat (filepath .Join (abs , pkgDir ))
220
+ info , err := fs . stat (filepath .Join (abs , pkgDir ))
194
221
if err == nil && info .IsDir () {
195
222
return abs , nil
196
223
}
0 commit comments