-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathsourcemap.go
More file actions
216 lines (179 loc) · 4.94 KB
/
sourcemap.go
File metadata and controls
216 lines (179 loc) · 4.94 KB
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
package dalec
import (
"context"
"strings"
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/solver/errdefs"
"github.com/moby/buildkit/solver/pb"
)
// Unexported source map stored on Spec
type sourceMap struct {
filename string
language string
data []byte
pos *pb.Range
}
type sourceMapContext struct {
fileName string
data []byte
language string
}
type sourceMapContextKey struct{}
func sourceMapInfo(ctx context.Context) sourceMapContext {
v := ctx.Value(sourceMapContextKey{})
if v == nil {
return sourceMapContext{}
}
return v.(sourceMapContext)
}
// LocationConstraint returns an llb.ConstraintsOpt for the given yamlPath, or nil when not present
func (sm *sourceMap) GetLocation(state llb.State) (ret llb.ConstraintsOpt) {
return sm.getLocation(&state)
}
func (sm *sourceMap) GetRootLocation() llb.ConstraintsOpt {
return sm.getLocation(nil)
}
func (sm *sourceMap) getLocation(st *llb.State) llb.ConstraintsOpt {
if sm == nil {
return ConstraintsOptFunc(func(*llb.Constraints) {})
}
// If source state is given but it has no defined output, creating a source map from it will generate invalid LLM input,
// which then cannot be converted to state if needed, therefore omit adding it.
if st != nil && st.Output() == nil {
st = nil
}
sourceMap := llb.NewSourceMap(st, sm.filename, sm.language, sm.data)
return sourceMap.Location([]*pb.Range{sm.pos})
}
func (sm *sourceMap) GetErrdefsSource() *errdefs.Source {
if sm == nil {
return nil
}
return &errdefs.Source{
Info: &pb.SourceInfo{
Filename: sm.filename,
Data: sm.data,
Language: sm.language,
},
Ranges: []*pb.Range{sm.pos},
}
}
// nodeToRange converts an AST node to a protobuf Range
func nodeToRange(node ast.Node) *pb.Range {
token := node.GetToken()
pos := token.Position
start := &pb.Position{
Line: int32(pos.Line),
Character: int32(pos.Column),
}
walk := &endPosVisitor{}
ast.Walk(walk, node)
return &pb.Range{
Start: start,
End: walk.Position(),
}
}
type endPosVisitor struct {
endLine int
endChar int
}
func (v *endPosVisitor) Visit(n ast.Node) ast.Visitor {
if n == nil {
return nil
}
if n.Type() == ast.CommentType {
return v
}
pos := n.GetToken().Position
if pos.Line < v.endLine {
return v
}
v.endLine = pos.Line
v.endChar = pos.Column
if n.Type() != ast.StringType {
return v
}
setEndChar := func(ns string) {
newlines := strings.Count(ns, "\n")
v.endLine += newlines - 1
if newlines == 0 {
v.endChar = pos.Column + len(ns)
return
}
last := strings.LastIndex(ns, "\n")
if last != -1 {
v.endChar = len(ns) - last
}
}
// Work around panic calling `n.String()` on *ast.StringNode
// Ref: https://github.com/goccy/go-yaml/issues/797
// This only happens in some specific cases, not on every string node.
// In this case use the `Value` field from the string node.
// Its not as accurate since there's some extra formatting that `n.String()` does here,
// but at least in terms of getting the right line number this will be decent.
defer func() {
recover() //nolint:errcheck
setEndChar(n.(*ast.StringNode).Value)
}()
setEndChar(n.String())
return v
}
func (v *endPosVisitor) Position() *pb.Position {
return &pb.Position{
Line: int32(v.endLine),
Character: int32(v.endChar),
}
}
func newSourceMap(ctx context.Context, node ast.Node) *sourceMap {
smCtx := sourceMapInfo(ctx)
return &sourceMap{
filename: smCtx.fileName,
language: smCtx.language,
data: smCtx.data,
pos: nodeToRange(node),
}
}
// MergeSourceLocations multiple source locations into one location for each file found.
// It only merges locations for the provided opts, not all locations in the constraints.
// This is useful when there multiple ranges in a file for a single operation.
func MergeSourceLocations(opts ...llb.ConstraintsOpt) llb.ConstraintsOpt {
return ConstraintsOptFunc(func(c *llb.Constraints) {
if len(opts) == 0 {
return
}
var c2 llb.Constraints
for _, opt := range opts {
opt.SetConstraintsOption(&c2)
}
fileIdx := make(map[string]*llb.SourceLocation, len(c.SourceLocations))
for _, loc := range c2.SourceLocations {
v, ok := fileIdx[loc.SourceMap.Filename]
if !ok {
fileIdx[loc.SourceMap.Filename] = loc
continue
}
v.Ranges = append(v.Ranges, loc.Ranges...)
}
for _, loc := range fileIdx {
c.SourceLocations = append(c.SourceLocations, loc)
}
})
}
// sourceMappedValue is useful for unmarshalling core types (string, int, bool,
// etc) that you want to capture the source map for.
type sourceMappedValue[T any] struct {
Value T
sourceMap *sourceMap
}
func (s *sourceMappedValue[T]) UnmarshalYAML(ctx context.Context, node ast.Node) error {
type internal sourceMappedValue[T]
var i internal
if err := yaml.NodeToValue(node, &i.Value, decodeOpts(ctx)...); err != nil {
return err
}
s.Value = i.Value
s.sourceMap = newSourceMap(ctx, node)
return nil
}