Skip to content

Commit a5ce3bd

Browse files
rugpanovIsaac
andcommitted
fix(python-setup): scope stale conflict retries to the files they restore
* A later setup run makes an older retry stale only when it ran inside the folder whose files the retry restores: the uv workspace root for a member, else the project. A run of an unrelated project no longer disables the retry. * Pick the backup folder in the form the CLI named the backup, also for a standalone project opened through a symlink. * checkProjectFileBackup checks that the backup is readable, not only that it exists. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 4a3c120 commit a5ce3bd

5 files changed

Lines changed: 140 additions & 30 deletions

File tree

‎packages/databricks-vscode/src/python-setup/controllers/PythonSetupAdoptionManager.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "node:path";
2+
import {PythonSetupState} from "../../vscode-objs/StateStorage";
23
import {TargetCompute} from "../../telemetry/constants";
34
import {PythonSetupAdoption} from "../../telemetry/pythonSetupExtensions";
45

@@ -26,7 +27,9 @@ export interface PythonSetupAdoptionDeps {
2627
* The project and venv folders in the persisted setup state; each is
2728
* undefined in state saved by older versions.
2829
*/
29-
savedSetup: () => {projectRoot?: string; venvPath?: string} | undefined;
30+
savedSetup: () =>
31+
| Pick<PythonSetupState, "projectRoot" | "venvPath">
32+
| undefined;
3033
/** Whether the managed venv interpreter in `venvDir` exists on disk. */
3134
venvExists: (venvDir: string) => boolean;
3235
/** Emit the gauge. Wraps telemetry; must be best-effort (never throw). */

‎packages/databricks-vscode/src/python-setup/controllers/PythonSetupEnvironmentSetup.test.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,75 @@ describe("PythonSetupEnvironmentSetup constraint-conflict recovery", () => {
21932193
expect(cli.calls).to.have.length(2);
21942194
});
21952195

2196+
it("keeps a retry live after a setup run of an unrelated project", async () => {
2197+
let activeRoot = "/projA";
2198+
const restored: string[] = [];
2199+
const cli = makeScriptedCli([
2200+
conflictResult(),
2201+
SUCCESS_REAL_RUN,
2202+
SUCCESS_REAL_RUN,
2203+
]);
2204+
let retryAction: PythonSetupErrorAction | undefined;
2205+
const setup = new PythonSetupEnvironmentSetup(
2206+
makeDeps({
2207+
cli,
2208+
projectRoot: () => activeRoot,
2209+
pickSetupPreset: async () => "full",
2210+
restoreProjectFile: async (_root, backupPath) => {
2211+
restored.push(backupPath);
2212+
},
2213+
showError: async (_message, _detail, actions) => {
2214+
retryAction = actions?.find(
2215+
(a) => a.label === "Retry DB Connect setup"
2216+
);
2217+
},
2218+
})
2219+
);
2220+
2221+
await setup.setup();
2222+
activeRoot = "/projB";
2223+
await setup.setup();
2224+
await (retryAction as {run: () => Promise<void>}).run();
2225+
2226+
expect(restored).to.deep.equal(["/proj/pyproject.toml.bak"]);
2227+
expect(cli.calls).to.have.length(3);
2228+
});
2229+
2230+
it("restores a backup that the CLI named from the real project folder", async () => {
2231+
// A standalone project open through a symlink: the CLI reports real paths.
2232+
const restored: Array<{root: string; backupPath: string}> = [];
2233+
let retryAction: PythonSetupErrorAction | undefined;
2234+
const setup = new PythonSetupEnvironmentSetup(
2235+
makeDeps({
2236+
cli: makeScriptedCli([
2237+
{
2238+
...conflictResult(),
2239+
backupPath: "/real/proj/pyproject.toml.bak",
2240+
},
2241+
SUCCESS_REAL_RUN,
2242+
]),
2243+
projectRoot: () => "/link/proj",
2244+
realPath: async () => "/real/proj",
2245+
pickSetupPreset: async () => "full",
2246+
restoreProjectFile: async (root, backupPath) => {
2247+
restored.push({root, backupPath});
2248+
},
2249+
showError: async (_message, _detail, actions) => {
2250+
retryAction = actions?.find(
2251+
(a) => a.label === "Retry DB Connect setup"
2252+
);
2253+
},
2254+
})
2255+
);
2256+
2257+
await setup.setup();
2258+
await (retryAction as {run: () => Promise<void>}).run();
2259+
2260+
expect(restored).to.deep.equal([
2261+
{root: "/real/proj", backupPath: "/real/proj/pyproject.toml.bak"},
2262+
]);
2263+
});
2264+
21962265
it("offers the retry when only the uv workspace root was backed up", async () => {
21972266
const {restored} = await retryMemberConflict({
21982267
...memberConflictResult(),

‎packages/databricks-vscode/src/python-setup/controllers/PythonSetupEnvironmentSetup.ts‎

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,8 @@ export class PythonSetupEnvironmentSetup implements Disposable {
339339
* "ready" line for it.
340340
*/
341341
private readonly readyRoots = new Set<string>();
342-
/**
343-
* Setup runs started this session. Any later run can rewrite a shared uv
344-
* workspace root pyproject.toml, so it makes an older member retry stale.
345-
*/
346-
private runCount = 0;
342+
/** The project folder of each setup run this session, in start order. */
343+
private readonly runFolders: string[] = [];
347344
/**
348345
* True when the currently active project has been set up successfully this
349346
* session. Reads the live `projectRoot` so it tracks the active project the
@@ -491,7 +488,7 @@ export class PythonSetupEnvironmentSetup implements Disposable {
491488
trigger?: PythonSetupRunTrigger
492489
): Promise<void> {
493490
const {cli, withProgress} = this.deps;
494-
this.runCount++;
491+
this.runFolders.push(cwd);
495492

496493
const flags = presetToFlags(preset);
497494
const invocation: SetupLocalInvocation = {
@@ -580,14 +577,15 @@ export class PythonSetupEnvironmentSetup implements Disposable {
580577
// back to. Without either, fall through to the ordinary doc-link
581578
// handling rather than offer a "retry as DB Connect" that can't work
582579
// (nothing to restore) or would loop (a run that already skipped pins).
583-
const cwdForms = result.uvWorkspaceRoot
580+
const isPinConflict =
581+
result.error?.code === "E_PROVISION_CONFLICT" &&
582+
!invocation.skipConstraints;
583+
const cwdForms = isPinConflict
584584
? [cwd, await this.realPathForConflict(cwd)]
585585
: [cwd];
586-
const conflictBackups =
587-
result.error?.code === "E_PROVISION_CONFLICT" &&
588-
!invocation.skipConstraints
589-
? conflictBackupsOf(result, cwdForms)
590-
: [];
586+
const conflictBackups = isPinConflict
587+
? conflictBackupsOf(result, cwdForms)
588+
: [];
591589
const recoverableConflict = conflictBackups.length > 0;
592590
const actions = recoverableConflict
593591
? this.buildConflictRecoveryActions(
@@ -780,11 +778,10 @@ export class PythonSetupEnvironmentSetup implements Disposable {
780778
* (`--no-constraints`). The restore is load-bearing: the conflict fails
781779
* *after* the pins were merged to disk, so `--no-constraints` alone would
782780
* leave the conflicting pins in place and only skip re-adding them. It runs
783-
* through {@link runGuarded} — so a click can't race an in-flight run, and
784-
* the restore only fires when the retry actually runs — and no-ops once the
785-
* project is ready, or once another member synced the same uv workspace, so
786-
* a stale toast's Retry can't undo what a later run provisioned. A failed
787-
* restore throws before the CLI spawns, leaving showError to log it.
781+
* through {@link runGuarded}, so a click can't race an in-flight run. It
782+
* does nothing once the project is ready, and it asks for a new setup once
783+
* a later run touched the same files, so a stale toast can't undo that run.
784+
* A failed restore throws before the CLI spawns, leaving showError to log it.
788785
* - "Open pyproject.toml" opens the merged file so the user can adjust the
789786
* dependencies that clashed.
790787
*/
@@ -794,7 +791,11 @@ export class PythonSetupEnvironmentSetup implements Disposable {
794791
uvWorkspaceRoot: string | undefined,
795792
backups: ProjectFileBackup[]
796793
): PythonSetupErrorAction[] {
797-
const runsAtConflict = this.runCount;
794+
const runsAtConflict = this.runFolders.length;
795+
// The files the retry restores: the uv workspace root for a member, else
796+
// the project. A later run inside this folder can have rewritten them;
797+
// a run of the root project is inside it too.
798+
const scope = uvWorkspaceRoot ?? cwd;
798799
return [
799800
{
800801
label: "Retry DB Connect setup",
@@ -804,9 +805,11 @@ export class PythonSetupEnvironmentSetup implements Disposable {
804805
if (this.readyRoots.has(cwd)) {
805806
return;
806807
}
807-
// Any later run can rewrite a shared uv workspace root file,
808-
// also for the root project, whose result names no root.
809-
if (this.runCount !== runsAtConflict) {
808+
if (
809+
this.runFolders
810+
.slice(runsAtConflict)
811+
.some((folder) => containsPath(scope, folder))
812+
) {
810813
return this.deps.notify(
811814
"Another Python environment setup ran after this " +
812815
"conflict. Run the setup again."
@@ -974,7 +977,6 @@ function conflictBackupsOf(
974977
result: PythonSetupResult,
975978
cwdForms: string[]
976979
): ProjectFileBackup[] {
977-
const cwd = cwdForms[0];
978980
if (result.uvWorkspaceRoot) {
979981
// A member's pins live in the root file, and --no-constraints leaves
980982
// them. Without a root backup, a retry would conflict again.
@@ -987,14 +989,26 @@ function conflictBackupsOf(
987989
];
988990
const {backupPath} = result;
989991
if (backupPath) {
990-
// The CLI names the backup from its form of the project folder.
991-
const folder =
992-
cwdForms.find((form) => containsPath(form, backupPath)) ?? cwd;
993-
backups.unshift({folder, backupPath});
992+
backups.unshift({
993+
folder: backupFolder(backupPath, cwdForms),
994+
backupPath,
995+
});
994996
}
995997
return backups;
996998
}
997999
return result.backupPath
998-
? [{folder: cwd, backupPath: result.backupPath}]
1000+
? [
1001+
{
1002+
folder: backupFolder(result.backupPath, cwdForms),
1003+
backupPath: result.backupPath,
1004+
},
1005+
]
9991006
: [];
10001007
}
1008+
1009+
/** The form of the project folder that the CLI named `backupPath` from. */
1010+
function backupFolder(backupPath: string, cwdForms: string[]): string {
1011+
return (
1012+
cwdForms.find((form) => containsPath(form, backupPath)) ?? cwdForms[0]
1013+
);
1014+
}

‎packages/databricks-vscode/src/python-setup/controllers/pythonSetupDeps.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {expect} from "chai";
22
import {
3+
chmod,
34
mkdir,
45
mkdtemp,
56
readdir,
@@ -1185,6 +1186,29 @@ describe("makePythonSetupDeps checkProjectFileBackup", () => {
11851186
}
11861187
});
11871188

1189+
it("rejects a backup that cannot be read", async function () {
1190+
if (process.platform === "win32" || process.getuid?.() === 0) {
1191+
// chmod does not remove read access there.
1192+
this.skip();
1193+
}
1194+
const dir = await mkdtemp(path.join(tmpdir(), "vpex-check-"));
1195+
try {
1196+
const backup = path.join(dir, "pyproject.toml.bak");
1197+
await writeFile(backup, "original\n");
1198+
await chmod(backup, 0o200);
1199+
const deps = makePythonSetupDeps(makeWiring());
1200+
let threw = false;
1201+
try {
1202+
await deps.checkProjectFileBackup(dir, backup);
1203+
} catch {
1204+
threw = true;
1205+
}
1206+
expect(threw).to.equal(true);
1207+
} finally {
1208+
await rm(dir, {recursive: true, force: true});
1209+
}
1210+
});
1211+
11881212
it("rejects a missing backup or one outside the folder", async () => {
11891213
const dir = await mkdtemp(path.join(tmpdir(), "vpex-check-"));
11901214
const outside = await mkdtemp(path.join(tmpdir(), "vpex-out-"));

‎packages/databricks-vscode/src/python-setup/controllers/pythonSetupDeps.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {randomBytes} from "crypto";
22
import {existsSync} from "fs";
3-
import {access, copyFile, realpath, rename, rm} from "fs/promises";
3+
import {access, constants, copyFile, realpath, rename, rm} from "fs/promises";
44
import path from "path";
55
import {commands, ProgressLocation, Uri, window} from "vscode";
66
import {PackageManagerDetection} from "../../language/packageManagerDetection";
@@ -292,7 +292,7 @@ export function makePythonSetupDeps(
292292
},
293293
checkProjectFileBackup: async (folder: string, backupPath: string) => {
294294
assertBackupInside(folder, backupPath);
295-
await access(backupPath);
295+
await access(backupPath, constants.R_OK);
296296
},
297297
restoreProjectFile: async (folder: string, backupPath: string) => {
298298
// Restore the CLI's pre-merge backup over pyproject.toml (the seam's

0 commit comments

Comments
 (0)