Skip to content

Commit 2be277b

Browse files
denikIsaac
andcommitted
bundle: report a directory at a state path as "not a file"
The workspace exports a directory as a ZIP archive even with direct_download=true, so since the state read stopped calling get-status first, a directory there failed with a JSON parse error on the archive. Recognize the ZIP header and say what is wrong. The test server now exports directories the same way. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 79e17eb commit 2be277b

7 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bundle:
2+
name: directory-at-state-path-$UNIQUE_NAME
3+
4+
resources:
5+
jobs:
6+
foo:
7+
name: foo
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cloud = true
2+
EnvMatrix.DMS = ["", "true"]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
>>> musterr [CLI] bundle plan
3+
Error: reading resources.json: not a file: the workspace has a directory at this path
4+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
envsubst < databricks.yml.tmpl > databricks.yml
2+
3+
# Something else created a directory where the CLI keeps its state file.
4+
state_path=$($CLI bundle validate -o json | jq -r .workspace.state_path)
5+
$CLI workspace mkdirs "$state_path/resources.json"
6+
7+
cleanup() {
8+
$CLI workspace delete --recursive "$($CLI bundle validate -o json | jq -r .workspace.root_path)"
9+
rm -f "$OUT_REQUESTS"
10+
}
11+
trap cleanup EXIT
12+
13+
trace musterr $CLI bundle plan
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Exercises the real workspace's export of a directory, which returns a ZIP archive.
2+
Cloud = true

‎bundle/statemgmt/state_pull.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package statemgmt
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"errors"
@@ -28,6 +29,9 @@ import (
2829

2930
type AlwaysPull bool
3031

32+
// zipMagic starts every ZIP archive; a state file is JSON and never does.
33+
var zipMagic = []byte("PK\x03\x04")
34+
3135
type StateDesc struct {
3236
Serial int `json:"serial"`
3337
Lineage string `json:"lineage"`
@@ -125,6 +129,11 @@ func _filerRead(ctx context.Context, f filer.Filer, path string) (*StateDesc, er
125129
state := &StateDesc{}
126130
err = json.Unmarshal(content, state)
127131
if err != nil {
132+
// The workspace exports a directory as a ZIP archive rather than failing, so a directory
133+
// at the state path reads fine and only shows up here.
134+
if bytes.HasPrefix(content, zipMagic) {
135+
return nil, errors.New("not a file: the workspace has a directory at this path")
136+
}
128137
return nil, fmt.Errorf("parsing state: %w", err)
129138
}
130139

‎libs/testserver/fake_workspace.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,9 +845,27 @@ func (s *FakeWorkspace) WorkspaceMkdirs(request workspace.Mkdirs) {
845845

846846
func (s *FakeWorkspace) WorkspaceExport(path string) []byte {
847847
defer s.LockUnlock()()
848+
if _, isDir := s.directories[path]; isDir {
849+
return exportDirectory(path)
850+
}
848851
return s.files[path].Data
849852
}
850853

854+
// exportDirectory mimics the real API, which exports a directory as a ZIP archive even with
855+
// direct_download=true. Only the directory's own entry is included: callers just need to see
856+
// that they got an archive rather than a file.
857+
func exportDirectory(dirPath string) []byte {
858+
var buf bytes.Buffer
859+
zw := zip.NewWriter(&buf)
860+
if _, err := zw.Create(path.Base(dirPath) + "/"); err != nil {
861+
panic(err)
862+
}
863+
if err := zw.Close(); err != nil {
864+
panic(err)
865+
}
866+
return buf.Bytes()
867+
}
868+
851869
// WorkspaceDelete implements POST /api/2.0/workspace/delete. As in the real API, a
852870
// non-recursive delete of a directory that still has children fails instead of removing
853871
// it, which is what lets a caller delete a directory only if it is empty.

0 commit comments

Comments
 (0)