Skip to content

Commit

Permalink
Only watch files on a non virtual workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
rchiodo committed Oct 17, 2022
1 parent 947935d commit f835c57
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IDisposable } from '../../../../common/types';
import { createDeferred, Deferred } from '../../../../common/utils/async';
import { Disposables } from '../../../../common/utils/resourceLifecycle';
import { traceError } from '../../../../logging';
import { arePathsSame } from '../../../common/externalDependencies';
import { arePathsSame, isVirtualWorkspace } from '../../../common/externalDependencies';
import { getEnvPath } from '../../info/env';
import { BasicEnvInfo, IPythonEnvsIterator, Locator, PythonLocatorQuery } from '../../locator';

Expand Down Expand Up @@ -123,10 +123,14 @@ export abstract class LazyResourceBasedLocator extends Locator<BasicEnvInfo> imp
return;
}
this.watchersReady = createDeferred<void>();
await this.initWatchers().catch((ex) => {
traceError(ex);
this.watchersReady?.reject(ex);
});
this.watchersReady.resolve();

// Don't create any file watchers in a virtual workspace.
if (!(await isVirtualWorkspace())) {
await this.initWatchers().catch((ex) => {
traceError(ex);
this.watchersReady?.reject(ex);
});
this.watchersReady.resolve();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

import * as path from 'path';
import { Uri } from 'vscode';
import { chain, iterable } from '../../../../common/utils/async';
import { findInterpretersInDir, looksLikeBasicVirtualPython } from '../../../common/commonUtils';
import { pathExists } from '../../../common/externalDependencies';
Expand Down Expand Up @@ -54,9 +53,9 @@ async function getVirtualEnvKind(interpreterPath: string): Promise<PythonEnvKind
export class WorkspaceVirtualEnvironmentLocator extends FSWatchingLocator {
public readonly providerId: string = 'workspaceVirtualEnvLocator';

public constructor(private readonly root: Uri) {
public constructor(private readonly root: string) {
super(
() => (this.root.scheme === 'file' ? getWorkspaceVirtualEnvDirs(this.root.fsPath) : []),
() => getWorkspaceVirtualEnvDirs(this.root),
getVirtualEnvKind,
{
// Note detecting kind of virtual env depends on the file structure around the
Expand All @@ -68,8 +67,8 @@ export class WorkspaceVirtualEnvironmentLocator extends FSWatchingLocator {
}

protected doIterEnvs(): IPythonEnvsIterator<BasicEnvInfo> {
async function* iterator(root: Uri) {
const envRootDirs = root.scheme === 'file' ? await getWorkspaceVirtualEnvDirs(root.fsPath) : [];
async function* iterator(root: string) {
const envRootDirs = await getWorkspaceVirtualEnvDirs(root);
const envGenerators = envRootDirs.map((envRootDir) => {
async function* generator() {
traceVerbose(`Searching for workspace virtual envs in: ${envRootDir}`);
Expand Down
8 changes: 8 additions & 0 deletions src/client/pythonEnvironments/common/externalDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as fsapi from 'fs-extra';
import * as path from 'path';
import * as vscode from 'vscode';
import { IWorkspaceService } from '../../common/application/types';
import { ExecutionResult, IProcessServiceFactory, ShellOptions, SpawnOptions } from '../../common/process/types';
import { IDisposable, IConfigurationService } from '../../common/types';
import { chain, iterable } from '../../common/utils/async';
Expand All @@ -27,6 +28,13 @@ export async function exec(file: string, args: string[], options: SpawnOptions =
return service.exec(file, args, options);
}

// Workspace

export async function isVirtualWorkspace(): Promise<boolean> {
const service = internalServiceContainer.get<IWorkspaceService>(IWorkspaceService);
return service.isVirtualWorkspace;
}

// filesystem

export function pathExists(absPath: string): Promise<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion src/client/pythonEnvironments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function watchRoots(args: WatchRootsArgs): IDisposable {

function createWorkspaceLocator(ext: ExtensionState): WorkspaceLocators {
const locators = new WorkspaceLocators(watchRoots, [
(root: vscode.Uri) => [new WorkspaceVirtualEnvironmentLocator(root), new PoetryLocator(root.fsPath)],
(root: vscode.Uri) => [new WorkspaceVirtualEnvironmentLocator(root.fsPath), new PoetryLocator(root.fsPath)],
// Add an ILocator factory func here for each kind of workspace-rooted locator.
]);
ext.disposables.push(locators);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
// Licensed under the MIT License.

import * as path from 'path';
import { Uri } from 'vscode';
import { PythonEnvKind } from '../../../../../client/pythonEnvironments/base/info';
import { WorkspaceVirtualEnvironmentLocator } from '../../../../../client/pythonEnvironments/base/locators/lowLevel/workspaceVirtualEnvLocator';
import { TEST_LAYOUT_ROOT } from '../../../common/commonTestConstants';
import { testLocatorWatcher } from './watcherTestUtils';

suite('WorkspaceVirtualEnvironment Locator', async () => {
const testWorkspaceFolder = path.join(TEST_LAYOUT_ROOT, 'workspace', 'folder1');
testLocatorWatcher(
testWorkspaceFolder,
async (root?: string) => new WorkspaceVirtualEnvironmentLocator(Uri.file(root!)),
{
arg: testWorkspaceFolder,
kind: PythonEnvKind.Venv,
},
);
testLocatorWatcher(testWorkspaceFolder, async (root?: string) => new WorkspaceVirtualEnvironmentLocator(root!), {
arg: testWorkspaceFolder,
kind: PythonEnvKind.Venv,
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import * as path from 'path';
import * as sinon from 'sinon';
import { Uri } from 'vscode';
import * as fsWatcher from '../../../../../client/common/platform/fileSystemWatcher';
import * as platformUtils from '../../../../../client/common/utils/platform';
import { PythonEnvKind } from '../../../../../client/pythonEnvironments/base/info';
Expand All @@ -28,7 +27,7 @@ suite('WorkspaceVirtualEnvironment Locator', () => {
/* do nothing */
},
});
locator = new WorkspaceVirtualEnvironmentLocator(Uri.file(testWorkspaceFolder));
locator = new WorkspaceVirtualEnvironmentLocator(testWorkspaceFolder);
});
teardown(async () => {
await locator.dispose();
Expand Down

0 comments on commit f835c57

Please sign in to comment.