Skip to content

Commit 836fa96

Browse files
authored
add workspace attribute to project setting (#404)
fixes #400
1 parent 31eea3c commit 836fa96

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/features/projectManager.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,21 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
6767

6868
// For each override, resolve its path and add as a project if not already present
6969
for (const o of overrides) {
70-
const uri = Uri.file(path.resolve(w.uri.fsPath, o.path));
70+
let uriFromWorkspace: Uri | undefined = undefined;
71+
// if override has a workspace property, resolve the path relative to that workspace
72+
if (o.workspace) {
73+
//
74+
const workspaceFolder = workspaces.find((ws) => ws.name === o.workspace);
75+
if (workspaceFolder) {
76+
if (workspaceFolder.uri.toString() !== w.uri.toString()) {
77+
continue; // skip if the workspace is not the same as the current workspace
78+
}
79+
uriFromWorkspace = Uri.file(path.resolve(workspaceFolder.uri.fsPath, o.path));
80+
}
81+
}
82+
const uri = uriFromWorkspace ? uriFromWorkspace : Uri.file(path.resolve(w.uri.fsPath, o.path));
83+
84+
// Check if the project already exists in the newProjects array
7185
if (!newProjects.some((p) => p.uri.toString() === uri.toString())) {
7286
newProjects.push(new PythonProjectsImpl(o.path, uri));
7387
}

src/features/settings/settingHelpers.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { PythonProject } from '../../api';
1111
import { DEFAULT_ENV_MANAGER_ID, DEFAULT_PACKAGE_MANAGER_ID } from '../../common/constants';
1212
import { traceError, traceInfo } from '../../common/logging';
13-
import { getWorkspaceFile } from '../../common/workspace.apis';
13+
import { getWorkspaceFile, getWorkspaceFolders } from '../../common/workspace.apis';
1414
import { PythonProjectManager, PythonProjectSettings } from '../../internal.api';
1515

1616
function getSettings(
@@ -284,6 +284,7 @@ export interface EditProjectSettings {
284284
project: PythonProject;
285285
envManager?: string;
286286
packageManager?: string;
287+
workspace?: string;
287288
}
288289

289290
export async function addPythonProjectSetting(edits: EditProjectSettings[]): Promise<void> {
@@ -306,13 +307,23 @@ export async function addPythonProjectSetting(edits: EditProjectSettings[]): Pro
306307
traceError(`Unable to find workspace for ${e.project.uri.fsPath}`);
307308
});
308309

310+
const isMultiroot = (getWorkspaceFolders() ?? []).length > 1;
311+
309312
const promises: Thenable<void>[] = [];
310313
workspaces.forEach((es, w) => {
311314
const config = workspace.getConfiguration('python-envs', w.uri);
312315
const overrides = config.get<PythonProjectSettings[]>('pythonProjects', []);
313316
es.forEach((e) => {
317+
if (isMultiroot) {
318+
}
314319
const pwPath = path.normalize(e.project.uri.fsPath);
315-
const index = overrides.findIndex((s) => path.resolve(w.uri.fsPath, s.path) === pwPath);
320+
const index = overrides.findIndex((s) => {
321+
if (s.workspace) {
322+
// If the workspace is set, check workspace and path in existing overrides
323+
return s.workspace === w.name && path.resolve(w.uri.fsPath, s.path) === pwPath;
324+
}
325+
return path.resolve(w.uri.fsPath, s.path) === pwPath;
326+
});
316327
if (index >= 0) {
317328
overrides[index].envManager = e.envManager ?? envManager;
318329
overrides[index].packageManager = e.packageManager ?? pkgManager;
@@ -321,6 +332,7 @@ export async function addPythonProjectSetting(edits: EditProjectSettings[]): Pro
321332
path: path.relative(w.uri.fsPath, pwPath).replace(/\\/g, '/'),
322333
envManager,
323334
packageManager: pkgManager,
335+
workspace: isMultiroot ? w.name : undefined,
324336
});
325337
}
326338
});

src/internal.api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export interface PythonProjectSettings {
295295
path: string;
296296
envManager: string;
297297
packageManager: string;
298+
workspace?: string;
298299
}
299300

300301
export class PythonEnvironmentImpl implements PythonEnvironment {

0 commit comments

Comments
 (0)