forked from cue-lang/cue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
142 lines (124 loc) · 4.64 KB
/
errors.go
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
// 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 (
"fmt"
"path/filepath"
"strings"
"github.com/wylswz/cue-se/cue/build"
"github.com/wylswz/cue-se/cue/errors"
"github.com/wylswz/cue-se/cue/token"
)
// A PackageError describes an error loading information about a package.
type PackageError struct {
ImportStack []string // shortest path from package named on command line to this one
Pos token.Pos // position of error
errors.Message // the error itself
IsImportCycle bool // the error is an import cycle
}
func (p *PackageError) Position() token.Pos { return p.Pos }
func (p *PackageError) InputPositions() []token.Pos { return nil }
func (p *PackageError) Path() []string { return p.ImportStack }
func (p *PackageError) fillPos(cwd string, positions []token.Pos) {
if len(positions) > 0 && !p.Pos.IsValid() {
p.Pos = positions[0]
}
}
// TODO(localize)
func (p *PackageError) Error() string {
// Import cycles deserve special treatment.
if p.IsImportCycle {
return fmt.Sprintf("%s\npackage %s\n", p.Message, strings.Join(p.ImportStack, "\n\timports "))
}
if p.Pos.IsValid() {
// Omit import stack. The full path to the file where the error
// is the most important thing.
return p.Pos.String() + ": " + p.Message.Error()
}
if len(p.ImportStack) == 0 {
return p.Message.Error()
}
return "package " + strings.Join(p.ImportStack, "\n\timports ") + ": " + p.Message.Error()
}
// NoFilesError is the error used by Import to describe a directory
// containing no usable source files. (It may still contain
// tool files, files hidden by build tags, and so on.)
type NoFilesError struct {
Package *build.Instance
ignored bool // whether any Go files were ignored due to build tags
}
func (e *NoFilesError) Position() token.Pos { return token.NoPos }
func (e *NoFilesError) InputPositions() []token.Pos { return nil }
func (e *NoFilesError) Path() []string { return nil }
// TODO(localize)
func (e *NoFilesError) Msg() (string, []interface{}) { return e.Error(), nil }
// TODO(localize)
func (e *NoFilesError) Error() string {
// Count files beginning with _, which we will pretend don't exist at all.
dummy := 0
for _, f := range e.Package.IgnoredFiles {
if strings.HasPrefix(filepath.Base(f.Filename), "_") {
dummy++
}
}
// path := shortPath(e.Package.Root, e.Package.Dir)
path := e.Package.DisplayPath
if len(e.Package.IgnoredFiles) > dummy {
b := strings.Builder{}
b.WriteString("build constraints exclude all CUE files in ")
b.WriteString(path)
b.WriteString(":")
// CUE files exist, but they were ignored due to build constraints.
for _, f := range e.Package.IgnoredFiles {
b.WriteString("\n ")
b.WriteString(filepath.ToSlash(e.Package.RelPath(f)))
if f.ExcludeReason != nil {
b.WriteString(": ")
b.WriteString(f.ExcludeReason.Error())
}
}
return b.String()
}
// if len(e.Package.TestCUEFiles) > 0 {
// // Test CUE files exist, but we're not interested in them.
// // The double-negative is unfortunate but we want e.Package.Dir
// // to appear at the end of error message.
// return "no non-test CUE files in " + e.Package.Dir
// }
return "no CUE files in " + path
}
// MultiplePackageError describes an attempt to build a package composed of
// CUE files from different packages.
type MultiplePackageError struct {
Dir string // directory containing files
Packages []string // package names found
Files []string // corresponding files: Files[i] declares package Packages[i]
}
func (e *MultiplePackageError) Position() token.Pos { return token.NoPos }
func (e *MultiplePackageError) InputPositions() []token.Pos { return nil }
func (e *MultiplePackageError) Path() []string { return nil }
func (e *MultiplePackageError) Msg() (string, []interface{}) {
return "found packages %q (%s) and %s (%s) in %q", []interface{}{
e.Packages[0],
e.Files[0],
e.Packages[1],
e.Files[1],
e.Dir,
}
}
func (e *MultiplePackageError) Error() string {
// Error string limited to two entries for compatibility.
format, args := e.Msg()
return fmt.Sprintf(format, args...)
}