Skip to content

Commit a5a514b

Browse files
rugpanovIsaac
andcommitted
localenv: stop on an interrupt during workspace discovery; uv-prefix the names
- An interrupt while uv workspace dir runs no longer falls back to the standalone behavior: with cached constraints, that run went on to rewrite the member pyproject.toml before it reported E_CANCELED. - Rename the warning to W_UV_WORKSPACE_CONSTRAINTS_REPLACED and the Go fields to match the uv-prefixed JSON names. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 163f8b6 commit a5a514b

5 files changed

Lines changed: 82 additions & 39 deletions

File tree

‎cmd/environments/output.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func renderResult(ctx context.Context, cmd *cobra.Command, res *libslocalenv.Res
7676
if res.DryRun {
7777
if res.Plan != nil {
7878
cmdio.LogString(ctx, "Plan: "+res.Plan.WouldWrite)
79-
if res.Plan.WouldWriteWorkspace != "" {
80-
cmdio.LogString(ctx, "Plan: "+res.Plan.WouldWriteWorkspace+" (uv workspace root)")
79+
if res.Plan.WouldWriteUvWorkspace != "" {
80+
cmdio.LogString(ctx, "Plan: "+res.Plan.WouldWriteUvWorkspace+" (uv workspace root)")
8181
}
8282
for _, region := range res.Plan.ChangedRegions {
8383
cmdio.LogString(ctx, " changed region: "+region)
@@ -118,14 +118,14 @@ func renderSuccess(ctx context.Context, res *libslocalenv.Result) {
118118
pyprojectDetail = "updated (backup: " + res.BackupPath + ")"
119119
}
120120
cmdio.LogString(ctx, fmt.Sprintf(" %-20s%s", "pyproject.toml", pyprojectDetail))
121-
if res.WorkspaceRoot != "" {
121+
if res.UvWorkspaceRoot != "" {
122122
// The root file changes only when its constraint block changes, and every
123123
// change writes a backup; --no-constraints and a no-op re-run leave it alone.
124124
rootDetail := "unchanged"
125-
if res.WorkspaceBackupPath != "" {
126-
rootDetail = "constraints updated (backup: " + res.WorkspaceBackupPath + ")"
125+
if res.UvWorkspaceBackupPath != "" {
126+
rootDetail = "constraints updated (backup: " + res.UvWorkspaceBackupPath + ")"
127127
}
128-
cmdio.LogString(ctx, fmt.Sprintf(" %-20s%s", "uv workspace root", res.WorkspaceRoot))
128+
cmdio.LogString(ctx, fmt.Sprintf(" %-20s%s", "uv workspace root", res.UvWorkspaceRoot))
129129
cmdio.LogString(ctx, fmt.Sprintf(" %-20s%s", "Root pyproject.toml", rootDetail))
130130
}
131131
renderInstalledPythonFallback(ctx, res)

‎cmd/environments/output_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ func TestRenderSuccessWorkspaceMember(t *testing.T) {
170170
res.OK = true
171171
res.Resolved = &libslocalenv.ResolvedInfo{PythonVersion: "3.12"}
172172
res.VenvPath = "../../.venv"
173-
res.WorkspaceRoot = "/repo"
174-
res.WorkspaceBackupPath = "/repo/pyproject.toml.bak"
173+
res.UvWorkspaceRoot = "/repo"
174+
res.UvWorkspaceBackupPath = "/repo/pyproject.toml.bak"
175175

176176
out := renderText(t, res, nil)
177177

@@ -196,7 +196,7 @@ func TestRenderDryRunWorkspaceMember(t *testing.T) {
196196
res := libslocalenv.NewResult()
197197
res.OK = true
198198
res.DryRun = true
199-
res.Plan = &libslocalenv.Plan{WouldWrite: "/repo/a/pyproject.toml", WouldWriteWorkspace: "/repo/pyproject.toml"}
199+
res.Plan = &libslocalenv.Plan{WouldWrite: "/repo/a/pyproject.toml", WouldWriteUvWorkspace: "/repo/pyproject.toml"}
200200
out := renderText(t, res, nil)
201201
assert.Contains(t, out, "Plan: /repo/pyproject.toml (uv workspace root)")
202202
}
@@ -206,7 +206,7 @@ func TestRenderSuccessWorkspaceMemberUnchangedRoot(t *testing.T) {
206206
res := libslocalenv.NewResult()
207207
res.OK = true
208208
res.VenvPath = "../../.venv"
209-
res.WorkspaceRoot = "/repo"
209+
res.UvWorkspaceRoot = "/repo"
210210

211211
out := renderText(t, res, nil)
212212

‎libs/localenv/pipeline.go‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,17 @@ func (p *Pipeline) run(ctx context.Context) error {
209209
// https://docs.astral.sh/uv/concepts/projects/workspaces/
210210
var rootRel string
211211
p.workspaceRoot, rootRel = p.discoverWorkspaceRoot(ctx)
212+
// A discovery failure keeps the standalone behavior, but an interrupt must
213+
// not: the run would go on to merge as a standalone project and write files.
214+
if err := ctx.Err(); err != nil {
215+
return p.fail(PhasePreflight, false, NewError(ErrCanceled, err, "interrupted"))
216+
}
212217
venvBase := p.ProjectDir
213218
p.venvRel = venvDir
214219
if p.workspaceRoot != "" {
215220
venvBase = p.workspaceRoot
216221
p.venvRel = filepath.Join(rootRel, venvDir)
217-
p.res.WorkspaceRoot = filepath.ToSlash(p.workspaceRoot)
222+
p.res.UvWorkspaceRoot = filepath.ToSlash(p.workspaceRoot)
218223
if !p.Check {
219224
if err := ensureWritable(p.workspaceRoot); err != nil {
220225
return p.fail(PhasePreflight, false, NewError(ErrNotWritable, err, "uv workspace root %s is not writable", filepath.ToSlash(p.workspaceRoot)))
@@ -508,9 +513,9 @@ func (p *Pipeline) mergePlan(_ context.Context, pyMinor string, c *Constraints,
508513
// Unlike the member file, the root is reported only when the run changes
509514
// it; Result.WorkspaceRoot already names the root.
510515
if root != nil && root.changed {
511-
plan.WouldWriteWorkspace = filepath.ToSlash(root.path)
516+
plan.WouldWriteUvWorkspace = filepath.ToSlash(root.path)
512517
if backupName, statErr := p.plannedBackupName(p.workspaceRoot); statErr == nil {
513-
plan.WouldBackupWorkspace = filepath.ToSlash(backupName)
518+
plan.WouldBackupUvWorkspace = filepath.ToSlash(backupName)
514519
}
515520
}
516521
p.res.Plan = plan
@@ -546,7 +551,7 @@ func (p *Pipeline) planRootMerge(deps []string, opts MergeOptions) (*rootMerge,
546551
// bundle) wrote them or the user did, and the new ones apply to every member.
547552
if replaced {
548553
p.res.Warnings = append(p.res.Warnings, Warning{
549-
Code: WarnWorkspaceConstraintsReplaced,
554+
Code: WarnUvWorkspaceConstraintsReplaced,
550555
Message: fmt.Sprintf("the constraint pins in %s are replaced; all members of this uv workspace share them", filepath.ToSlash(path)),
551556
})
552557
}
@@ -587,7 +592,7 @@ func (p *Pipeline) applyMerge(_ context.Context, mergedBytes []byte, root *rootM
587592
// when either write changed it.
588593
rootBackup, rootMutated, perr := p.writePyproject(root.path, root.merged, false)
589594
if rootBackup != "" {
590-
p.res.WorkspaceBackupPath = filepath.ToSlash(rootBackup)
595+
p.res.UvWorkspaceBackupPath = filepath.ToSlash(rootBackup)
591596
}
592597
if perr != nil {
593598
perr.Msg = "uv workspace root: " + perr.Msg

‎libs/localenv/result.go‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,12 @@ type ResolvedInfo struct {
215215
type Plan struct {
216216
WouldWrite string `json:"wouldWrite"`
217217
WouldBackup string `json:"wouldBackup,omitempty"`
218-
// WouldWriteWorkspace and WouldBackupWorkspace name the workspace root
218+
// WouldWriteUvWorkspace and WouldBackupUvWorkspace name the workspace root
219219
// pyproject.toml and its planned backup, for a uv workspace member only.
220-
WouldWriteWorkspace string `json:"wouldWriteUvWorkspace,omitempty"`
221-
WouldBackupWorkspace string `json:"wouldBackupUvWorkspace,omitempty"`
222-
WouldInstallPython string `json:"wouldInstallPython,omitempty"`
223-
Diff string `json:"diff"`
220+
WouldWriteUvWorkspace string `json:"wouldWriteUvWorkspace,omitempty"`
221+
WouldBackupUvWorkspace string `json:"wouldBackupUvWorkspace,omitempty"`
222+
WouldInstallPython string `json:"wouldInstallPython,omitempty"`
223+
Diff string `json:"diff"`
224224

225225
ChangedRegions []string `json:"-"`
226226
}
@@ -297,11 +297,11 @@ const (
297297
// the user's own pyproject pins it (constraints-only mode) — so it agrees with the
298298
// validate hard-fail, which keys on the installed venv rather than the mode.
299299
WarnStandalonePysparkConflict = "W_STANDALONE_PYSPARK_CONFLICT"
300-
// WarnWorkspaceConstraintsReplaced: the project is a uv workspace member and
300+
// WarnUvWorkspaceConstraintsReplaced: the project is a uv workspace member and
301301
// the run replaced constraint pins the workspace root already had, from an
302302
// earlier run (for example for another bundle) or written by the user. Every
303303
// member of the workspace now resolves against the new pins.
304-
WarnWorkspaceConstraintsReplaced = "W_WORKSPACE_CONSTRAINTS_REPLACED"
304+
WarnUvWorkspaceConstraintsReplaced = "W_UV_WORKSPACE_CONSTRAINTS_REPLACED"
305305
)
306306

307307
// Result is the full outcome of a sync run and the root of the --json object
@@ -322,10 +322,10 @@ type Result struct {
322322
Greenfield bool `json:"greenfield"`
323323
Plan *Plan `json:"plan,omitempty"`
324324
VenvPath string `json:"venvPath,omitempty"`
325-
// WorkspaceRoot is the uv workspace root (absolute, slash form) when the
325+
// UvWorkspaceRoot is the uv workspace root (absolute, slash form) when the
326326
// project is a workspace member. The [tool.uv] constraint block and the venv
327327
// live there. Empty for a standalone project.
328-
WorkspaceRoot string `json:"uvWorkspaceRoot,omitempty"`
328+
UvWorkspaceRoot string `json:"uvWorkspaceRoot,omitempty"`
329329
PythonResolution PythonResolution `json:"pythonResolution,omitempty"`
330330
// PythonInterpreter is the exact interpreter chosen by the installed-Python
331331
// fallback, named in the text summary so the user can tell which interpreter
@@ -341,9 +341,9 @@ type Result struct {
341341
Warnings []Warning `json:"warnings"`
342342
Error *PipelineError `json:"error"`
343343
BackupPath string `json:"backupPath,omitempty"`
344-
// WorkspaceBackupPath is the backup of the workspace root pyproject.toml, set
344+
// UvWorkspaceBackupPath is the backup of the workspace root pyproject.toml, set
345345
// only when the run changed that file.
346-
WorkspaceBackupPath string `json:"uvWorkspaceBackupPath,omitempty"`
346+
UvWorkspaceBackupPath string `json:"uvWorkspaceBackupPath,omitempty"`
347347
// DurationMs is the pipeline's wall time in milliseconds (spec §6). It covers the
348348
// CLI pipeline only; the extension measures its own end-to-end latency (process
349349
// spawn, interpreter adoption) separately.

‎libs/localenv/workspace_test.go‎

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package localenv
22

33
import (
4+
"context"
45
"errors"
56
"os"
67
"path/filepath"
@@ -120,10 +121,10 @@ func TestPipelineWorkspaceMember(t *testing.T) {
120121
assert.Contains(t, string(memberBytes), `"databricks-connect~=17.2.0"`)
121122

122123
assert.Equal(t, "../../.venv", res.VenvPath)
123-
assert.Equal(t, filepath.ToSlash(root), res.WorkspaceRoot)
124-
assert.Equal(t, filepath.ToSlash(filepath.Join(root, backupFile)), res.WorkspaceBackupPath)
124+
assert.Equal(t, filepath.ToSlash(root), res.UvWorkspaceRoot)
125+
assert.Equal(t, filepath.ToSlash(filepath.Join(root, backupFile)), res.UvWorkspaceBackupPath)
125126
assert.Equal(t, filepath.ToSlash(filepath.Join(member, backupFile)), res.BackupPath)
126-
assert.NotContains(t, codes(res.Warnings), WarnWorkspaceConstraintsReplaced, "first run must not warn")
127+
assert.NotContains(t, codes(res.Warnings), WarnUvWorkspaceConstraintsReplaced, "first run must not warn")
127128
}
128129

129130
func TestPipelineWorkspaceMemberIdempotent(t *testing.T) {
@@ -133,8 +134,8 @@ func TestPipelineWorkspaceMemberIdempotent(t *testing.T) {
133134
res, err := p.Run(t.Context())
134135
require.NoError(t, err)
135136
assert.Empty(t, res.BackupPath)
136-
assert.Empty(t, res.WorkspaceBackupPath)
137-
assert.NotContains(t, codes(res.Warnings), WarnWorkspaceConstraintsReplaced)
137+
assert.Empty(t, res.UvWorkspaceBackupPath)
138+
assert.NotContains(t, codes(res.Warnings), WarnUvWorkspaceConstraintsReplaced)
138139
for _, dir := range []string{root, member} {
139140
matches, err := filepath.Glob(filepath.Join(dir, "pyproject.toml.*.bak"))
140141
require.NoError(t, err)
@@ -155,7 +156,7 @@ func TestPipelineWorkspaceConstraintsReplacedWarning(t *testing.T) {
155156

156157
res, err := p.Run(t.Context())
157158
require.NoError(t, err)
158-
assert.Contains(t, codes(res.Warnings), WarnWorkspaceConstraintsReplaced)
159+
assert.Contains(t, codes(res.Warnings), WarnUvWorkspaceConstraintsReplaced)
159160
}
160161

161162
func TestPipelineWorkspaceMemberNoConstraints(t *testing.T) {
@@ -168,7 +169,7 @@ func TestPipelineWorkspaceMemberNoConstraints(t *testing.T) {
168169
after, err := os.ReadFile(filepath.Join(root, pyprojectFile))
169170
require.NoError(t, err)
170171
assert.Equal(t, before, after)
171-
assert.Empty(t, res.WorkspaceBackupPath)
172+
assert.Empty(t, res.UvWorkspaceBackupPath)
172173
assert.NoFileExists(t, filepath.Join(root, backupFile))
173174
}
174175

@@ -184,8 +185,8 @@ func TestPipelineWorkspaceMemberDryRun(t *testing.T) {
184185
assert.Equal(t, before, after)
185186
require.NotNil(t, res.Plan)
186187
assert.Equal(t, filepath.ToSlash(filepath.Join(member, pyprojectFile)), res.Plan.WouldWrite)
187-
assert.Equal(t, filepath.ToSlash(filepath.Join(root, pyprojectFile)), res.Plan.WouldWriteWorkspace)
188-
assert.Equal(t, filepath.ToSlash(filepath.Join(root, backupFile)), res.Plan.WouldBackupWorkspace)
188+
assert.Equal(t, filepath.ToSlash(filepath.Join(root, pyprojectFile)), res.Plan.WouldWriteUvWorkspace)
189+
assert.Equal(t, filepath.ToSlash(filepath.Join(root, backupFile)), res.Plan.WouldBackupUvWorkspace)
189190
assert.Contains(t, res.Plan.Diff, "workspace root")
190191
}
191192

@@ -286,7 +287,7 @@ func TestPipelineWorkspaceReplacesUserRootPinsWithWarning(t *testing.T) {
286287

287288
res, err := p.Run(t.Context())
288289
require.NoError(t, err)
289-
assert.Contains(t, codes(res.Warnings), WarnWorkspaceConstraintsReplaced)
290+
assert.Contains(t, codes(res.Warnings), WarnUvWorkspaceConstraintsReplaced)
290291
}
291292

292293
func TestApplyMergeRootFailureBeforeAnyWriteIsNotDiskMutated(t *testing.T) {
@@ -335,7 +336,44 @@ func TestPipelineWorkspaceMemberDryRunReRunPlansNoRootWrite(t *testing.T) {
335336
res, err := p.Run(t.Context())
336337
require.NoError(t, err)
337338
require.NotNil(t, res.Plan)
338-
assert.Empty(t, res.Plan.WouldWriteWorkspace)
339-
assert.Empty(t, res.Plan.WouldBackupWorkspace)
340-
assert.NotEmpty(t, res.WorkspaceRoot)
339+
assert.Empty(t, res.Plan.WouldWriteUvWorkspace)
340+
assert.Empty(t, res.Plan.WouldBackupUvWorkspace)
341+
assert.NotEmpty(t, res.UvWorkspaceRoot)
342+
}
343+
344+
// cancelDiscoveryPM cancels the run while workspace discovery runs, like a
345+
// SIGINT that interrupts `uv workspace dir`.
346+
type cancelDiscoveryPM struct {
347+
fakePM
348+
cancel context.CancelFunc
349+
}
350+
351+
func (f cancelDiscoveryPM) WorkspaceRoot(ctx context.Context, _ string) (string, error) {
352+
f.cancel()
353+
return "", ctx.Err()
354+
}
355+
356+
func TestPipelineCancelDuringDiscoveryWritesNothing(t *testing.T) {
357+
p, root, member := newMemberPipeline(t)
358+
// A first run fills the constraint cache, so the canceled run below can
359+
// still fetch; then the member file is reset so a merge would change it.
360+
_, err := p.Run(t.Context())
361+
require.NoError(t, err)
362+
require.NoError(t, os.WriteFile(filepath.Join(member, pyprojectFile),
363+
[]byte("[project]\nname = \"a\"\nversion = \"0.1.0\"\nrequires-python = \">=3.10\"\n"), 0o644))
364+
365+
ctx, cancel := context.WithCancel(t.Context())
366+
defer cancel()
367+
p.PM = cancelDiscoveryPM{fakePM: fakePM{py: "3.12", dbc: "17.2.0", root: root}, cancel: cancel}
368+
before, err := os.ReadFile(filepath.Join(member, pyprojectFile))
369+
require.NoError(t, err)
370+
371+
res, err := p.Run(ctx)
372+
require.Error(t, err)
373+
require.NotNil(t, res.Error)
374+
assert.Equal(t, ErrCanceled, res.Error.Code)
375+
assert.False(t, res.Error.DiskMutated)
376+
after, err := os.ReadFile(filepath.Join(member, pyprojectFile))
377+
require.NoError(t, err)
378+
assert.Equal(t, before, after)
341379
}

0 commit comments

Comments
 (0)