File: import.go

package info (click to toggle)
golang-github-cue-lang-cue 0.14.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,644 kB
  • sloc: makefile: 20; sh: 15
file content (542 lines) | stat: -rw-r--r-- 16,052 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Copyright 2018 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package load

import (
	"cmp"
	"fmt"
	"io"
	"io/fs"
	"os"
	pathpkg "path"
	"path/filepath"
	"slices"
	"strings"

	"cuelang.org/go/cue/ast"
	"cuelang.org/go/cue/build"
	"cuelang.org/go/cue/errors"
	"cuelang.org/go/cue/token"
	"cuelang.org/go/internal/filetypes"
	"cuelang.org/go/mod/module"
)

// importPkg returns details about the CUE package named by the import path,
// interpreting local import paths relative to l.cfg.Dir.
// If the path is a local import path naming a package that can be imported
// using a standard import path, the returned package will set p.ImportPath
// to that path.
//
// In the directory and ancestor directories up to including one with a
// cue.mod file, all .cue files are considered part of the package except for:
//
//   - files starting with _ or . (likely editor temporary files)
//   - files with build constraints not satisfied by the context
//
// If an error occurs, importPkg sets the error in the returned instance,
// which then may contain partial information.
//
// pkgName indicates which packages to load. It supports the following
// values:
//
//	""      the default package for the directory, if only one
//	        is present.
//	_       anonymous files (which may be marked with _)
//	*       all packages
func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
	retErr := func(errs errors.Error) []*build.Instance {
		// XXX: move this loop to ReportError
		for _, err := range errors.Errors(errs) {
			p.ReportError(err)
		}
		return []*build.Instance{p}
	}

	for _, item := range l.stk {
		if item == p.ImportPath {
			return retErr(&PackageError{Message: errors.NewMessagef("package import cycle not allowed")})
		}
	}
	l.stk.Push(p.ImportPath)
	defer l.stk.Pop()

	cfg := l.cfg
	ctxt := cfg.fileSystem

	if p.Err != nil {
		return []*build.Instance{p}
	}

	fp := newFileProcessor(cfg, p, l.tagger)

	if p.PkgName == "" {
		if l.cfg.Package == "*" {
			fp.allPackages = true
			p.PkgName = "_"
		} else {
			p.PkgName = l.cfg.Package
		}
	}
	if p.PkgName != "" {
		// If we have an explicit package name, we can ignore other packages.
		fp.ignoreOther = true
	}

	var dirs [][2]string
	genDir := GenPath(cfg.ModuleRoot)
	if strings.HasPrefix(p.Dir, genDir) {
		dirs = append(dirs, [2]string{genDir, p.Dir})
		// && p.PkgName != "_"
		for _, sub := range []string{"pkg", "usr"} {
			rel, err := filepath.Rel(genDir, p.Dir)
			if err != nil {
				// should not happen
				return retErr(errors.Wrapf(err, token.NoPos, "invalid path"))
			}
			base := filepath.Join(cfg.ModuleRoot, modDir, sub)
			dir := filepath.Join(base, rel)
			dirs = append(dirs, [2]string{base, dir})
		}
	} else {
		dirs = append(dirs, [2]string{cfg.ModuleRoot, p.Dir})
	}

	found := false
	for _, d := range dirs {
		info, err := ctxt.stat(d[1])
		if err == nil && info.IsDir() {
			found = true
			break
		}
	}

	if !found {
		return retErr(
			&PackageError{
				Message: errors.NewMessagef("cannot find package %q", p.DisplayPath),
			})
	}

	// This algorithm assumes that multiple directories within cue.mod/*/
	// have the same module scope and that there are no invalid modules.
	inModule := false // if pkg == "_"
	for _, d := range dirs {
		if l.cfg.findModRoot(d[1]) != "" {
			inModule = true
			break
		}
	}

	// Walk the parent directories up to the module root to add their files as well,
	// since a package foo/bar/baz inherits from parent packages foo/bar and foo.
	// See https://cuelang.org/docs/concept/modules-packages-instances/#instances.
	for _, d := range dirs {
		dir := filepath.Clean(d[1])
		for {
			sd, ok := l.dirCachedBuildFiles[dir]
			if !ok {
				sd = l.scanDir(dir)
				l.dirCachedBuildFiles[dir] = sd
			}
			if err := sd.err; err != nil {
				if errors.Is(err, fs.ErrNotExist) {
					break
				}
				return retErr(errors.Wrapf(err, token.NoPos, "import failed reading dir %v", dir))
			}
			for _, name := range sd.filenames {
				file, err := filetypes.ParseFileAndType(name, "", filetypes.Input)
				if err != nil {
					p.UnknownFiles = append(p.UnknownFiles, &build.File{
						Filename:      name,
						ExcludeReason: errors.Newf(token.NoPos, "unknown filetype"),
					})
				} else {
					fp.add(dir, file, 0)
				}
			}
			if p.PkgName == "" || !inModule || l.cfg.isModRoot(dir) || dir == d[0] {
				break
			}

			// From now on we just ignore files that do not belong to the same
			// package.
			fp.ignoreOther = true

			parent, _ := filepath.Split(dir)
			parent = filepath.Clean(parent)

			if parent == dir || len(parent) < len(d[0]) {
				break
			}
			dir = parent
		}
	}

	all := []*build.Instance{}

	for _, p := range fp.pkgs {
		impPath, err := addImportQualifier(importPath(p.ImportPath), p.PkgName)
		p.ImportPath = string(impPath)
		if err != nil {
			p.ReportError(errors.Promote(err, ""))
		}

		if len(p.BuildFiles) == 0 &&
			len(p.IgnoredFiles) == 0 &&
			len(p.OrphanedFiles) == 0 &&
			len(p.InvalidFiles) == 0 &&
			len(p.UnknownFiles) == 0 {
			// The package has no files in it. This can happen
			// when the default package added in newFileProcessor
			// doesn't have any associated files.
			continue
		}
		all = append(all, p)
		rewriteFiles(p, cfg.ModuleRoot, false)
		if errs := fp.finalize(p); errs != nil {
			p.ReportError(errs)
			return all
		}

		l.addFiles(p)
		_ = p.Complete()
	}
	slices.SortFunc(all, func(a, b *build.Instance) int {
		// Instances may share the same directory but have different package names.
		// Sort by directory first, then by package name.
		if c := cmp.Compare(a.Dir, b.Dir); c != 0 {
			return c
		}

		return cmp.Compare(a.PkgName, b.PkgName)
	})
	return all
}

func (l *loader) scanDir(dir string) cachedDirFiles {
	files, err := l.cfg.fileSystem.readDir(dir)
	if err != nil {
		return cachedDirFiles{
			err: err,
		}
	}
	filenames := make([]string, 0, len(files))
	for _, f := range files {
		if f.IsDir() {
			continue
		}
		name := f.Name()
		if name == "-" {
			// The name "-" has a special significance to the file types
			// logic, but only when specified directly on the command line.
			// We don't want an actual file named "-" to have special
			// significant, so avoid that by making sure we don't see a naked "-"
			// even when a file named "-" is present in a directory.
			name = "./-"
		}
		filenames = append(filenames, name)
	}
	return cachedDirFiles{
		filenames: filenames,
	}
}

func setFileSource(cfg *Config, f *build.File) error {
	if f.Source != nil {
		return nil
	}
	fullPath := f.Filename

	// If the input file is stdin or a non-regular file,
	// such as a named pipe or a device file, we can only read it once.
	// Given that later on we may consume the source multiple times,
	// such as first to only parse the imports and later to parse the whole file,
	// read the whole file here upfront and buffer the bytes.
	//
	// TODO(perf): this causes an upfront "stat" syscall for every input file,
	// which is wasteful given that in the majority of cases we deal with regular files.
	// Consider doing the buffering the first time we open the file later on.
	if fullPath == "-" {
		b, err := io.ReadAll(cfg.stdin())
		if err != nil {
			return errors.Newf(token.NoPos, "read stdin: %v", err)
		}
		f.Source = b
		return nil
	}

	if !filepath.IsAbs(fullPath) {
		fullPath = filepath.Join(cfg.Dir, fullPath)
		// Ensure that encoding.NewDecoder will work correctly.
		f.Filename = fullPath
	}
	if fi := cfg.fileSystem.getOverlay(fullPath); fi != nil {
		if fi.file != nil {
			f.Source = fi.file
		} else {
			f.Source = fi.contents
		}
		return nil
	}

	// Note that we do this after ensuring fullPath is absolute, and after checking
	// whether the overlay provides the source.
	info, err := os.Stat(fullPath)
	if err != nil {
		return err
	}
	if !info.Mode().IsRegular() {
		b, err := os.ReadFile(fullPath)
		if err != nil {
			return err
		}
		f.Source = b
		return nil
	}

	return nil
}

func (l *loader) loadFunc() build.LoadFunc {
	if l.cfg.SkipImports {
		return nil
	}
	return l._loadFunc
}

func (l *loader) _loadFunc(pos token.Pos, path string) *build.Instance {
	impPath := importPath(path)
	if isLocalImport(path) {
		return l.cfg.newErrInstance(errors.Newf(pos, "relative import paths not allowed (%q)", path))
	}

	if isStdlibPackage(path) {
		// It looks like a builtin.
		return nil
	}

	p := l.newInstance(pos, impPath)
	_ = l.importPkg(pos, p)
	return p
}

// newRelInstance returns a build instance from the given
// relative import path.
func (l *loader) newRelInstance(pos token.Pos, path, pkgName string) *build.Instance {
	if !isLocalImport(path) {
		panic(fmt.Errorf("non-relative import path %q passed to newRelInstance", path))
	}

	p := l.cfg.Context.NewInstance(path, nil)
	p.PkgName = pkgName
	p.DisplayPath = filepath.ToSlash(path)
	// p.ImportPath = string(dir) // compute unique ID.
	p.Root = l.cfg.ModuleRoot
	p.Module = l.cfg.Module
	p.ModuleFile = l.cfg.modFile

	var err errors.Error
	if path != cleanImport(path) {
		err = errors.Append(err, l.errPkgf(nil,
			"non-canonical import path: %q should be %q", path, pathpkg.Clean(path)))
	}

	dir := filepath.Join(l.cfg.Dir, filepath.FromSlash(path))
	if pkgPath, e := importPathFromAbsDir(l.cfg, dir, path); e != nil {
		// Detect later to keep error messages consistent.
	} else {
		// Add package qualifier if the configuration requires it.
		name := l.cfg.Package
		switch name {
		case "_", "*":
			name = ""
		}
		pkgPath, e := addImportQualifier(pkgPath, name)
		if e != nil {
			// Detect later to keep error messages consistent.
		} else {
			p.ImportPath = string(pkgPath)
		}
	}

	p.Dir = dir

	if filepath.IsAbs(path) || strings.HasPrefix(path, "/") {
		err = errors.Append(err, errors.Newf(pos,
			"absolute import path %q not allowed", path))
	}
	if err != nil {
		p.Err = errors.Append(p.Err, err)
		p.Incomplete = true
	}
	return p
}

func importPathFromAbsDir(c *Config, absDir string, origPath string) (importPath, error) {
	if c.ModuleRoot == "" {
		return "", fmt.Errorf("cannot determine import path for %q (root undefined)", origPath)
	}

	dir := filepath.Clean(absDir)
	if !strings.HasPrefix(dir, c.ModuleRoot) {
		return "", fmt.Errorf("cannot determine import path for %q (dir outside of root)", origPath)
	}

	pkg := filepath.ToSlash(dir[len(c.ModuleRoot):])
	switch {
	case strings.HasPrefix(pkg, "/cue.mod/"):
		pkg = pkg[len("/cue.mod/"):]
		if pkg == "" {
			return "", fmt.Errorf("invalid package %q (root of %s)", origPath, modDir)
		}

	case c.Module == "":
		return "", fmt.Errorf("cannot determine import path for %q (no module)", origPath)
	default:
		impPath := ast.ParseImportPath(c.Module)
		impPath.Path += pkg
		impPath.Qualifier = ""
		pkg = impPath.String()
	}
	return importPath(pkg), nil
}

func (l *loader) newInstance(pos token.Pos, p importPath) *build.Instance {
	dir, mv, modRoot, err := l.absDirFromImportPath(pos, p)
	i := l.cfg.Context.NewInstance(dir, nil)
	i.Err = errors.Append(i.Err, err)
	i.Dir = dir
	parts := ast.ParseImportPath(string(p))
	i.PkgName = parts.Qualifier
	if i.PkgName == "" {
		i.Err = errors.Append(i.Err, l.errPkgf([]token.Pos{pos}, "cannot determine package name for %q; set it explicitly with ':'", p))
	} else if i.PkgName == "_" {
		i.Err = errors.Append(i.Err, l.errPkgf([]token.Pos{pos}, "_ is not a valid import path qualifier in %q", p))
	}
	i.DisplayPath = string(p)
	i.ImportPath = string(p)
	if mv.Path() == "" {
		return i
	}
	mf, err1 := l.modFileCache.modFile(mv, modRoot)
	if err != nil {
		i.Err = errors.Append(i.Err, errors.Promote(err1, ""))
	}
	root, err1 := absPathForSourceLoc(modRoot)
	if err != nil {
		i.Err = errors.Append(i.Err, errors.Promote(err1, ""))
	} else {
		i.Root = root
	}
	i.Module = mv.Path()
	i.ModuleFile = mf

	return i
}

// absDirFromImportPath converts a giving import path to an absolute directory
// and a package name. The root directory must be set.
//
// The returned directory may not exist.
func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (dir string, mv module.Version, modRoot module.SourceLoc, _ errors.Error) {
	dir, mv, modRoot, err := l.absDirFromImportPath1(pos, p)
	if err != nil {
		// Any error trying to determine the package location
		// is a PackageError.
		return "", module.Version{}, module.SourceLoc{}, l.errPkgf([]token.Pos{pos}, "%s", err.Error())
	}
	return dir, mv, modRoot, nil
}

func (l *loader) absDirFromImportPath1(pos token.Pos, p importPath) (absDir string, mv module.Version, modRoot module.SourceLoc, err error) {
	failf := func(f string, a ...any) (string, module.Version, module.SourceLoc, error) {
		return "", module.Version{}, module.SourceLoc{}, fmt.Errorf(f, a...)
	}
	if p == "" {
		return failf("empty import path")
	}
	if l.cfg.ModuleRoot == "" {
		return failf("cannot import %q (root undefined)", p)
	}
	if isStdlibPackage(string(p)) {
		return failf("standard library import path %q cannot be imported as a CUE package", p)
	}
	if l.pkgs == nil {
		return failf("imports are unavailable because there is no cue.mod/module.cue file")
	}
	// Extract the package name.
	parts := ast.ParseImportPath(string(p))
	unqualified := parts.Unqualified().String()
	// TODO predicate registry-aware lookup on module.cue-declared CUE version?

	// Note: use the canonical form of the import path because
	// that's the form passed to [modpkgload.LoadPackages]
	// and hence it's available by that name via Pkg.
	pkg := l.pkgs.Pkg(parts.Canonical().String())
	// TODO(mvdan): using "unqualified" for the errors below doesn't seem right,
	// should we not be using either the original path or the canonical path?
	// The unqualified import path should only be used for filepath.FromSlash further below.
	if pkg == nil {
		return failf("no dependency found for package %q", unqualified)
	}
	if err := pkg.Error(); err != nil {
		return failf("cannot find package %q: %v", unqualified, err)
	}
	if mv := pkg.Mod(); mv.IsLocal() {
		// It's a local package that's present inside one or both of the gen, usr or pkg
		// directories. Even though modpkgload tells us exactly what those directories
		// are, the rest of the cue/load logic expects only a single directory for now,
		// so just use that.
		absDir = filepath.Join(GenPath(l.cfg.ModuleRoot), parts.Path)
	} else {
		locs := pkg.Locations()
		if len(locs) > 1 {
			return failf("package %q unexpectedly found in multiple locations", unqualified)
		}
		if len(locs) == 0 {
			return failf("no location found for package %q", unqualified)
		}
		var err error
		absDir, err = absPathForSourceLoc(locs[0])
		if err != nil {
			return failf("cannot determine source directory for package %q: %v", unqualified, err)
		}
	}
	return absDir, pkg.Mod(), pkg.ModRoot(), nil
}

func absPathForSourceLoc(loc module.SourceLoc) (string, error) {
	osfs, ok := loc.FS.(module.OSRootFS)
	if !ok {
		return "", fmt.Errorf("cannot get absolute path for FS of type %T", loc.FS)
	}
	osPath := osfs.OSRoot()
	if osPath == "" {
		return "", fmt.Errorf("cannot get absolute path for FS of type %T", loc.FS)
	}
	return filepath.Join(osPath, loc.Dir), nil
}

// isStdlibPackage reports whether pkgPath looks like
// an import from the standard library.
func isStdlibPackage(pkgPath string) bool {
	firstElem, _, _ := strings.Cut(pkgPath, "/")
	if firstElem == "" {
		return false // absolute paths like "/foo/bar"
	}
	// Paths like ".foo/bar", "./foo/bar", or "foo.com/bar" are not standard library import paths.
	return strings.IndexByte(firstElem, '.') == -1
}