Skip to content

Commit 0f2ac6f

Browse files
committed
fix: preserve Poetry command working directory
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 6b2fe9b5-38ea-442f-b07a-b6c71134d480
1 parent 7e9c7a3 commit 0f2ac6f

2 files changed

Lines changed: 70 additions & 12 deletions

File tree

src/managers/poetry/poetryPackageManager.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class PoetryPackageManager implements PackageManager, Disposable {
8282
}
8383

8484
try {
85-
await this.runPoetryManage(environment, { install: toInstall, uninstall: toUninstall });
85+
await this.runPoetryManage({ install: toInstall, uninstall: toUninstall });
8686
await updatePackagesAndNotify(this, environment, this.packages.get(environment.envId.id), (changes) => {
8787
this._onDidChangePackages.fire({ environment, manager: this, changes });
8888
});
@@ -174,10 +174,7 @@ export class PoetryPackageManager implements PackageManager, Disposable {
174174
this.packages.clear();
175175
}
176176

177-
private async runPoetryManage(
178-
environment: PythonEnvironment,
179-
options: { install?: string[]; uninstall?: string[] },
180-
): Promise<void> {
177+
private async runPoetryManage(options: { install?: string[]; uninstall?: string[] }): Promise<void> {
181178
const poetry = await getPoetry();
182179
if (!poetry) {
183180
throw new Error(
@@ -186,13 +183,10 @@ export class PoetryPackageManager implements PackageManager, Disposable {
186183
),
187184
);
188185
}
189-
const cwd = await this.getPoetryCwd(environment);
190-
191186
// Handle uninstalls first
192187
if (options.uninstall && options.uninstall.length > 0) {
193188
const removeCmd = new PoetryRemoveCommand({
194189
pythonExecutable: poetry,
195-
cwd,
196190
log: this.log,
197191
});
198192
const packages = parsePackageSpecs(options.uninstall);
@@ -206,7 +200,6 @@ export class PoetryPackageManager implements PackageManager, Disposable {
206200
if (options.install && options.install.length > 0) {
207201
const addCmd = new PoetryAddCommand({
208202
pythonExecutable: poetry,
209-
cwd,
210203
log: this.log,
211204
});
212205
const packages = parsePackageSpecs(options.install);
@@ -237,16 +230,14 @@ export class PoetryPackageManager implements PackageManager, Disposable {
237230
return (data ?? []).map((pkg) => this.api.createPackageItem(pkg, environment, this));
238231
}
239232

240-
async getDirectPackageNames(environment: PythonEnvironment): Promise<Set<string> | undefined> {
233+
async getDirectPackageNames(_environment: PythonEnvironment): Promise<Set<string> | undefined> {
241234
try {
242235
const poetry = await getPoetry();
243236
if (!poetry) {
244237
return undefined;
245238
}
246-
const cwd = await this.getPoetryCwd(environment);
247239
const showTopLevelCmd = new PoetryShowTopLevelCommand({
248240
pythonExecutable: poetry,
249-
cwd,
250241
log: this.log,
251242
});
252243
return await showTopLevelCmd.execute();
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import assert from 'assert';
5+
import * as path from 'path';
6+
import * as sinon from 'sinon';
7+
import { LogOutputChannel, Uri } from 'vscode';
8+
import { PythonEnvironmentApi } from '../../../api';
9+
import * as windowApis from '../../../common/window.apis';
10+
import * as packageChanges from '../../../managers/common/packageChanges';
11+
import { PoetryPackageManager } from '../../../managers/poetry/poetryPackageManager';
12+
import { PoetryManager } from '../../../managers/poetry/poetryManager';
13+
import * as poetryUtils from '../../../managers/poetry/poetryUtils';
14+
import * as runPoetryModule from '../../../managers/poetry/commands/runPoetry';
15+
import { createMockPythonEnvironment } from '../../mocks/pythonEnvironment';
16+
17+
suite('PoetryPackageManager', () => {
18+
const environment = createMockPythonEnvironment({
19+
envPath: path.join(process.cwd(), '.venv'),
20+
managerId: 'ms-python.python:poetry',
21+
});
22+
let runPoetryStub: sinon.SinonStub;
23+
let manager: PoetryPackageManager;
24+
25+
setup(() => {
26+
const api = {
27+
getPythonProjects: () => [
28+
{
29+
name: 'project',
30+
uri: Uri.file(path.join(process.cwd(), 'project', 'pyproject.toml')),
31+
},
32+
],
33+
} as unknown as PythonEnvironmentApi;
34+
const log = {
35+
append: sinon.stub(),
36+
error: sinon.stub(),
37+
info: sinon.stub(),
38+
show: sinon.stub(),
39+
} as unknown as LogOutputChannel;
40+
41+
sinon.stub(poetryUtils, 'getPoetry').resolves('poetry');
42+
sinon.stub(windowApis, 'withProgress').callsFake((_options, task) => task({} as never, {} as never));
43+
sinon.stub(packageChanges, 'updatePackagesAndNotify').resolves([]);
44+
runPoetryStub = sinon.stub(runPoetryModule, 'runPoetry').resolves('');
45+
manager = new PoetryPackageManager(api, log, {} as PoetryManager);
46+
});
47+
48+
teardown(() => {
49+
manager.dispose();
50+
sinon.restore();
51+
});
52+
53+
test('package management inherits the process working directory', async () => {
54+
await manager.manage(environment, { install: ['requests'], uninstall: ['flask'] });
55+
56+
assert.strictEqual(runPoetryStub.callCount, 2);
57+
assert.strictEqual(runPoetryStub.firstCall.args[1], undefined);
58+
assert.strictEqual(runPoetryStub.secondCall.args[1], undefined);
59+
});
60+
61+
test('direct package listing inherits the process working directory', async () => {
62+
await manager.getDirectPackageNames(environment);
63+
64+
assert.strictEqual(runPoetryStub.callCount, 1);
65+
assert.strictEqual(runPoetryStub.firstCall.args[1], undefined);
66+
});
67+
});

0 commit comments

Comments
 (0)