forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Ability to track the user selected Environment #25090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,14 +7,19 @@ import { sendTelemetryEvent } from '../../telemetry'; | |
import { EventName } from '../../telemetry/constants'; | ||
import { PythonInterpreterTelemetry } from '../../telemetry/types'; | ||
import { IComponentAdapter } from '../contracts'; | ||
import { IPythonPathUpdaterServiceFactory, IPythonPathUpdaterServiceManager } from './types'; | ||
import { | ||
IRecommendedEnvironmentService, | ||
IPythonPathUpdaterServiceFactory, | ||
IPythonPathUpdaterServiceManager, | ||
} from './types'; | ||
|
||
@injectable() | ||
export class PythonPathUpdaterService implements IPythonPathUpdaterServiceManager { | ||
constructor( | ||
@inject(IPythonPathUpdaterServiceFactory) | ||
private readonly pythonPathSettingsUpdaterFactory: IPythonPathUpdaterServiceFactory, | ||
@inject(IComponentAdapter) private readonly pyenvs: IComponentAdapter, | ||
@inject(IRecommendedEnvironmentService) private readonly preferredEnvService: IRecommendedEnvironmentService, | ||
) {} | ||
|
||
public async updatePythonPath( | ||
|
@@ -28,6 +33,9 @@ export class PythonPathUpdaterService implements IPythonPathUpdaterServiceManage | |
let failed = false; | ||
try { | ||
await pythonPathUpdater.updatePythonPath(pythonPath); | ||
if (trigger === 'ui') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is the right spot for tracking what env is selected by the user. |
||
this.preferredEnvService.trackUserSelectedEnvironment(pythonPath, wkspace); | ||
} | ||
} catch (err) { | ||
failed = true; | ||
const reason = err as Error; | ||
|
102 changes: 102 additions & 0 deletions
102
src/client/interpreter/configuration/recommededEnvironmentService.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { inject, injectable } from 'inversify'; | ||
import { IRecommendedEnvironmentService } from './types'; | ||
import { PythonExtension } from '../../api/types'; | ||
import { IExtensionContext, Resource } from '../../common/types'; | ||
import { Uri, workspace } from 'vscode'; | ||
import { getWorkspaceStateValue, updateWorkspaceStateValue } from '../../common/persistentState'; | ||
import { traceError } from '../../logging'; | ||
|
||
const MEMENTO_KEY = 'userSelectedEnvPath'; | ||
|
||
@injectable() | ||
export class RecommendedEnvironmentService implements IRecommendedEnvironmentService { | ||
private api?: PythonExtension['environments']; | ||
constructor(@inject(IExtensionContext) private readonly extensionContext: IExtensionContext) {} | ||
|
||
registerEnvApi(api: PythonExtension['environments']) { | ||
this.api = api; | ||
} | ||
|
||
trackUserSelectedEnvironment(environmentPath: string | undefined, uri: Uri | undefined) { | ||
if (workspace.workspaceFolders?.length) { | ||
try { | ||
void updateWorkspaceStateValue(MEMENTO_KEY, getDataToStore(environmentPath, uri)); | ||
} catch (ex) { | ||
traceError('Failed to update workspace state for preferred environment', ex); | ||
} | ||
} else { | ||
void this.extensionContext.globalState.update(MEMENTO_KEY, environmentPath); | ||
} | ||
} | ||
|
||
getRecommededEnvironment( | ||
resource: Resource, | ||
): | ||
| { environmentPath: string; reason: 'globalUserSelected' | 'workspaceUserSelected' | 'defaultRecommended' } | ||
| undefined { | ||
let workspaceState: string | undefined = undefined; | ||
try { | ||
workspaceState = getWorkspaceStateValue<string>(MEMENTO_KEY); | ||
} catch (ex) { | ||
traceError('Failed to get workspace state for preferred environment', ex); | ||
} | ||
|
||
if (workspace.workspaceFolders?.length && workspaceState) { | ||
const workspaceUri = ( | ||
(resource ? workspace.getWorkspaceFolder(resource)?.uri : undefined) || | ||
workspace.workspaceFolders[0].uri | ||
).toString(); | ||
|
||
try { | ||
const existingJson: Record<string, string> = JSON.parse(workspaceState); | ||
const selectedEnvPath = existingJson[workspaceUri]; | ||
if (selectedEnvPath) { | ||
return { environmentPath: selectedEnvPath, reason: 'workspaceUserSelected' }; | ||
} | ||
} catch (ex) { | ||
traceError('Failed to parse existing workspace state value for preferred environment', ex); | ||
} | ||
} | ||
|
||
const globalSelectedEnvPath = this.extensionContext.globalState.get<string | undefined>(MEMENTO_KEY); | ||
if (globalSelectedEnvPath) { | ||
return { environmentPath: globalSelectedEnvPath, reason: 'globalUserSelected' }; | ||
} | ||
return this.api && workspace.isTrusted | ||
? { | ||
environmentPath: this.api.getActiveEnvironmentPath(resource).path, | ||
reason: 'defaultRecommended', | ||
} | ||
: undefined; | ||
} | ||
} | ||
|
||
function getDataToStore(environmentPath: string | undefined, uri: Uri | undefined): string | undefined { | ||
if (!workspace.workspaceFolders?.length) { | ||
return environmentPath; | ||
} | ||
const workspaceUri = ( | ||
(uri ? workspace.getWorkspaceFolder(uri)?.uri : undefined) || workspace.workspaceFolders[0].uri | ||
).toString(); | ||
const existingData = getWorkspaceStateValue<string>(MEMENTO_KEY); | ||
if (!existingData) { | ||
return JSON.stringify(environmentPath ? { [workspaceUri]: environmentPath } : {}); | ||
} | ||
try { | ||
const existingJson: Record<string, string> = JSON.parse(existingData); | ||
if (environmentPath) { | ||
existingJson[workspaceUri] = environmentPath; | ||
} else { | ||
delete existingJson[workspaceUri]; | ||
} | ||
return JSON.stringify(existingJson); | ||
} catch (ex) { | ||
traceError('Failed to parse existing workspace state value for preferred environment', ex); | ||
return JSON.stringify({ | ||
[workspaceUri]: environmentPath, | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old code no longer required