Skip to content

Commit

Permalink
Ensure we start watching environments when activating discovery compo…
Browse files Browse the repository at this point in the history
…nent (microsoft#19827)

Ensure we start watchers and create workspace related objects when
activating discovery component
  • Loading branch information
Kartik Raj authored Sep 13, 2022
1 parent 2ef2db4 commit 2f4e9a0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { IDisposable } from '../../../../common/types';
import { createDeferred, Deferred } from '../../../../common/utils/async';
import { Disposables } from '../../../../common/utils/resourceLifecycle';
import { traceError } from '../../../../logging';
import { PythonEnvInfo } from '../../info';
import { IPythonEnvsIterator, Locator, PythonLocatorQuery } from '../../locator';

Expand All @@ -28,15 +29,22 @@ export abstract class LazyResourceBasedLocator<I = PythonEnvInfo> extends Locato

private watchersReady?: Deferred<void>;

/**
* This can be used to initialize resources when subclasses are created.
*/
protected async activate(): Promise<void> {
await this.ensureResourcesReady();
// There is not need to wait for the watchers to get started.
this.ensureWatchersReady().ignoreErrors();
}

public async dispose(): Promise<void> {
await this.disposables.dispose();
}

public async *iterEnvs(query?: PythonLocatorQuery): IPythonEnvsIterator<I> {
await this.ensureResourcesReady();
await this.activate();
yield* this.doIterEnvs(query);
// There is not need to wait for the watchers to get started.
this.ensureWatchersReady().ignoreErrors();
}

/**
Expand Down Expand Up @@ -87,7 +95,10 @@ export abstract class LazyResourceBasedLocator<I = PythonEnvInfo> extends Locato
return;
}
this.resourcesReady = createDeferred<void>();
await this.initResources();
await this.initResources().catch((ex) => {
traceError(ex);
this.resourcesReady?.reject(ex);
});
this.resourcesReady.resolve();
}

Expand All @@ -97,7 +108,10 @@ export abstract class LazyResourceBasedLocator<I = PythonEnvInfo> extends Locato
return;
}
this.watchersReady = createDeferred<void>();
await this.initWatchers();
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 @@ -84,6 +84,7 @@ export abstract class FSWatchingLocator<I = PythonEnvInfo> extends LazyResourceB
private readonly watcherKind: FSWatcherKind = FSWatcherKind.Global,
) {
super();
this.activate().ignoreErrors();
}

protected async initWatchers(): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions src/client/pythonEnvironments/base/locators/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class WorkspaceLocators<I = PythonEnvInfo> extends LazyResourceBasedLocat

constructor(private readonly watchRoots: WatchRootsFunc, private readonly factories: WorkspaceLocatorFactory<I>[]) {
super();
this.activate().ignoreErrors();
}

public async dispose(): Promise<void> {
Expand Down

0 comments on commit 2f4e9a0

Please sign in to comment.