Skip to content

Commit 3f33fc2

Browse files
Fix: Remove global context that caused "add as Python Project" menu to disappear after single use (#724)
The "add as Python Project" context menu item was disappearing after being used once and only reappearing after a full window reload. ## Root Cause The issue was caused by improper management of the global VS Code context `python-envs:isExistingProject`. The context menu visibility was controlled by the condition: ```json "when": "explorerViewletVisible && explorerResourceIsFolder && !python-envs:isExistingProject" ``` During command execution, the code would set this global context: ```typescript commands.executeCommand('setContext', 'python-envs:isExistingProject', isExistingProject(resource)); ``` Once this context was set to `true` for any resource, it would prevent the menu from appearing for all subsequent right-clicks, regardless of the target folder. ## Solution - **Removed** the global context setting logic from the `addPythonProjectGivenResource` command - **Updated** the context menu conditions in `package.json` to simply check for folder/file type without the problematic context condition - **Preserved** existing duplicate detection logic that gracefully handles attempts to add already-existing projects The menu will now consistently appear for folders and `.py` files. If users attempt to add an already-existing project, the existing error handling shows a "No new projects found" warning. ## Changes - `src/extension.ts`: Removed context setting logic and unused helper function (11 lines) - `package.json`: Simplified context menu conditions (2 lines) Fixes #723. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
1 parent 796e8cb commit 3f33fc2

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,12 @@
489489
{
490490
"command": "python-envs.addPythonProjectGivenResource",
491491
"group": "inline",
492-
"when": "explorerViewletVisible && explorerResourceIsFolder && !python-envs:isExistingProject"
492+
"when": "explorerViewletVisible && explorerResourceIsFolder"
493493
},
494494
{
495495
"command": "python-envs.addPythonProjectGivenResource",
496496
"group": "inline",
497-
"when": "explorerViewletVisible && resourceExtname == .py && !python-envs:isExistingProject"
497+
"when": "explorerViewletVisible && resourceExtname == .py"
498498
}
499499
],
500500
"editor/title/run": [

src/extension.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,6 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
189189
const projectManager: PythonProjectManager = new PythonProjectManagerImpl();
190190
context.subscriptions.push(projectManager);
191191

192-
// Helper function to check if a resource is an existing Python project
193-
const isExistingProject = (uri: Uri | undefined): boolean => {
194-
if (!uri) {
195-
return false;
196-
}
197-
return projectManager.get(uri) !== undefined;
198-
};
199-
200192
const envVarManager: EnvVarManager = new PythonEnvVariableManager(projectManager);
201193
context.subscriptions.push(envVarManager);
202194

@@ -323,11 +315,6 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
323315
});
324316
}),
325317
commands.registerCommand('python-envs.addPythonProjectGivenResource', async (resource) => {
326-
// Set context to show/hide menu item depending on whether the resource is already a Python project
327-
if (resource instanceof Uri) {
328-
commands.executeCommand('setContext', 'python-envs:isExistingProject', isExistingProject(resource));
329-
}
330-
331318
await addPythonProjectCommand(resource, projectManager, envManagers, projectCreators);
332319
const totalProjectCount = projectManager.getProjects().length + 1;
333320
sendTelemetryEvent(EventNames.ADD_PROJECT, undefined, {

src/features/creators/autoFindProjects.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,17 @@ export class AutoFindProjects implements PythonProjectCreator {
8383

8484
if (filtered.length === 0) {
8585
// No new projects found that are not already in the project manager
86-
traceInfo('All discovered projects are already registered in the project manager');
86+
traceInfo(
87+
`All selected resources are already registered in the project manager: ${files
88+
.map((uri) => uri.fsPath)
89+
.join(', ')}`,
90+
);
8791
setImmediate(() => {
88-
showWarningMessage('No new projects found');
92+
if (files.length === 1) {
93+
showWarningMessage(`${files[0].fsPath} already exists as project.`);
94+
} else {
95+
showWarningMessage('Selected resources already exist as projects.');
96+
}
8997
});
9098
return;
9199
}

src/features/creators/existingProjects.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,17 @@ export class ExistingProjects implements PythonProjectCreator {
5656

5757
if (filtered.length === 0) {
5858
// No new projects found that are not already in the project manager
59-
traceInfo('All discovered projects are already registered in the project manager');
59+
const formattedProjectPaths =
60+
existingAddUri === undefined ? 'None' : existingAddUri.map((uri) => uri.fsPath).join(', ');
61+
traceInfo(
62+
`All selected resources are already registered in the project manager. Resources selected: ${formattedProjectPaths}`,
63+
);
6064
setImmediate(() => {
61-
showWarningMessage('No new projects found');
65+
if (existingAddUri && existingAddUri.length === 1) {
66+
showWarningMessage(`Selected resource already exists as project.`);
67+
} else {
68+
showWarningMessage('Selected resources already exist as projects.');
69+
}
6270
});
6371
return;
6472
}

0 commit comments

Comments
 (0)