...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package load
16
17 import (
18 "fmt"
19 "path/filepath"
20 "strings"
21
22 "cuelang.org/go/cue/build"
23 "cuelang.org/go/cue/errors"
24 "cuelang.org/go/cue/token"
25 )
26
27
28 type PackageError struct {
29 ImportStack []string
30 Pos token.Pos
31 errors.Message
32 IsImportCycle bool
33 }
34
35 func (p *PackageError) Position() token.Pos { return p.Pos }
36 func (p *PackageError) InputPositions() []token.Pos { return nil }
37 func (p *PackageError) Path() []string { return p.ImportStack }
38
39 func (p *PackageError) fillPos(cwd string, positions []token.Pos) {
40 if len(positions) > 0 && !p.Pos.IsValid() {
41 p.Pos = positions[0]
42 }
43 }
44
45
46 func (p *PackageError) Error() string {
47
48 if p.IsImportCycle {
49 return fmt.Sprintf("%s\npackage %s\n", p.Message, strings.Join(p.ImportStack, "\n\timports "))
50 }
51 if p.Pos.IsValid() {
52
53
54 return p.Pos.String() + ": " + p.Message.Error()
55 }
56 if len(p.ImportStack) == 0 {
57 return p.Message.Error()
58 }
59 return "package " + strings.Join(p.ImportStack, "\n\timports ") + ": " + p.Message.Error()
60 }
61
62
63
64
65 type NoFilesError struct {
66 Package *build.Instance
67
68 ignored bool
69 }
70
71 func (e *NoFilesError) Position() token.Pos { return token.NoPos }
72 func (e *NoFilesError) InputPositions() []token.Pos { return nil }
73 func (e *NoFilesError) Path() []string { return nil }
74
75
76 func (e *NoFilesError) Msg() (string, []interface{}) { return e.Error(), nil }
77
78
79 func (e *NoFilesError) Error() string {
80
81 dummy := 0
82 for _, f := range e.Package.IgnoredFiles {
83 if strings.HasPrefix(filepath.Base(f.Filename), "_") {
84 dummy++
85 }
86 }
87
88
89 path := e.Package.DisplayPath
90
91 if len(e.Package.IgnoredFiles) > dummy {
92 b := strings.Builder{}
93 b.WriteString("build constraints exclude all CUE files in ")
94 b.WriteString(path)
95 b.WriteString(":")
96
97 for _, f := range e.Package.IgnoredFiles {
98 b.WriteString("\n ")
99 b.WriteString(filepath.ToSlash(e.Package.RelPath(f)))
100 if f.ExcludeReason != nil {
101 b.WriteString(": ")
102 b.WriteString(f.ExcludeReason.Error())
103 }
104 }
105 return b.String()
106 }
107
108
109
110
111
112
113 return "no CUE files in " + path
114 }
115
116
117
118 type MultiplePackageError struct {
119 Dir string
120 Packages []string
121 Files []string
122 }
123
124 func (e *MultiplePackageError) Position() token.Pos { return token.NoPos }
125 func (e *MultiplePackageError) InputPositions() []token.Pos { return nil }
126 func (e *MultiplePackageError) Path() []string { return nil }
127
128 func (e *MultiplePackageError) Msg() (string, []interface{}) {
129 return "found packages %q (%s) and %s (%s) in %q", []interface{}{
130 e.Packages[0],
131 e.Files[0],
132 e.Packages[1],
133 e.Files[1],
134 e.Dir,
135 }
136 }
137
138 func (e *MultiplePackageError) Error() string {
139
140 format, args := e.Msg()
141 return fmt.Sprintf(format, args...)
142 }
143
View as plain text