Skip to content

Commit 0706167

Browse files
committed
address feedback 2
1 parent 085f7a4 commit 0706167

3 files changed

Lines changed: 23 additions & 36 deletions

File tree

src/common/lockfile.apis.ts

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ import * as crypto from 'crypto';
55
import * as fsapi from 'fs-extra';
66
import * as path from 'path';
77

8+
export interface AcquireFileLockOptions {
9+
readonly timeoutMs: number;
10+
readonly retryIntervalMs: number;
11+
}
12+
13+
export interface AcquiredFileLock {
14+
readonly release: () => Promise<void>;
15+
/** Keep the lock and make later acquisition attempts fail immediately. */
16+
readonly retain: () => Promise<void>;
17+
}
18+
19+
type LockState = 'held' | 'released' | 'retained';
20+
821
/** Acquire an atomic lock released only explicitly; interrupted operations remain locked. */
922
export async function acquireFileLock(filePath: string, options: AcquireFileLockOptions): Promise<AcquiredFileLock> {
1023
const lockPath = `${path.resolve(filePath)}.lock`;
@@ -40,13 +53,13 @@ export async function acquireFileLock(filePath: string, options: AcquireFileLock
4053
try {
4154
await fsapi.writeFile(retainedMarker, '', { flag: 'wx' });
4255
} catch (error) {
43-
if (isAlreadyExistsError(error)) {
56+
if (hasErrorCode(error, 'EEXIST')) {
4457
return;
4558
}
4659
try {
4760
await fsapi.rename(ownerMarker, retainedMarker);
4861
} catch (renameError) {
49-
if (!isAlreadyExistsError(renameError)) {
62+
if (!hasErrorCode(renameError, 'EEXIST')) {
5063
throw createLockError(
5164
'Failed to mark the lock as retained',
5265
'ERETAINFAILED',
@@ -64,7 +77,7 @@ export async function acquireFileLock(filePath: string, options: AcquireFileLock
6477
try {
6578
await fsapi.unlink(ownerMarker);
6679
} catch (error) {
67-
if (isFileNotFoundError(error)) {
80+
if (hasErrorCode(error, 'ENOENT')) {
6881
throw createLockError('Lock ownership was compromised', 'ECOMPROMISED', lockPath);
6982
}
7083
throw error;
@@ -73,7 +86,7 @@ export async function acquireFileLock(filePath: string, options: AcquireFileLock
7386
},
7487
};
7588
} catch (error) {
76-
if (!isAlreadyExistsError(error)) {
89+
if (!hasErrorCode(error, 'EEXIST')) {
7790
throw error;
7891
}
7992
if (await isRetainedLock(lockPath)) {
@@ -87,20 +100,12 @@ export async function acquireFileLock(filePath: string, options: AcquireFileLock
87100
}
88101
}
89102

90-
function isAlreadyExistsError(error: unknown): boolean {
91-
return hasErrorCode(error, 'EEXIST');
92-
}
93-
94-
function isFileNotFoundError(error: unknown): boolean {
95-
return hasErrorCode(error, 'ENOENT');
96-
}
97-
98103
async function isRetainedLock(lockPath: string): Promise<boolean> {
99104
try {
100105
await fsapi.lstat(path.join(lockPath, 'retained'));
101106
return true;
102107
} catch (error) {
103-
if (isFileNotFoundError(error)) {
108+
if (hasErrorCode(error, 'ENOENT')) {
104109
return false;
105110
}
106111
throw error;
@@ -120,16 +125,3 @@ function createLockError(message: string, code: string, lockPath: string): NodeJ
120125
async function delay(milliseconds: number): Promise<void> {
121126
return new Promise((resolve) => setTimeout(resolve, milliseconds));
122127
}
123-
124-
export interface AcquireFileLockOptions {
125-
readonly timeoutMs: number;
126-
readonly retryIntervalMs: number;
127-
}
128-
129-
export interface AcquiredFileLock {
130-
readonly release: () => Promise<void>;
131-
/** Keep the lock and make later acquisition attempts fail immediately. */
132-
readonly retain: () => Promise<void>;
133-
}
134-
135-
type LockState = 'held' | 'released' | 'retained';

src/managers/builtin/venvUtils.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ export interface CreateEnvironmentResult {
6767
pkgInstallationCancelled?: boolean;
6868
}
6969

70-
export interface CreateWithProgressOptions {
71-
/** Whether to record uv-created environments in workspace state. Defaults to true. */
72-
readonly trackUvEnvironment?: boolean;
73-
}
74-
7570
export async function clearVenvCache(): Promise<void> {
7671
const keys = [VENV_WORKSPACE_KEY, VENV_GLOBAL_KEY, UV_ENVS_KEY];
7772
const state = await getWorkspacePersistentState();
@@ -358,7 +353,7 @@ export async function createWithProgress(
358353
venvRoot: Uri,
359354
envPath: string,
360355
packages?: PipPackages,
361-
options?: CreateWithProgressOptions,
356+
trackUvEnvironment = true,
362357
): Promise<CreateEnvironmentResult | undefined> {
363358
const pythonPath = getVenvPythonPath(envPath);
364359

@@ -401,7 +396,7 @@ export async function createWithProgress(
401396
const env = api.createPythonEnvironmentItem(await getPythonInfo(resolved), manager);
402397

403398
if (
404-
options?.trackUvEnvironment !== false &&
399+
trackUvEnvironment &&
405400
useUv &&
406401
(resolved.kind === NativePythonEnvironmentKind.venvUv ||
407402
resolved.kind === NativePythonEnvironmentKind.uvWorkspace)

src/test/managers/builtin/venvUtils.createWithProgress.unit.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { createWithProgress } from '../../../managers/builtin/venvUtils';
1616
import { NativePythonEnvironmentKind, NativePythonFinder } from '../../../managers/common/nativePythonFinder';
1717
import * as managerUtils from '../../../managers/common/utils';
1818

19-
suite('createWithProgress uv tracking options', () => {
19+
suite('createWithProgress uv tracking', () => {
2020
let addUvEnvironmentStub: sinon.SinonStub;
2121
let api: PythonEnvironmentApi;
2222
let baseEnvironment: PythonEnvironment;
@@ -105,7 +105,7 @@ suite('createWithProgress uv tracking options', () => {
105105
Uri.file(tempRoot),
106106
envPath,
107107
undefined,
108-
{ trackUvEnvironment: false },
108+
false, // trackUvEnvironment
109109
);
110110

111111
assert.ok(result?.environment);
@@ -124,7 +124,7 @@ suite('createWithProgress uv tracking options', () => {
124124
Uri.file(tempRoot),
125125
envPath,
126126
{ install: ['requests'], uninstall: [] },
127-
{ trackUvEnvironment: false },
127+
false, // trackUvEnvironment
128128
);
129129

130130
assert.ok(result?.environment);

0 commit comments

Comments
 (0)