Skip to content

Commit b6609e7

Browse files
author
Kartik Raj
committed
Fix errors
1 parent 4095967 commit b6609e7

19 files changed

+152
-63
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@
506506
]
507507
},
508508
"python.condaPath": {
509-
"default": "",
509+
"default": "conda",
510510
"description": "%python.condaPath.description%",
511511
"scope": "machine",
512512
"type": "string"

src/client/common/platform/fileSystemWatcher.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
/* eslint-disable global-require */
12
// Copyright (c) Microsoft Corporation. All rights reserved.
23
// Licensed under the MIT License.
34

5+
import type * as vscodeTypes from 'vscode';
46
import { traceError, traceVerbose } from '../../logging';
57
import { IDisposable } from '../types';
68
import { Disposables } from '../utils/resourceLifecycle';
@@ -20,7 +22,7 @@ export function watchLocationForPattern(
2022
callback: (type: FileChangeType, absPath: string) => void,
2123
): IDisposable {
2224
try {
23-
const vscode = require('vscode');
25+
const vscode = require('vscode') as typeof vscodeTypes;
2426
const globPattern = new vscode.RelativePattern(baseDir, pattern);
2527
const disposables = new Disposables();
2628
traceVerbose(`Start watching: ${baseDir} with pattern ${pattern} using VSCode API`);
@@ -30,8 +32,8 @@ export function watchLocationForPattern(
3032
disposables.push(watcher.onDidDelete((e) => callback(FileChangeType.Deleted, e.fsPath)));
3133
return disposables;
3234
} catch (ex) {
33-
traceError(ex);
34-
console.log(ex);
35+
traceError('Watcher', (ex as Error).message);
36+
console.log('Watcher', (ex as Error).message);
3537
// eslint-disable-next-line @typescript-eslint/no-empty-function
3638
return { dispose: () => {} };
3739
}

src/client/common/utils/filesystem.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
import * as fs from 'fs';
5-
import * as vscode from 'vscode';
65
import { traceError } from '../../logging';
76

87
export enum FileType {

src/client/pythonEnvironments/base/locators/composite/envsMiddleware.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { PythonEnvsWatcher } from '../../watcher';
1616
import { IDisposableRegistry } from '../../../../common/types';
1717
import { WorkspaceLocators } from '../wrappers';
1818
import { createSubLocators } from '../../../locator';
19+
import { PythonDiscoverySettings } from '../../../common/settings';
1920

2021
/**
2122
* A service acts as a bridge between Env Resolver and Env Collection.
@@ -31,9 +32,9 @@ export class EnvsMiddleWare extends PythonEnvsWatcher implements IEnvsMiddleware
3132

3233
private iterators = new Map<EnvIteratorId, IPythonEnvsIterator>();
3334

34-
constructor(folders: readonly WorkspaceFolder[] | undefined) {
35+
constructor(folders: readonly WorkspaceFolder[] | undefined, settings: PythonDiscoverySettings) {
3536
super();
36-
const { locator, disposables, workspaceLocator } = createSubLocators(folders);
37+
const { locator, disposables, workspaceLocator } = createSubLocators(folders, settings);
3738
this.disposables = disposables;
3839
this.locator = locator;
3940
this.workspaceLocator = workspaceLocator;

src/client/pythonEnvironments/base/locators/composite/resolverUtils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import { BasicEnvInfo } from '../../locator';
3030
import { parseVersionFromExecutable } from '../../info/executable';
3131
import { traceError, traceWarn } from '../../../../logging';
3232
import { isVirtualEnvironment } from '../../../common/environmentManagers/simplevirtualenvs';
33-
import { getWorkspaceFolderPaths } from '../../../../common/vscodeApis/workspaceApis';
3433
import { ActiveState } from '../../../common/environmentManagers/activestate';
3534

3635
function getResolvers(): Map<PythonEnvKind, (env: BasicEnvInfo) => Promise<PythonEnvInfo>> {

src/client/pythonEnvironments/base/locators/composite/worker.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22
/* eslint-disable @typescript-eslint/ban-types */
33
/* eslint-disable no-restricted-globals */
44
import { parentPort, workerData } from 'worker_threads';
5+
import { WorkspaceFolder } from 'vscode';
6+
import { URI as Uri } from 'vscode-uri';
57
import { EnvsMiddleWare } from './envsMiddleware';
8+
import { traceError } from '../../../../logging';
69

7-
const folders = workerData;
8-
const envsMiddleware = new EnvsMiddleWare(folders);
10+
const { folders, settings } = workerData;
11+
const workspaceFolders = (folders as WorkspaceFolder[]).map((w) => {
12+
const wuri = w.uri;
13+
const workspaceFolder = {
14+
name: w.name,
15+
uri: Uri.parse((w.uri as unknown) as string),
16+
index: w.index,
17+
};
18+
if (typeof wuri === 'string') {
19+
workspaceFolder.uri = Uri.parse(wuri);
20+
} else if ('scheme' in wuri && 'path' in wuri) {
21+
workspaceFolder.uri = Uri.parse(`${wuri.scheme}://${wuri.path}`);
22+
} else {
23+
traceError('Unexpected search location', JSON.stringify(wuri));
24+
}
25+
return workspaceFolder;
26+
});
27+
28+
const envsMiddleware = new EnvsMiddleWare(workspaceFolders, settings);
929

1030
if (!parentPort) {
1131
throw new Error('Not in a worker thread');
@@ -15,6 +35,7 @@ console.log('Worker thread started');
1535

1636
// // Listen for messages from the main thread
1737
parentPort.on('message', async (event) => {
38+
// console.log(JSON.stringify(settings));
1839
console.log('Worker thread received message', event);
1940
if (!parentPort) {
2041
throw new Error('Not in a worker thread');

src/client/pythonEnvironments/base/locators/composite/workerMiddleware.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Event, EventEmitter, WorkspaceFolder, WorkspaceFoldersChangeEvent, workspace } from 'vscode';
1+
import { Event, EventEmitter, Uri, WorkspaceFolder, WorkspaceFoldersChangeEvent } from 'vscode';
22
import * as path from 'path';
33
import { Worker } from 'worker_threads';
44
import { PythonEnvInfo } from '../../info';
@@ -10,6 +10,7 @@ import {
1010
PythonLocatorQuery,
1111
} from '../../locator';
1212
import { PythonEnvsWatcher } from '../../watcher';
13+
import { PythonDiscoverySettings } from '../../../common/settings';
1314

1415
/**
1516
* A service acts as a bridge between Env Resolver and Env Collection.
@@ -19,9 +20,11 @@ export class WorkerThreadMiddleWare extends PythonEnvsWatcher implements IWorker
1920

2021
private onUpdatedMap = new Map<EnvIteratorId, Event<PythonEnvUpdatedEvent | ProgressNotificationEvent>>();
2122

22-
constructor(folders: readonly WorkspaceFolder[] | undefined) {
23+
constructor(folders: readonly WorkspaceFolder[] | undefined, settings: PythonDiscoverySettings) {
2324
super();
24-
this.worker = new Worker(path.join(__dirname, 'worker.js'), { workerData: folders });
25+
this.worker = new Worker(path.join(__dirname, 'worker.js'), {
26+
workerData: { folders: convertWorkspaceFolders(folders), settings },
27+
});
2528
this.worker.addListener('message', (event) => {
2629
const { methodName, result } = event;
2730
if (methodName === 'onChanged') {
@@ -82,3 +85,17 @@ export class WorkerThreadMiddleWare extends PythonEnvsWatcher implements IWorker
8285
});
8386
}
8487
}
88+
89+
function convertWorkspaceFolders(workspaceFolders: readonly WorkspaceFolder[] | undefined) {
90+
if (!workspaceFolders) {
91+
return [];
92+
}
93+
return workspaceFolders.map((w) => {
94+
const workspaceFolder: WorkspaceFolder = {
95+
name: w.name,
96+
uri: (w.uri.toString() as unknown) as Uri,
97+
index: w.index,
98+
};
99+
return workspaceFolder;
100+
});
101+
}

src/client/pythonEnvironments/base/locators/lowLevel/activeStateLocator.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import { BasicEnvInfo, IPythonEnvsIterator } from '../../locator';
99
import { traceError, traceVerbose } from '../../../../logging';
1010
import { LazyResourceBasedLocator } from '../common/resourceBasedLocator';
1111
import { findInterpretersInDir } from '../../../common/commonUtils';
12+
import { PythonDiscoverySettings } from '../../../common/settings';
1213

1314
export class ActiveStateLocator extends LazyResourceBasedLocator {
1415
public readonly providerId: string = 'activestate';
1516

17+
public constructor(private readonly settings: PythonDiscoverySettings) {
18+
super();
19+
}
20+
1621
// eslint-disable-next-line class-methods-use-this
1722
public async *doIterEnvs(): IPythonEnvsIterator<BasicEnvInfo> {
18-
const state = await ActiveState.getState();
23+
const state = await ActiveState.getState(this.settings);
1924
if (state === undefined) {
2025
traceVerbose(`Couldn't locate the state binary.`);
2126
return;

src/client/pythonEnvironments/base/locators/lowLevel/condaLocator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import { BasicEnvInfo, IPythonEnvsIterator } from '../../locator';
66
import { Conda, getCondaEnvironmentsTxt } from '../../../common/environmentManagers/conda';
77
import { traceError, traceVerbose } from '../../../../logging';
88
import { FSWatchingLocator } from './fsWatchingLocator';
9+
import { PythonDiscoverySettings } from '../../../common/settings';
910

1011
export class CondaEnvironmentLocator extends FSWatchingLocator {
1112
public readonly providerId: string = 'conda-envs';
1213

13-
public constructor() {
14+
public constructor(private readonly settings: PythonDiscoverySettings) {
1415
super(
1516
() => getCondaEnvironmentsTxt(),
1617
async () => PythonEnvKind.Conda,
@@ -20,7 +21,7 @@ export class CondaEnvironmentLocator extends FSWatchingLocator {
2021

2122
// eslint-disable-next-line class-methods-use-this
2223
public async *doIterEnvs(): IPythonEnvsIterator<BasicEnvInfo> {
23-
const conda = await Conda.getConda();
24+
const conda = await Conda.getConda(undefined, this.settings);
2425
if (conda === undefined) {
2526
traceVerbose(`Couldn't locate the conda binary.`);
2627
return;

src/client/pythonEnvironments/base/locators/lowLevel/customVirtualEnvLocator.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import { PythonEnvKind } from '../../info';
99
import { BasicEnvInfo, IPythonEnvsIterator } from '../../locator';
1010
import { FSWatchingLocator } from './fsWatchingLocator';
1111
import { findInterpretersInDir, looksLikeBasicVirtualPython } from '../../../common/commonUtils';
12-
import {
13-
getPythonSetting,
14-
onDidChangePythonSetting,
15-
pathExists,
16-
untildify,
17-
} from '../../../common/externalDependencies';
12+
import { onDidChangePythonSetting, pathExists, untildify } from '../../../common/externalDependencies';
1813
import { isPipenvEnvironment } from '../../../common/environmentManagers/pipenv';
1914
import {
2015
isVenvEnvironment,
@@ -24,6 +19,7 @@ import {
2419
import '../../../../common/extensions';
2520
import { asyncFilter } from '../../../../common/utils/arrayUtils';
2621
import { traceError, traceVerbose } from '../../../../logging';
22+
import { PythonDiscoverySettings } from '../../../common/settings';
2723
/**
2824
* Default number of levels of sub-directories to recurse when looking for interpreters.
2925
*/
@@ -35,13 +31,13 @@ export const VENVFOLDERS_SETTING_KEY = 'venvFolders';
3531
/**
3632
* Gets all custom virtual environment locations to look for environments.
3733
*/
38-
async function getCustomVirtualEnvDirs(): Promise<string[]> {
34+
async function getCustomVirtualEnvDirs(settings: PythonDiscoverySettings): Promise<string[]> {
3935
const venvDirs: string[] = [];
40-
const venvPath = getPythonSetting<string>(VENVPATH_SETTING_KEY);
36+
const venvPath = settings[VENVPATH_SETTING_KEY];
4137
if (venvPath) {
4238
venvDirs.push(untildify(venvPath));
4339
}
44-
const venvFolders = getPythonSetting<string[]>(VENVFOLDERS_SETTING_KEY) ?? [];
40+
const venvFolders: string[] = settings[VENVFOLDERS_SETTING_KEY];
4541
const homeDir = getUserHomeDir();
4642
if (homeDir && (await pathExists(homeDir))) {
4743
venvFolders.map((item) => path.join(homeDir, item)).forEach((d) => venvDirs.push(d));
@@ -81,8 +77,8 @@ async function getVirtualEnvKind(interpreterPath: string): Promise<PythonEnvKind
8177
export class CustomVirtualEnvironmentLocator extends FSWatchingLocator {
8278
public readonly providerId: string = 'custom-virtual-envs';
8379

84-
constructor() {
85-
super(getCustomVirtualEnvDirs, getVirtualEnvKind, {
80+
constructor(private readonly settings: PythonDiscoverySettings) {
81+
super(async () => getCustomVirtualEnvDirs(settings), getVirtualEnvKind, {
8682
// Note detecting kind of virtual env depends on the file structure around the
8783
// executable, so we need to wait before attempting to detect it. However even
8884
// if the type detected is incorrect, it doesn't do any practical harm as kinds
@@ -98,8 +94,8 @@ export class CustomVirtualEnvironmentLocator extends FSWatchingLocator {
9894

9995
// eslint-disable-next-line class-methods-use-this
10096
protected doIterEnvs(): IPythonEnvsIterator<BasicEnvInfo> {
101-
async function* iterator() {
102-
const envRootDirs = await getCustomVirtualEnvDirs();
97+
async function* iterator(settings: PythonDiscoverySettings) {
98+
const envRootDirs = await getCustomVirtualEnvDirs(settings);
10399
const envGenerators = envRootDirs.map((envRootDir) => {
104100
async function* generator() {
105101
traceVerbose(`Searching for custom virtual envs in: ${envRootDir}`);
@@ -135,6 +131,6 @@ export class CustomVirtualEnvironmentLocator extends FSWatchingLocator {
135131
traceVerbose(`Finished searching for custom virtual envs`);
136132
}
137133

138-
return iterator();
134+
return iterator(this.settings);
139135
}
140136
}

src/client/pythonEnvironments/base/locators/lowLevel/poetryLocator.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ import '../../../../common/extensions';
1414
import { asyncFilter } from '../../../../common/utils/arrayUtils';
1515
import { traceError, traceVerbose } from '../../../../logging';
1616
import { LazyResourceBasedLocator } from '../common/resourceBasedLocator';
17+
import { PythonDiscoverySettings } from '../../../common/settings';
1718

1819
/**
1920
* Gets all default virtual environment locations to look for in a workspace.
2021
*/
21-
async function getVirtualEnvDirs(root: string): Promise<string[]> {
22+
async function getVirtualEnvDirs(root: string, settings: PythonDiscoverySettings): Promise<string[]> {
2223
const envDirs = [path.join(root, localPoetryEnvDirName)];
23-
const poetry = await Poetry.getPoetry(root);
24+
const poetry = await Poetry.getPoetry(root, settings);
2425
const virtualenvs = await poetry?.getEnvList();
2526
if (virtualenvs) {
2627
envDirs.push(...virtualenvs);
@@ -42,13 +43,13 @@ async function getVirtualEnvKind(interpreterPath: string): Promise<PythonEnvKind
4243
export class PoetryLocator extends LazyResourceBasedLocator {
4344
public readonly providerId: string = 'poetry';
4445

45-
public constructor(private readonly root: string) {
46+
public constructor(private readonly root: string, private readonly settings: PythonDiscoverySettings) {
4647
super();
4748
}
4849

4950
protected doIterEnvs(): IPythonEnvsIterator<BasicEnvInfo> {
50-
async function* iterator(root: string) {
51-
const envDirs = await getVirtualEnvDirs(root);
51+
async function* iterator(root: string, settings: PythonDiscoverySettings) {
52+
const envDirs = await getVirtualEnvDirs(root, settings);
5253
const envGenerators = envDirs.map((envDir) => {
5354
async function* generator() {
5455
traceVerbose(`Searching for poetry virtual envs in: ${envDir}`);
@@ -73,6 +74,6 @@ export class PoetryLocator extends LazyResourceBasedLocator {
7374
traceVerbose(`Finished searching for poetry envs`);
7475
}
7576

76-
return iterator(this.root);
77+
return iterator(this.root, this.settings);
7778
}
7879
}

src/client/pythonEnvironments/common/commonUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import * as fs from 'fs';
55
import * as path from 'path';
66
import { convertFileType, DirEntry, FileType, getFileFilter, getFileType } from '../../common/utils/filesystem';
7-
import { Architecture, getOSType, OSType } from '../../common/utils/platform';
7+
import { getOSType, OSType } from '../../common/utils/platform';
88
import { traceError } from '../../logging';
99
import { PythonVersion, UNKNOWN_PYTHON_VERSION } from '../base/info';
1010
import { comparePythonVersionSpecificity } from '../base/info/env';

src/client/pythonEnvironments/common/environmentManagers/activestate.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { cache } from '../../../common/utils/decorators';
1616
import { traceError, traceVerbose } from '../../../logging';
1717
import { getOSType, getUserHomeDir, OSType } from '../../../common/utils/platform';
18+
import { PythonDiscoverySettings } from '../settings';
1819

1920
export const ACTIVESTATETOOLPATH_SETTING_KEY = 'activeStateToolPath';
2021

@@ -36,9 +37,9 @@ export async function isActiveStateEnvironment(interpreterPath: string): Promise
3637
export class ActiveState {
3738
private static statePromise: Promise<ActiveState | undefined> | undefined;
3839

39-
public static async getState(): Promise<ActiveState | undefined> {
40+
public static async getState(settings?: PythonDiscoverySettings): Promise<ActiveState | undefined> {
4041
if (ActiveState.statePromise === undefined) {
41-
ActiveState.statePromise = ActiveState.locate();
42+
ActiveState.statePromise = ActiveState.locate(settings);
4243
}
4344
return ActiveState.statePromise;
4445
}
@@ -59,10 +60,11 @@ export class ActiveState {
5960
: path.join(home, '.local', 'ActiveState', 'StateTool');
6061
}
6162

62-
private static async locate(): Promise<ActiveState | undefined> {
63+
private static async locate(settings?: PythonDiscoverySettings): Promise<ActiveState | undefined> {
6364
const stateToolDir = this.getStateToolDir();
64-
const stateCommand =
65-
getPythonSetting<string>(ACTIVESTATETOOLPATH_SETTING_KEY) ?? ActiveState.defaultStateCommand;
65+
const stateCommand = settings
66+
? settings[ACTIVESTATETOOLPATH_SETTING_KEY]
67+
: getPythonSetting<string>(ACTIVESTATETOOLPATH_SETTING_KEY);
6668
if (stateToolDir && ((await pathExists(stateToolDir)) || stateCommand !== this.defaultStateCommand)) {
6769
return new ActiveState();
6870
}

src/client/pythonEnvironments/common/environmentManagers/conda.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { traceError, traceVerbose } from '../../../logging';
2323
import { OUTPUT_MARKER_SCRIPT } from '../../../common/process/internal/scripts';
2424
import { splitLines } from '../../../common/stringUtils';
2525
import { SpawnOptions } from '../../../common/process/types';
26+
import { PythonDiscoverySettings } from '../settings';
2627

2728
export const AnacondaCompanyName = 'Anaconda, Inc.';
2829
export const CONDAPATH_SETTING_KEY = 'condaPath';
@@ -271,9 +272,9 @@ export class Conda {
271272
});
272273
}
273274

274-
public static async getConda(shellPath?: string): Promise<Conda | undefined> {
275+
public static async getConda(shellPath?: string, settings?: PythonDiscoverySettings): Promise<Conda | undefined> {
275276
if (Conda.condaPromise.get(shellPath) === undefined || isTestExecution()) {
276-
Conda.condaPromise.set(shellPath, Conda.locate(shellPath));
277+
Conda.condaPromise.set(shellPath, Conda.locate(shellPath, settings));
277278
}
278279
return Conda.condaPromise.get(shellPath);
279280
}
@@ -284,10 +285,12 @@ export class Conda {
284285
*
285286
* @return A Conda instance corresponding to the binary, if successful; otherwise, undefined.
286287
*/
287-
private static async locate(shellPath?: string): Promise<Conda | undefined> {
288+
private static async locate(shellPath?: string, settings?: PythonDiscoverySettings): Promise<Conda | undefined> {
288289
traceVerbose(`Searching for conda.`);
289290
const home = getUserHomeDir();
290-
const customCondaPath = getPythonSetting<string>(CONDAPATH_SETTING_KEY);
291+
const customCondaPath = settings
292+
? settings[CONDAPATH_SETTING_KEY]
293+
: getPythonSetting<string>(CONDAPATH_SETTING_KEY);
291294
const suffix = getOSType() === OSType.Windows ? 'Scripts\\conda.exe' : 'bin/conda';
292295

293296
// Produce a list of candidate binaries to be probed by exec'ing them.

0 commit comments

Comments
 (0)