From 05e5f26ad36876895da6d526be86e197ea4068f8 Mon Sep 17 00:00:00 2001 From: "ilya.kuznetsov" Date: Mon, 23 Nov 2026 21:06:58 +0000 Subject: [PATCH 1/5] WIP: implement --- .nextchanges/bundles/bundle-file-origin.md | 1 + .../bundle/apps/app_yaml/out.app.yml.txt | 1 + .../resources/jobs/request-summary/output.txt | 2 +- .../sync/bundle_file_origin/databricks.yml | 2 + .../bundle/sync/bundle_file_origin/hello.py | 1 + .../sync/bundle_file_origin/out.test.toml | 2 + .../bundle/sync/bundle_file_origin/output.txt | 36 +++++++++++++ .../bundle/sync/bundle_file_origin/script | 13 +++++ .../bundle/sync/bundle_file_origin/test.toml | 1 + acceptance/bundle/sync/output.txt | 6 ++- .../upload/internal_server_error/output.txt | 2 +- acceptance/bundle/upload/timeout/output.txt | 6 +-- .../simple/out.requests.deploy.json | 1 + .../validate/sync_patterns/out.sync.txt | 1 + bundle/deploy/files/sync.go | 13 +++++ bundle/deploy/files/sync_test.go | 53 +++++++++++++++++++ libs/filer/workspace_files_client.go | 23 ++++++++ libs/filer/workspace_files_client_test.go | 43 +++++++++++++++ libs/sync/sync.go | 9 +++- 19 files changed, 209 insertions(+), 7 deletions(-) create mode 100644 .nextchanges/bundles/bundle-file-origin.md create mode 100644 acceptance/bundle/sync/bundle_file_origin/databricks.yml create mode 100644 acceptance/bundle/sync/bundle_file_origin/hello.py create mode 100644 acceptance/bundle/sync/bundle_file_origin/out.test.toml create mode 100644 acceptance/bundle/sync/bundle_file_origin/output.txt create mode 100644 acceptance/bundle/sync/bundle_file_origin/script create mode 100644 acceptance/bundle/sync/bundle_file_origin/test.toml create mode 100644 bundle/deploy/files/sync_test.go diff --git a/.nextchanges/bundles/bundle-file-origin.md b/.nextchanges/bundles/bundle-file-origin.md new file mode 100644 index 00000000000..a32cb06d9f7 --- /dev/null +++ b/.nextchanges/bundles/bundle-file-origin.md @@ -0,0 +1 @@ +* `bundle deploy` and `bundle sync` now tag the files they upload as deployment copies, so the workspace can tell them apart from a bundle's source files. diff --git a/acceptance/bundle/apps/app_yaml/out.app.yml.txt b/acceptance/bundle/apps/app_yaml/out.app.yml.txt index 29cb7da70fa..ebe0928b2c9 100644 --- a/acceptance/bundle/apps/app_yaml/out.app.yml.txt +++ b/acceptance/bundle/apps/app_yaml/out.app.yml.txt @@ -2,6 +2,7 @@ "method": "POST", "path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/app/app.yml", "q": { + "bundleFileOrigin": "DEPLOYMENT_COPY", "overwrite": "true" }, "raw_body": "command:\n - python\n - app.py\n" diff --git a/acceptance/bundle/resources/jobs/request-summary/output.txt b/acceptance/bundle/resources/jobs/request-summary/output.txt index a05f10b342f..993864c8554 100644 --- a/acceptance/bundle/resources/jobs/request-summary/output.txt +++ b/acceptance/bundle/resources/jobs/request-summary/output.txt @@ -28,7 +28,7 @@ GET /api/2.0/workspace/get-status path=/Workspace/Users/[USERNAME]/.bundle/reque GET /api/2.0/workspace/get-status path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock return_export_info=true GET /api/2.0/workspace/get-status path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock return_export_info=true GET /api/2.0/workspace/get-status path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock return_export_info=true -POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/request-summary/default/files/databricks.yml overwrite=true +POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/request-summary/default/files/databricks.yml bundleFileOrigin=DEPLOYMENT_COPY overwrite=true POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock overwrite=false POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock overwrite=false POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deployment.json overwrite=true diff --git a/acceptance/bundle/sync/bundle_file_origin/databricks.yml b/acceptance/bundle/sync/bundle_file_origin/databricks.yml new file mode 100644 index 00000000000..576d7a9ef25 --- /dev/null +++ b/acceptance/bundle/sync/bundle_file_origin/databricks.yml @@ -0,0 +1,2 @@ +bundle: + name: test-bundle diff --git a/acceptance/bundle/sync/bundle_file_origin/hello.py b/acceptance/bundle/sync/bundle_file_origin/hello.py new file mode 100644 index 00000000000..11b15b1a458 --- /dev/null +++ b/acceptance/bundle/sync/bundle_file_origin/hello.py @@ -0,0 +1 @@ +print("hello") diff --git a/acceptance/bundle/sync/bundle_file_origin/out.test.toml b/acceptance/bundle/sync/bundle_file_origin/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/sync/bundle_file_origin/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/sync/bundle_file_origin/output.txt b/acceptance/bundle/sync/bundle_file_origin/output.txt new file mode 100644 index 00000000000..294719392e4 --- /dev/null +++ b/acceptance/bundle/sync/bundle_file_origin/output.txt @@ -0,0 +1,36 @@ + +=== Deploy tags the bundle files it uploads, not its state files + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Files: 2 uploaded, 0 deleted +Resources: 0 created, 0 changed, 0 deleted, 0 unchanged + +>>> print_requests.py --summary --sort //default/files/ //state/metadata.json +POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/databricks.yml bundleFileOrigin=DEPLOYMENT_COPY overwrite=true +POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/hello.py bundleFileOrigin=DEPLOYMENT_COPY overwrite=true +POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/metadata.json overwrite=true + +=== Bundle sync writes to the same file_path, so it tags them too + +>>> [CLI] bundle sync --full +Action: PUT: databricks.yml, hello.py +Initial Sync Complete +Uploaded databricks.yml +Uploaded hello.py + +>>> print_requests.py --summary --sort //api/2.0/workspace-files/import-file +POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/databricks.yml bundleFileOrigin=DEPLOYMENT_COPY overwrite=true +POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/hello.py bundleFileOrigin=DEPLOYMENT_COPY overwrite=true + +=== A plain sync does not tag its uploads + +>>> MSYS_NO_PATHCONV=1 [CLI] sync . /Users/[USERNAME]/plain-sync +Action: PUT: databricks.yml, hello.py +Initial Sync Complete +Uploaded databricks.yml +Uploaded hello.py + +>>> print_requests.py --summary --sort //api/2.0/workspace-files/import-file +POST /api/2.0/workspace-files/import-file/Users/[USERNAME]/plain-sync/databricks.yml overwrite=true +POST /api/2.0/workspace-files/import-file/Users/[USERNAME]/plain-sync/hello.py overwrite=true diff --git a/acceptance/bundle/sync/bundle_file_origin/script b/acceptance/bundle/sync/bundle_file_origin/script new file mode 100644 index 00000000000..8a5db36787c --- /dev/null +++ b/acceptance/bundle/sync/bundle_file_origin/script @@ -0,0 +1,13 @@ +title "Deploy tags the bundle files it uploads, not its state files" +trace $CLI bundle deploy +# Deployment history (DMS=true) also uploads resources.json, so only metadata.json stands for the state files. +trace print_requests.py --summary --sort //default/files/ //state/metadata.json + +title "Bundle sync writes to the same file_path, so it tags them too" +trace $CLI bundle sync --full | sort +trace print_requests.py --summary --sort //api/2.0/workspace-files/import-file + +title "A plain sync does not tag its uploads" +# Prevent Git Bash on Windows from converting the /Users/... sync target into a Windows path. +trace MSYS_NO_PATHCONV=1 $CLI sync . /Users/$CURRENT_USER_NAME/plain-sync | sort +trace print_requests.py --summary --sort //api/2.0/workspace-files/import-file diff --git a/acceptance/bundle/sync/bundle_file_origin/test.toml b/acceptance/bundle/sync/bundle_file_origin/test.toml new file mode 100644 index 00000000000..159efe02696 --- /dev/null +++ b/acceptance/bundle/sync/bundle_file_origin/test.toml @@ -0,0 +1 @@ +RecordRequests = true diff --git a/acceptance/bundle/sync/output.txt b/acceptance/bundle/sync/output.txt index 276972e0d5d..2b6cd8e61f4 100644 --- a/acceptance/bundle/sync/output.txt +++ b/acceptance/bundle/sync/output.txt @@ -1,8 +1,12 @@ >>> [CLI] bundle sync --output text -Action: PUT: .gitignore, databricks.yml, dryrun/databricks.yml, dryrun/out.test.toml, ignored-folder/folder1/script.py, ignored-folder/script.py, replace-node-type/bom.sql, replace-node-type/databricks.yml.tmpl, replace-node-type/out.test.toml, replace-node-type/plain.sql, upload-504/databricks.yml, upload-504/out.test.toml, zip-extract/archive.zip, zip-extract/archive_v2.zip, zip-extract/databricks.yml.tmpl, zip-extract/out.test.toml +Action: PUT: .gitignore, bundle_file_origin/databricks.yml, bundle_file_origin/hello.py, bundle_file_origin/out.test.toml, databricks.yml, dryrun/databricks.yml, dryrun/out.test.toml, ignored-folder/folder1/script.py, ignored-folder/script.py, replace-node-type/bom.sql, replace-node-type/databricks.yml.tmpl, replace-node-type/out.test.toml, replace-node-type/plain.sql, upload-504/databricks.yml, upload-504/out.test.toml, zip-extract/archive.zip, zip-extract/archive_v2.zip, zip-extract/databricks.yml.tmpl, zip-extract/out.test.toml Initial Sync Complete Uploaded .gitignore +Uploaded bundle_file_origin +Uploaded bundle_file_origin/databricks.yml +Uploaded bundle_file_origin/hello.py +Uploaded bundle_file_origin/out.test.toml Uploaded databricks.yml Uploaded dryrun Uploaded dryrun/databricks.yml diff --git a/acceptance/bundle/upload/internal_server_error/output.txt b/acceptance/bundle/upload/internal_server_error/output.txt index d75ee2a71b1..3a4f21ea673 100644 --- a/acceptance/bundle/upload/internal_server_error/output.txt +++ b/acceptance/bundle/upload/internal_server_error/output.txt @@ -3,7 +3,7 @@ Warn: retrying after 500 Internal Server Error from POST /api/2.0/workspace-file Warn: retrying after 500 Internal Server Error from POST /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/file_to_upload.txt Error: Fault injected by test. (500 INJECTED) -Endpoint: POST [DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true +Endpoint: POST [DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true&bundleFileOrigin=DEPLOYMENT_COPY HTTP Status: 500 Internal Server Error API error_code: INJECTED API message: Fault injected by test. diff --git a/acceptance/bundle/upload/timeout/output.txt b/acceptance/bundle/upload/timeout/output.txt index 26b3b8b8bae..3df896b23ca 100644 --- a/acceptance/bundle/upload/timeout/output.txt +++ b/acceptance/bundle/upload/timeout/output.txt @@ -1,7 +1,7 @@ Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... -Warn: retrying after Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true": request timed out after 5s of inactivity -Warn: retrying after Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true": request timed out after 5s of inactivity -Error: Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true": request timed out after 5s of inactivity +Warn: retrying after Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true&bundleFileOrigin=DEPLOYMENT_COPY": request timed out after 5s of inactivity +Warn: retrying after Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true&bundleFileOrigin=DEPLOYMENT_COPY": request timed out after 5s of inactivity +Error: Post "[DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Ftest-bundle%2Fdefault%2Ffiles%2Ffile_to_upload.txt?overwrite=true&bundleFileOrigin=DEPLOYMENT_COPY": request timed out after 5s of inactivity /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/file_to_upload.txt /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/file_to_upload.txt diff --git a/acceptance/bundle/user_agent/simple/out.requests.deploy.json b/acceptance/bundle/user_agent/simple/out.requests.deploy.json index aaca239572d..79fc8c8aa0e 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.deploy.json +++ b/acceptance/bundle/user_agent/simple/out.requests.deploy.json @@ -132,6 +132,7 @@ "method": "POST", "path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/empty.py", "q": { + "bundleFileOrigin": "DEPLOYMENT_COPY", "overwrite": "true" } } diff --git a/acceptance/bundle/validate/sync_patterns/out.sync.txt b/acceptance/bundle/validate/sync_patterns/out.sync.txt index ac5aba9ab29..dff8e130c30 100644 --- a/acceptance/bundle/validate/sync_patterns/out.sync.txt +++ b/acceptance/bundle/validate/sync_patterns/out.sync.txt @@ -2,6 +2,7 @@ "method": "POST", "path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files/conf/dir/test.yml", "q": { + "bundleFileOrigin": "DEPLOYMENT_COPY", "overwrite": "true" } } diff --git a/bundle/deploy/files/sync.go b/bundle/deploy/files/sync.go index 152df0f3692..45dcf6888bf 100644 --- a/bundle/deploy/files/sync.go +++ b/bundle/deploy/files/sync.go @@ -3,8 +3,10 @@ package files import ( "context" "fmt" + "path" "github.com/databricks/cli/bundle" + "github.com/databricks/cli/libs/dbr" "github.com/databricks/cli/libs/sync" ) @@ -55,6 +57,8 @@ func GetSyncOptions(ctx context.Context, b *bundle.Bundle) (*sync.SyncOptions, e SnapshotBasePath: cacheDir, WorkspaceClient: b.WorkspaceClient(ctx), + + DeploymentCopy: !writesOntoSource(ctx, b), } if b.Config.Workspace.CurrentUser != nil { @@ -63,3 +67,12 @@ func GetSyncOptions(ctx context.Context, b *bundle.Bundle) (*sync.SyncOptions, e return opts, nil } + +// writesOntoSource reports whether file_path is the sync root itself, as in a +// deploy from a workspace folder that points file_path at that folder. Those +// files are the bundle's source, not a deployment copy. +// The paths are compared first so that contexts without runtime detection, +// such as unit tests, do not reach dbr.RunsOnRuntime. +func writesOntoSource(ctx context.Context, b *bundle.Bundle) bool { + return path.Clean(b.Config.Workspace.FilePath) == path.Clean(b.SyncRootPath) && dbr.RunsOnRuntime(ctx) +} diff --git a/bundle/deploy/files/sync_test.go b/bundle/deploy/files/sync_test.go new file mode 100644 index 00000000000..399618c5fc7 --- /dev/null +++ b/bundle/deploy/files/sync_test.go @@ -0,0 +1,53 @@ +package files + +import ( + "testing" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/libs/dbr" + "github.com/stretchr/testify/assert" +) + +func TestWritesOntoSource(t *testing.T) { + for _, tc := range []struct { + name string + isDbr bool + syncRootPath string + filePath string + expect bool + }{ + { + name: "file_path is the workspace sync root", + isDbr: true, + syncRootPath: "/Workspace/Users/me/my_bundle", + filePath: "/Workspace/Users/me/my_bundle/", + expect: true, + }, + { + name: "default file_path from the workspace", + isDbr: true, + syncRootPath: "/Workspace/Users/me/my_bundle", + filePath: "/Workspace/Users/me/.bundle/my_bundle/default/files", + expect: false, + }, + { + name: "same path outside the runtime", + isDbr: false, + syncRootPath: "/Workspace/Users/me/my_bundle", + filePath: "/Workspace/Users/me/my_bundle", + expect: false, + }, + } { + t.Run(tc.name, func(t *testing.T) { + ctx := dbr.MockRuntime(t.Context(), dbr.Environment{IsDbr: tc.isDbr}) + b := &bundle.Bundle{ + SyncRootPath: tc.syncRootPath, + Config: config.Root{ + Workspace: config.Workspace{FilePath: tc.filePath}, + }, + } + assert.Equal(t, tc.expect, writesOntoSource(ctx, b)) + }) + } +} diff --git a/libs/filer/workspace_files_client.go b/libs/filer/workspace_files_client.go index 30ca51f03d9..cf1a2d2fd5d 100644 --- a/libs/filer/workspace_files_client.go +++ b/libs/filer/workspace_files_client.go @@ -122,8 +122,15 @@ type WorkspaceFilesClient struct { // File operations will be relative to this path. root WorkspaceRootPath + + // Sent with every upload so the workspace knows where the file came from. + bundleFileOrigin string } +// BundleFileOriginDeploymentCopy marks a file written by a bundle deployment, +// so the workspace does not treat a deployed databricks.yml as a bundle's source. +const BundleFileOriginDeploymentCopy = "DEPLOYMENT_COPY" + // workspaceIDHeaders returns the workspace routing header map for outbound // API calls, or nil if the workspace client is unset. Wraps the shared // auth.WorkspaceIDHeaders helper with a nil-safe workspaceClient guard @@ -137,6 +144,16 @@ func (w *WorkspaceFilesClient) workspaceIDHeaders() map[string]string { } func NewWorkspaceFilesClient(w *databricks.WorkspaceClient, root string) (Filer, error) { + return newWorkspaceFilesClient(w, root, "") +} + +// NewWorkspaceFilesClientForBundleDeploy returns a client that tags every +// upload as a bundle deployment copy. +func NewWorkspaceFilesClientForBundleDeploy(w *databricks.WorkspaceClient, root string) (Filer, error) { + return newWorkspaceFilesClient(w, root, BundleFileOriginDeploymentCopy) +} + +func newWorkspaceFilesClient(w *databricks.WorkspaceClient, root, bundleFileOrigin string) (Filer, error) { apiClient, err := client.New(w.Config) if err != nil { return nil, err @@ -147,6 +164,8 @@ func NewWorkspaceFilesClient(w *databricks.WorkspaceClient, root string) (Filer, apiClient: apiClient, root: NewWorkspaceRootPath(root), + + bundleFileOrigin: bundleFileOrigin, }, nil } @@ -163,6 +182,10 @@ func (w *WorkspaceFilesClient) Write(ctx context.Context, name string, reader io url.PathEscape(strings.TrimLeft(absPath, "/")), overwrite, ) + // Workspaces that do not know the parameter ignore it. + if w.bundleFileOrigin != "" { + urlPath += "&bundleFileOrigin=" + url.QueryEscape(w.bundleFileOrigin) + } // Buffer the file contents because we may need to retry below and we cannot read twice. body, err := io.ReadAll(reader) diff --git a/libs/filer/workspace_files_client_test.go b/libs/filer/workspace_files_client_test.go index 2915af60436..c7de6592fd6 100644 --- a/libs/filer/workspace_files_client_test.go +++ b/libs/filer/workspace_files_client_test.go @@ -3,6 +3,8 @@ package filer import ( "encoding/json" "io/fs" + "net/url" + "strings" "testing" "time" @@ -181,3 +183,44 @@ func TestWorkspaceFilesClientStatNotFound(t *testing.T) { err := statWithError(t, 404, "RESOURCE_DOES_NOT_EXIST") assert.ErrorIs(t, err, fs.ErrNotExist) } + +func TestWorkspaceFilesClientWriteBundleFileOrigin(t *testing.T) { + for _, tc := range []struct { + name string + newFn func(*databricks.WorkspaceClient, string) (Filer, error) + expect url.Values + }{ + { + name: "default", + newFn: NewWorkspaceFilesClient, + expect: url.Values{"overwrite": {"true"}}, + }, + { + name: "bundle deploy", + newFn: NewWorkspaceFilesClientForBundleDeploy, + expect: url.Values{"overwrite": {"true"}, "bundleFileOrigin": {"DEPLOYMENT_COPY"}}, + }, + } { + t.Run(tc.name, func(t *testing.T) { + var query url.Values + server := testserver.New(t) + server.Handle("POST", "/api/2.0/workspace-files/import-file/{path...}", func(req testserver.Request) any { + query = req.URL.Query() + return "" + }) + + client, err := databricks.NewWorkspaceClient(&databricks.Config{ + Host: server.URL, + Token: "testtoken", + }) + require.NoError(t, err) + + f, err := tc.newFn(client, "/test") + require.NoError(t, err) + + err = f.Write(t.Context(), "databricks.yml", strings.NewReader("bundle: {}"), OverwriteIfExists) + require.NoError(t, err) + assert.Equal(t, tc.expect, query) + }) + } +} diff --git a/libs/sync/sync.go b/libs/sync/sync.go index 1e31b85481f..223dd8c302c 100644 --- a/libs/sync/sync.go +++ b/libs/sync/sync.go @@ -52,6 +52,9 @@ type SyncOptions struct { DryRun bool NoValidateRemotePath bool + + // Tag uploads as bundle deployment copies. + DeploymentCopy bool } type Sync struct { @@ -115,7 +118,11 @@ func New(ctx context.Context, opts SyncOptions) (*Sync, error) { } } - workspaceFiler, err := filer.NewWorkspaceFilesClient(opts.WorkspaceClient, opts.RemotePath) + newWorkspaceFiler := filer.NewWorkspaceFilesClient + if opts.DeploymentCopy { + newWorkspaceFiler = filer.NewWorkspaceFilesClientForBundleDeploy + } + workspaceFiler, err := newWorkspaceFiler(opts.WorkspaceClient, opts.RemotePath) if err != nil { return nil, err } From 486e0fdae5d47671cf9cbd54b8dd5ea9870215e6 Mon Sep 17 00:00:00 2001 From: "ilya.kuznetsov" Date: Mon, 23 Nov 2026 21:13:01 +0000 Subject: [PATCH 2/5] WIP: verify --- libs/filer/workspace_files_client.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/libs/filer/workspace_files_client.go b/libs/filer/workspace_files_client.go index cf1a2d2fd5d..dd7534e7dc8 100644 --- a/libs/filer/workspace_files_client.go +++ b/libs/filer/workspace_files_client.go @@ -123,14 +123,11 @@ type WorkspaceFilesClient struct { // File operations will be relative to this path. root WorkspaceRootPath - // Sent with every upload so the workspace knows where the file came from. - bundleFileOrigin string + // Tag every upload as written by a bundle deployment, so the workspace + // does not treat a deployed databricks.yml as a bundle's source. + deploymentCopy bool } -// BundleFileOriginDeploymentCopy marks a file written by a bundle deployment, -// so the workspace does not treat a deployed databricks.yml as a bundle's source. -const BundleFileOriginDeploymentCopy = "DEPLOYMENT_COPY" - // workspaceIDHeaders returns the workspace routing header map for outbound // API calls, or nil if the workspace client is unset. Wraps the shared // auth.WorkspaceIDHeaders helper with a nil-safe workspaceClient guard @@ -144,16 +141,16 @@ func (w *WorkspaceFilesClient) workspaceIDHeaders() map[string]string { } func NewWorkspaceFilesClient(w *databricks.WorkspaceClient, root string) (Filer, error) { - return newWorkspaceFilesClient(w, root, "") + return newWorkspaceFilesClient(w, root, false) } // NewWorkspaceFilesClientForBundleDeploy returns a client that tags every // upload as a bundle deployment copy. func NewWorkspaceFilesClientForBundleDeploy(w *databricks.WorkspaceClient, root string) (Filer, error) { - return newWorkspaceFilesClient(w, root, BundleFileOriginDeploymentCopy) + return newWorkspaceFilesClient(w, root, true) } -func newWorkspaceFilesClient(w *databricks.WorkspaceClient, root, bundleFileOrigin string) (Filer, error) { +func newWorkspaceFilesClient(w *databricks.WorkspaceClient, root string, deploymentCopy bool) (Filer, error) { apiClient, err := client.New(w.Config) if err != nil { return nil, err @@ -165,7 +162,7 @@ func newWorkspaceFilesClient(w *databricks.WorkspaceClient, root, bundleFileOrig root: NewWorkspaceRootPath(root), - bundleFileOrigin: bundleFileOrigin, + deploymentCopy: deploymentCopy, }, nil } @@ -183,8 +180,8 @@ func (w *WorkspaceFilesClient) Write(ctx context.Context, name string, reader io overwrite, ) // Workspaces that do not know the parameter ignore it. - if w.bundleFileOrigin != "" { - urlPath += "&bundleFileOrigin=" + url.QueryEscape(w.bundleFileOrigin) + if w.deploymentCopy { + urlPath += "&bundleFileOrigin=DEPLOYMENT_COPY" } // Buffer the file contents because we may need to retry below and we cannot read twice. From 27be6ea4cb7e5b8353eca846a8f2458d4dc90d4c Mon Sep 17 00:00:00 2001 From: "ilya.kuznetsov" Date: Mon, 23 Nov 2026 21:22:09 +0000 Subject: [PATCH 3/5] WIP: tag library uploads too --- acceptance/bundle/deploy/readplan/whl-upload/output.txt | 2 ++ acceptance/bundle/libraries/outside_of_bundle_root/output.txt | 1 + bundle/libraries/filer_workspace.go | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/acceptance/bundle/deploy/readplan/whl-upload/output.txt b/acceptance/bundle/deploy/readplan/whl-upload/output.txt index fef495b6973..7ea15a3dc8d 100644 --- a/acceptance/bundle/deploy/readplan/whl-upload/output.txt +++ b/acceptance/bundle/deploy/readplan/whl-upload/output.txt @@ -13,6 +13,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged "method": "POST", "path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/whl-upload/default/artifacts/.internal/my_test_code-0.0.1+[UNIX_TIME_NANOS]-py3-none-any.whl", "q": { + "bundleFileOrigin": "DEPLOYMENT_COPY", "overwrite": "true" } } @@ -39,6 +40,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged "method": "POST", "path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/whl-upload/default/artifacts/.internal/my_test_code-0.0.1+[UNIX_TIME_NANOS]-py3-none-any.whl", "q": { + "bundleFileOrigin": "DEPLOYMENT_COPY", "overwrite": "true" } } diff --git a/acceptance/bundle/libraries/outside_of_bundle_root/output.txt b/acceptance/bundle/libraries/outside_of_bundle_root/output.txt index e7f1bc13643..a7e632d59bd 100644 --- a/acceptance/bundle/libraries/outside_of_bundle_root/output.txt +++ b/acceptance/bundle/libraries/outside_of_bundle_root/output.txt @@ -57,6 +57,7 @@ Resources: 2 created, 0 changed, 0 deleted, 0 unchanged "method": "POST", "path": "/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/outside_of_bundle_root/default/artifacts/.internal/test.whl", "q": { + "bundleFileOrigin": "DEPLOYMENT_COPY", "overwrite": "true" } } diff --git a/bundle/libraries/filer_workspace.go b/bundle/libraries/filer_workspace.go index 3d223c342f8..35fe78693c5 100644 --- a/bundle/libraries/filer_workspace.go +++ b/bundle/libraries/filer_workspace.go @@ -9,6 +9,6 @@ import ( ) func filerForWorkspace(ctx context.Context, b *bundle.Bundle, uploadPath string) (filer.Filer, string, diag.Diagnostics) { - f, err := filer.NewWorkspaceFilesClient(b.WorkspaceClient(ctx), uploadPath) + f, err := filer.NewWorkspaceFilesClientForBundleDeploy(b.WorkspaceClient(ctx), uploadPath) return f, uploadPath, diag.FromErr(err) } From 6fbb5ec7cc07f54f759971c59b0db5d83812515a Mon Sep 17 00:00:00 2001 From: "ilya.kuznetsov" Date: Mon, 23 Nov 2026 21:37:48 +0000 Subject: [PATCH 4/5] WIP: fix source guard for unprefixed file_path and missing runtime detection --- .nextchanges/bundles/bundle-file-origin.md | 2 +- bundle/deploy/files/sync.go | 14 +++++++++++--- bundle/deploy/files/sync_test.go | 11 +++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.nextchanges/bundles/bundle-file-origin.md b/.nextchanges/bundles/bundle-file-origin.md index a32cb06d9f7..c7e2c3e9a61 100644 --- a/.nextchanges/bundles/bundle-file-origin.md +++ b/.nextchanges/bundles/bundle-file-origin.md @@ -1 +1 @@ -* `bundle deploy` and `bundle sync` now tag the files they upload as deployment copies, so the workspace can tell them apart from a bundle's source files. +* Tag the files and libraries that bundle commands upload to the workspace as deployment copies, so the workspace can tell them apart from a bundle's source files. diff --git a/bundle/deploy/files/sync.go b/bundle/deploy/files/sync.go index 45dcf6888bf..0c55723bdb2 100644 --- a/bundle/deploy/files/sync.go +++ b/bundle/deploy/files/sync.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "path" + "strings" "github.com/databricks/cli/bundle" "github.com/databricks/cli/libs/dbr" @@ -71,8 +72,15 @@ func GetSyncOptions(ctx context.Context, b *bundle.Bundle) (*sync.SyncOptions, e // writesOntoSource reports whether file_path is the sync root itself, as in a // deploy from a workspace folder that points file_path at that folder. Those // files are the bundle's source, not a deployment copy. -// The paths are compared first so that contexts without runtime detection, -// such as unit tests, do not reach dbr.RunsOnRuntime. func writesOntoSource(ctx context.Context, b *bundle.Bundle) bool { - return path.Clean(b.Config.Workspace.FilePath) == path.Clean(b.SyncRootPath) && dbr.RunsOnRuntime(ctx) + if !dbr.HasDetection(ctx) || !dbr.RunsOnRuntime(ctx) { + return false + } + // A file_path set from a variable can still lack the /Workspace prefix, + // because PrependWorkspacePrefix runs before variables are resolved. + filePath := b.Config.Workspace.FilePath + if !strings.HasPrefix(filePath, "/Workspace/") { + filePath = path.Join("/Workspace", filePath) + } + return path.Clean(filePath) == path.Clean(b.SyncRootPath) } diff --git a/bundle/deploy/files/sync_test.go b/bundle/deploy/files/sync_test.go index 399618c5fc7..3bda77f1925 100644 --- a/bundle/deploy/files/sync_test.go +++ b/bundle/deploy/files/sync_test.go @@ -24,6 +24,13 @@ func TestWritesOntoSource(t *testing.T) { filePath: "/Workspace/Users/me/my_bundle/", expect: true, }, + { + name: "file_path from a variable without the /Workspace prefix", + isDbr: true, + syncRootPath: "/Workspace/Users/me/my_bundle", + filePath: "/Users/me/my_bundle", + expect: true, + }, { name: "default file_path from the workspace", isDbr: true, @@ -50,4 +57,8 @@ func TestWritesOntoSource(t *testing.T) { assert.Equal(t, tc.expect, writesOntoSource(ctx, b)) }) } + + t.Run("no runtime detection", func(t *testing.T) { + assert.False(t, writesOntoSource(t.Context(), &bundle.Bundle{})) + }) } From e25f76c7b54193fd798a3f32639ea672bafce00a Mon Sep 17 00:00:00 2001 From: "ilya.kuznetsov" Date: Mon, 23 Nov 2026 21:54:15 +0000 Subject: [PATCH 5/5] Add PR link to changelog entry --- .nextchanges/bundles/bundle-file-origin.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nextchanges/bundles/bundle-file-origin.md b/.nextchanges/bundles/bundle-file-origin.md index c7e2c3e9a61..fe5a7f4259b 100644 --- a/.nextchanges/bundles/bundle-file-origin.md +++ b/.nextchanges/bundles/bundle-file-origin.md @@ -1 +1 @@ -* Tag the files and libraries that bundle commands upload to the workspace as deployment copies, so the workspace can tell them apart from a bundle's source files. +* Tag the files and libraries that bundle commands upload to the workspace as deployment copies, so the workspace can tell them apart from a bundle's source files. ([#7893](https://github.com/databricks/cli/pull/7893))