Skip to content

Commit bf7b2ea

Browse files
Kartik Rajwesm
authored andcommitted
Validate output returned by poetry (microsoft/vscode-python#18488)
* Validate output returned by poetry * Only build VSIX * If a directory does not exist do not watch it * Revert "Only build VSIX" This reverts commit 339756f33b1b3b9b7bc6e0a27b103e06862180a6. * News * Fix tests
1 parent db4064a commit bf7b2ea

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix CPU load issue caused by poetry plugin by not watching directories which do not exist.

extensions/positron-python/src/client/pythonEnvironments/base/locators/lowLevel/fsWatchingLocator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export enum FSWatcherKind {
2121
Workspace, // Watchers observes directory in the user's currently open workspace.
2222
}
2323

24-
type DirUnwatchableReason = 'too many files' | undefined;
24+
type DirUnwatchableReason = 'directory does not exist' | 'too many files' | undefined;
2525

2626
/**
2727
* Determine if the directory is watchable.
@@ -33,9 +33,9 @@ function checkDirWatchable(dirname: string): DirUnwatchableReason {
3333
} catch (err) {
3434
traceError('Reading directory to watch failed', err);
3535
if (err.code === 'ENOENT') {
36-
// We treat a missing directory as watchable since it should
37-
// be watchable if created later.
38-
return undefined;
36+
// Treat a missing directory as unwatchable since it can lead to CPU load issues:
37+
// https://github.com/microsoft/vscode-python/issues/18459
38+
return 'directory does not exist';
3939
}
4040
throw err; // re-throw
4141
}

extensions/positron-python/src/client/pythonEnvironments/common/environmentManagers/poetry.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55

66
import * as path from 'path';
77
import { getOSType, getUserHomeDir, OSType } from '../../../common/utils/platform';
8-
import { getPythonSetting, isParentPath, pathExistsSync, readFileSync, shellExecute } from '../externalDependencies';
8+
import {
9+
getPythonSetting,
10+
isParentPath,
11+
pathExists,
12+
pathExistsSync,
13+
readFileSync,
14+
shellExecute,
15+
} from '../externalDependencies';
916
import { getEnvironmentDirFromPath } from '../commonUtils';
1017
import { isVirtualenvEnvironment } from './simplevirtualenvs';
1118
import { StopWatch } from '../../../common/utils/stopWatch';
@@ -201,12 +208,16 @@ export class Poetry {
201208
* So we'll need to remove the string "(Activated)" after splitting lines to get the full path.
202209
*/
203210
const activated = '(Activated)';
204-
return result.stdout.splitLines().map((line) => {
205-
if (line.endsWith(activated)) {
206-
line = line.slice(0, -activated.length);
207-
}
208-
return line.trim();
209-
});
211+
const res = await Promise.all(
212+
result.stdout.splitLines().map(async (line) => {
213+
if (line.endsWith(activated)) {
214+
line = line.slice(0, -activated.length);
215+
}
216+
const folder = line.trim();
217+
return (await pathExists(folder)) ? folder : undefined;
218+
}),
219+
);
220+
return res.filter((r) => r !== undefined).map((r) => r!);
210221
}
211222

212223
/**

extensions/positron-python/src/test/pythonEnvironments/base/locators/lowLevel/fsWatchingLocator.unit.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import {
1212
FSWatchingLocator,
1313
} from '../../../../../client/pythonEnvironments/base/locators/lowLevel/fsWatchingLocator';
1414
import * as binWatcher from '../../../../../client/pythonEnvironments/common/pythonBinariesWatcher';
15+
import { TEST_LAYOUT_ROOT } from '../../../common/commonTestConstants';
1516

1617
suite('File System Watching Locator Tests', () => {
17-
const baseDir = '/this/is/a/fake/path';
18+
const baseDir = TEST_LAYOUT_ROOT;
19+
const fakeDir = '/this/is/a/fake/path';
1820
const callback = async () => Promise.resolve(PythonEnvKind.System);
1921
let watchLocationStub: sinon.SinonStub;
2022

@@ -34,7 +36,7 @@ suite('File System Watching Locator Tests', () => {
3436
envStructure?: binWatcher.PythonEnvStructure;
3537
} = {},
3638
) {
37-
super(() => baseDir, callback, opts, watcherKind);
39+
super(() => [baseDir, fakeDir], callback, opts, watcherKind);
3840
}
3941

4042
public async initialize() {
@@ -95,6 +97,11 @@ suite('File System Watching Locator Tests', () => {
9597
assert.strictEqual(watchLocationStub.callCount, expected.length);
9698
expected.forEach((glob) => {
9799
assert.ok(watchLocationStub.calledWithMatch(baseDir, sinon.match.any, glob));
100+
assert.strictEqual(
101+
// As directory does not exist, it should not be watched.
102+
watchLocationStub.calledWithMatch(fakeDir, sinon.match.any, glob),
103+
false,
104+
);
98105
});
99106
} else if (watcherKind === FSWatcherKind.Global) {
100107
assert.ok(watchLocationStub.notCalled);

0 commit comments

Comments
 (0)