-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathtranslate_paths.go
More file actions
403 lines (348 loc) · 14.2 KB
/
Copy pathtranslate_paths.go
File metadata and controls
403 lines (348 loc) · 14.2 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
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
package mutator
import (
"context"
"errors"
"fmt"
"io/fs"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"github.com/databricks/cli/bundle/config/mutator/paths"
"github.com/databricks/cli/bundle/libraries"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/notebook"
)
// translateOptions control path translation behavior.
type translateOptions struct {
// Mode specifies how the path should be translated.
Mode paths.TranslateMode
// AllowPathOutsideSyncRoot can be set for paths that are not tied to the sync root path.
// This is the case for artifact paths, for example.
AllowPathOutsideSyncRoot bool
}
type ErrIsNotebook struct {
path string
}
func (err ErrIsNotebook) Error() string {
return fmt.Sprintf("file at %s is a notebook", err.path)
}
type ErrIsNotNotebook struct {
path string
}
func (err ErrIsNotNotebook) Error() string {
return fmt.Sprintf("file at %s is not a notebook", err.path)
}
// seenKey is the cache key for the seen map in translateContext.
// It includes both the local path and the translation mode to prevent
// cross-mode cache collisions (e.g. artifact vs. workspace path translations).
type seenKey struct {
path string
mode paths.TranslateMode
}
type translatePaths struct{}
type translatePathsDashboards struct{}
// TranslatePaths converts paths to local notebook files into paths in the workspace file system.
func TranslatePaths() bundle.Mutator {
return &translatePaths{}
}
// TranslatePathsDashboards converts paths to local dashboard files into paths in the workspace file system.
func TranslatePathsDashboards() bundle.Mutator {
return &translatePathsDashboards{}
}
func (m *translatePaths) Name() string {
return "TranslatePaths"
}
func (m *translatePathsDashboards) Name() string {
return "TranslatePathsDashboards"
}
// translateContext is a context for rewriting paths in a config.
// It is freshly instantiated on every mutator apply call.
// It provides access to the underlying bundle object such that
// it doesn't have to be passed around explicitly.
type translateContext struct {
b *bundle.Bundle
// seen is a map of (local path, translation mode) pairs to their corresponding remote paths.
// If a local path has already been successfully resolved for a given mode, we do not need to resolve it again.
seen map[seenKey]string
// remoteRoot is the root path of the remote workspace.
// It is equal to ${workspace.file_path} for regular deployments.
// It points to the source root path for source-linked deployments.
remoteRoot string
// skipLocalFileValidation makes path translation tolerant of missing local files.
// When set, paths are translated without verifying files exist on the local filesystem.
// This is used by config-remote-sync: a user may rename a resource's root folder
// in the workspace UI, and the updated path may not exist locally. Path translation
// is still needed to produce fully resolved paths for comparison with remote state,
// but local file validation would incorrectly fail.
skipLocalFileValidation bool
}
// rewritePath converts a given relative path from the loaded config to a new path based on the passed rewriting function
//
// It takes these arguments:
// - The context in which the function is called.
// - The argument `dir` is the directory relative to which the relative path should be interpreted.
// - The argument `input` is the relative path to rewrite.
// - The argument `opts` is a struct that specifies how the path should be rewritten.
// It contains a `Mode` field that specifies how the path should be rewritten.
//
// The function returns the rewritten path if successful, or an error if the path could not be rewritten.
// The returned path is an empty string if the path was not rewritten.
func (t *translateContext) rewritePath(
ctx context.Context,
dir string,
input string,
opts translateOptions,
) (string, error) {
// If the input is a local requirements file, we need to translate it to an absolute path.
var flag string
if reqPath, flagPrefix, ok := libraries.IsLocalPathInPipFlag(input); ok {
input = reqPath
flag = flagPrefix
}
// We assume absolute paths point to a location in the workspace
if path.IsAbs(input) {
return "", nil
}
url, err := url.Parse(input)
if err != nil {
return "", err
}
// If the file path has scheme, it's a full path and we don't need to transform it
if url.Scheme != "" {
return "", nil
}
// Local path is relative to the directory the resource was defined in.
localPath := filepath.Join(dir, input)
key := seenKey{path: localPath, mode: opts.Mode}
if interp, ok := t.seen[key]; ok {
return interp, nil
}
// Local path must be contained in the sync root.
// If it isn't, it won't be synchronized into the workspace.
localRelPath, err := filepath.Rel(t.b.SyncRootPath, localPath)
if err != nil {
return "", err
}
if !opts.AllowPathOutsideSyncRoot && !filepath.IsLocal(localRelPath) {
return "", fmt.Errorf("path %s is not contained in sync root path", localPath)
}
// Normalize paths to separated by forward slashes.
localPath = filepath.ToSlash(localPath)
localRelPath = filepath.ToSlash(localRelPath)
// Convert local path into workspace path via specified function.
var interp string
switch opts.Mode {
case paths.TranslateModeNotebook:
interp, err = t.translateNotebookPath(ctx, input, localPath, localRelPath)
case paths.TranslateModeFile:
interp, err = t.translateFilePath(ctx, input, localPath, localRelPath)
case paths.TranslateModeDirectory:
interp, err = t.translateDirectoryPath(ctx, input, localPath, localRelPath)
case paths.TranslateModeGlob:
interp, err = t.translateGlobPath(ctx, input, localPath, localRelPath)
case paths.TranslateModeLocalAbsoluteDirectory:
interp, err = t.translateLocalAbsoluteDirectoryPath(ctx, input, localPath, localRelPath)
case paths.TranslateModeLocalRelative:
interp, err = t.translateLocalRelativePath(ctx, input, localPath, localRelPath)
case paths.TranslateModeLocalRelativeWithPrefix:
interp, err = t.translateLocalRelativeWithPrefixPath(ctx, input, localPath, localRelPath)
case paths.TranslateModeEnvironmentPipFlag:
interp, err = t.translateFilePath(ctx, input, localPath, localRelPath)
// Add the flag prefix to the path to indicate it's a local file used in pip flags for environment dependencies.
interp = flag + " " + interp
default:
return "", fmt.Errorf("unsupported translate mode: %d", opts.Mode)
}
if err != nil {
return "", err
}
t.seen[key] = interp
return interp, nil
}
func (t *translateContext) translateNotebookPath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) {
if t.skipLocalFileValidation {
localRelPathNoExt := strings.TrimSuffix(localRelPath, path.Ext(localRelPath))
return path.Join(t.remoteRoot, localRelPathNoExt), nil
}
nb, _, err := notebook.DetectWithFS(t.b.SyncRoot, localRelPath)
if errors.Is(err, fs.ErrNotExist) {
if path.Ext(localFullPath) != notebook.ExtensionNone {
return "", fmt.Errorf("notebook %s not found", literal)
}
// Check whether a file with a notebook extension already exists. This
// way we can provide a more targeted error message.
for _, ext := range notebook.Extensions {
literalWithExt := literal + ext
localRelPathWithExt := localRelPath + ext
if _, err := fs.Stat(t.b.SyncRoot, localRelPathWithExt); err == nil {
return "", fmt.Errorf(`notebook %q not found. Did you mean %q?
Local notebook references are expected to contain one of the following
file extensions: [%s]`, literal, literalWithExt, strings.Join(notebook.Extensions, ", "))
}
}
// Return a generic error message if no matching possible file is found.
return "", fmt.Errorf(`notebook %q not found. Local notebook references are expected
to contain one of the following file extensions: [%s]`, literal, strings.Join(notebook.Extensions, ", "))
}
if err != nil {
return "", fmt.Errorf("unable to determine if %s is a notebook: %w", localFullPath, err)
}
if !nb {
return "", ErrIsNotNotebook{localFullPath}
}
// Upon import, notebooks are stripped of their extension.
localRelPathNoExt := strings.TrimSuffix(localRelPath, path.Ext(localRelPath))
return path.Join(t.remoteRoot, localRelPathNoExt), nil
}
func (t *translateContext) translateFilePath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) {
if t.skipLocalFileValidation {
return path.Join(t.remoteRoot, localRelPath), nil
}
nb, _, err := notebook.DetectWithFS(t.b.SyncRoot, localRelPath)
if errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("file %s not found", literal)
}
if err != nil {
return "", fmt.Errorf("unable to determine if %s is not a notebook: %w", filepath.FromSlash(localFullPath), err)
}
if nb {
return "", ErrIsNotebook{localFullPath}
}
return path.Join(t.remoteRoot, localRelPath), nil
}
func (t *translateContext) translateDirectoryPath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) {
if t.skipLocalFileValidation {
return path.Join(t.remoteRoot, localRelPath), nil
}
info, err := t.b.SyncRoot.Stat(localRelPath)
if err != nil {
return "", err
}
if !info.IsDir() {
return "", fmt.Errorf("%s is not a directory", filepath.FromSlash(localFullPath))
}
return path.Join(t.remoteRoot, localRelPath), nil
}
func (t *translateContext) translateGlobPath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) {
return path.Join(t.remoteRoot, localRelPath), nil
}
func (t *translateContext) translateLocalAbsoluteDirectoryPath(ctx context.Context, literal, localFullPath, _ string) (string, error) {
if t.skipLocalFileValidation {
return localFullPath, nil
}
info, err := os.Stat(filepath.FromSlash(localFullPath))
if errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("directory %s not found", literal)
}
if err != nil {
return "", fmt.Errorf("unable to determine if %s is a directory: %w", filepath.FromSlash(localFullPath), err)
}
if !info.IsDir() {
return "", fmt.Errorf("expected %s to be a directory but found a file", literal)
}
return localFullPath, nil
}
func (t *translateContext) translateLocalRelativePath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) {
return localRelPath, nil
}
func (t *translateContext) translateLocalRelativeWithPrefixPath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) {
if !strings.HasPrefix(localRelPath, ".") {
localRelPath = "./" + localRelPath
}
return localRelPath, nil
}
func (t *translateContext) rewriteValue(ctx context.Context, p dyn.Path, v dyn.Value, dir string, opts translateOptions) (dyn.Value, error) {
out, err := t.rewritePath(ctx, dir, v.MustString(), opts)
if err != nil {
if target, ok := errors.AsType[ErrIsNotebook](err); ok {
return dyn.InvalidValue, fmt.Errorf(`expected a file for "%s" but got a notebook: %w`, p, target)
}
if target, ok := errors.AsType[ErrIsNotNotebook](err); ok {
return dyn.InvalidValue, fmt.Errorf(`expected a notebook for "%s" but got a file: %w`, p, target)
}
return dyn.InvalidValue, err
}
// If the path was not rewritten, return the original value.
if out == "" {
return v, nil
}
return dyn.NewValue(out, v.Locations()), nil
}
func applyTranslations(ctx context.Context, b *bundle.Bundle, t *translateContext, translations []func(context.Context, dyn.Value) (dyn.Value, error)) diag.Diagnostics {
// Set the remote root to the sync root if source-linked deployment is enabled.
// Otherwise, set it to the workspace file path.
if config.IsExplicitlyEnabled(t.b.Config.Presets.SourceLinkedDeployment) {
t.remoteRoot = t.b.SyncRootPath
} else {
t.remoteRoot = t.b.Config.Workspace.FilePath
}
err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
var err error
for _, fn := range translations {
v, err = fn(ctx, v)
if err != nil {
return dyn.InvalidValue, err
}
}
return v, nil
})
return diag.FromErr(err)
}
func (m *translatePaths) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
t := &translateContext{
b: b,
seen: make(map[seenKey]string),
skipLocalFileValidation: b.SkipLocalFileValidation,
}
return applyTranslations(ctx, b, t, []func(context.Context, dyn.Value) (dyn.Value, error){
t.applyJobTranslations(paths.VisitJobPaths, false),
t.applyJobTranslations(paths.VisitJobLibrariesPaths, true),
t.applyPipelineTranslations(paths.VisitPipelinePaths, false),
t.applyPipelineTranslations(paths.VisitPipelineLibrariesPaths, true),
t.applyArtifactTranslations,
t.applyAppsTranslations,
})
}
func (m *translatePathsDashboards) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
t := &translateContext{
b: b,
seen: make(map[seenKey]string),
skipLocalFileValidation: b.SkipLocalFileValidation,
}
return applyTranslations(ctx, b, t, []func(context.Context, dyn.Value) (dyn.Value, error){
t.applyDashboardTranslations,
})
}
// gatherFallbackPaths collects the fallback paths for relative paths in the configuration.
// Read more about the motivation for this functionality in the "fallback" path translation tests.
func gatherFallbackPaths(v dyn.Value, typ string) (map[string]string, error) {
fallback := make(map[string]string)
pattern := dyn.NewPattern(dyn.Key("resources"), dyn.Key(typ), dyn.AnyKey())
// Previous behavior was to use a resource's location as the base path to resolve
// relative paths in its definition. With the introduction of [dyn.Value] throughout,
// we can use the location of the [dyn.Value] of the relative path itself.
//
// This is more flexible, as resources may have overrides that are not
// located in the same directory as the resource configuration file.
//
// To maintain backwards compatibility, we allow relative paths to be resolved using
// the original approach as fallback if the [dyn.Value] location cannot be resolved.
_, err := dyn.MapByPattern(v, pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
key := p[2].Key()
dir, err := locationDirectory(v.Location())
if err != nil {
return dyn.InvalidValue, fmt.Errorf("unable to determine directory for %s: %w", p, err)
}
fallback[key] = dir
return v, nil
})
if err != nil {
return nil, err
}
return fallback, nil
}