Skip to content

Commit 64bbc15

Browse files
committed
support pipenv path setting
1 parent 377e9d9 commit 64bbc15

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

src/features/settings/settingHelpers.ts

Lines changed: 75 additions & 1 deletion
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, traceWarn } from '../../common/logging';
13-
import { getWorkspaceFile, getWorkspaceFolders } from '../../common/workspace.apis';
13+
import { getConfiguration, getWorkspaceFile, getWorkspaceFolders } from '../../common/workspace.apis';
1414
import { PythonProjectManager, PythonProjectSettings } from '../../internal.api';
1515

1616
function getSettings(
@@ -393,3 +393,77 @@ export async function removePythonProjectSetting(edits: EditProjectSettings[]):
393393
});
394394
await Promise.all(promises);
395395
}
396+
397+
/**
398+
* Gets user-configured setting for window-scoped settings.
399+
* Priority order: globalRemoteValue > globalLocalValue > globalValue
400+
* @param section - The configuration section (e.g., 'python-envs')
401+
* @param key - The configuration key (e.g., 'terminal.autoActivationType')
402+
* @returns The user-configured value or undefined if not set by user
403+
*/
404+
export function getSettingWindowScope<T>(section: string, key: string): T | undefined {
405+
const config = getConfiguration(section);
406+
const inspect = config.inspect<T>(key);
407+
if (!inspect) {
408+
return undefined;
409+
}
410+
411+
const inspectRecord = inspect as Record<string, unknown>;
412+
if ('globalRemoteValue' in inspect && inspectRecord.globalRemoteValue !== undefined) {
413+
return inspectRecord.globalRemoteValue as T;
414+
}
415+
if ('globalLocalValue' in inspect && inspectRecord.globalLocalValue !== undefined) {
416+
return inspectRecord.globalLocalValue as T;
417+
}
418+
if (inspect.globalValue !== undefined) {
419+
return inspect.globalValue;
420+
}
421+
return undefined;
422+
}
423+
424+
/**
425+
* Gets user-configured setting for workspace-scoped settings.
426+
* Priority order: workspaceFolderValue > workspaceValue > globalValue
427+
* @param section - The configuration section (e.g., 'python')
428+
* @param key - The configuration key (e.g., 'pipenvPath')
429+
* @param scope - Optional URI scope for workspace folder-specific settings
430+
* @returns The user-configured value or undefined if not set by user
431+
*/
432+
export function getSettingWorkspaceScope<T>(section: string, key: string, scope?: Uri): T | undefined {
433+
const config = getConfiguration(section, scope);
434+
const inspect = config.inspect<T>(key);
435+
if (!inspect) {
436+
return undefined;
437+
}
438+
439+
if (inspect.workspaceFolderValue !== undefined) {
440+
return inspect.workspaceFolderValue;
441+
}
442+
if (inspect.workspaceValue !== undefined) {
443+
return inspect.workspaceValue;
444+
}
445+
if (inspect.globalValue !== undefined) {
446+
return inspect.globalValue;
447+
}
448+
return undefined;
449+
}
450+
451+
/**
452+
* Gets user-configured setting for user-scoped settings.
453+
* Only checks globalValue (ignores defaultValue).
454+
* @param section - The configuration section (e.g., 'python')
455+
* @param key - The configuration key (e.g., 'pipenvPath')
456+
* @returns The user-configured value or undefined if not set by user
457+
*/
458+
export function getSettingUserScope<T>(section: string, key: string): T | undefined {
459+
const config = getConfiguration(section);
460+
const inspect = config.inspect<T>(key);
461+
if (!inspect) {
462+
return undefined;
463+
}
464+
465+
if (inspect.globalValue !== undefined) {
466+
return inspect.globalValue;
467+
}
468+
return undefined;
469+
}

src/managers/pipenv/pipenvUtils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { ENVS_EXTENSION_ID } from '../../common/constants';
1414
import { traceError, traceInfo } from '../../common/logging';
1515
import { getWorkspacePersistentState } from '../../common/persistentState';
16+
import { getSettingWorkspaceScope } from '../../features/settings/settingHelpers';
1617
import {
1718
isNativeEnvInfo,
1819
NativeEnvInfo,
@@ -46,6 +47,12 @@ export async function clearPipenvCache(): Promise<void> {
4647
pipenvPath = undefined;
4748
}
4849

50+
function getPipenvPathFromSettings(): Uri[] {
51+
// python.pipenvPath is a workspace-scoped setting
52+
const pipenvPath = getSettingWorkspaceScope<string>('python', 'pipenvPath');
53+
return pipenvPath ? [Uri.file(pipenvPath)] : [];
54+
}
55+
4956
export async function getPipenv(native?: NativePythonFinder): Promise<string | undefined> {
5057
if (pipenvPath) {
5158
return pipenvPath;
@@ -140,7 +147,9 @@ export async function refreshPipenv(
140147
manager: EnvironmentManager,
141148
): Promise<PythonEnvironment[]> {
142149
traceInfo('Refreshing pipenv environments');
143-
const data = await nativeFinder.refresh(hardRefresh);
150+
151+
const searchUris = getPipenvPathFromSettings();
152+
const data = await nativeFinder.refresh(hardRefresh, searchUris.length > 0 ? searchUris : undefined);
144153

145154
let pipenv = await getPipenv();
146155

0 commit comments

Comments
 (0)