Skip to content

Commit 9f65313

Browse files
authored
fix: improve extension ID caching and logging in getCallingExtension (#1169)
Fixes #584
1 parent 832c187 commit 9f65313

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/common/utils/frameUtils.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ import { Uri } from 'vscode';
22
import { ENVS_EXTENSION_ID, PYTHON_EXTENSION_ID } from '../constants';
33
import { parseStack } from '../errors/utils';
44
import { allExtensions, getExtension } from '../extension.apis';
5+
import { traceVerbose, traceWarn } from '../logging';
56
import { normalizePath } from './pathUtils';
7+
68
interface FrameData {
79
filePath: string;
810
functionName: string;
911
}
1012

13+
// Cache to avoid repeated stack walks for the same caller location
14+
const extensionIdCache = new Map<string, string>();
15+
1116
function getFrameData(): FrameData[] {
1217
const frames = parseStack(new Error());
1318
return frames.map((frame) => ({
@@ -65,24 +70,36 @@ export function getCallingExtension(): string {
6570
}
6671
}
6772

68-
const envExt = getExtension(ENVS_EXTENSION_ID);
69-
const pythonExt = getExtension(PYTHON_EXTENSION_ID);
70-
if (!envExt || !pythonExt) {
71-
throw new Error('Something went wrong with feature registration');
73+
// Generate cache key from the first relevant file path (the immediate caller)
74+
const cacheKey = filePaths[0] ?? '';
75+
const cachedResult = extensionIdCache.get(cacheKey);
76+
if (cachedResult) {
77+
traceVerbose(`Using cached extension ID for caller: ${cachedResult}`);
78+
return cachedResult;
7279
}
73-
const envsExtPath = normalizePath(envExt.extensionPath);
7480

75-
if (filePaths.every((filePath) => filePath.startsWith(envsExtPath))) {
81+
const envExt = getExtension(ENVS_EXTENSION_ID);
82+
const envsExtPath = envExt ? normalizePath(envExt.extensionPath) : undefined;
83+
84+
if (envsExtPath && filePaths.every((filePath) => filePath.startsWith(envsExtPath))) {
85+
extensionIdCache.set(cacheKey, PYTHON_EXTENSION_ID);
7686
return PYTHON_EXTENSION_ID;
7787
}
7888

7989
for (const ext of otherExts) {
8090
const extPath = normalizePath(ext.extensionPath);
8191
if (filePaths.some((filePath) => filePath.startsWith(extPath))) {
92+
extensionIdCache.set(cacheKey, ext.id);
8293
return ext.id;
8394
}
8495
}
8596

86-
// Fallback - we're likely being called from Python extension in conda registration
97+
// Fallback - we're likely being called from Python extension or built-in managers
98+
traceWarn(
99+
`Could not determine calling extension from stack frames. ` +
100+
`Using fallback namespace '${PYTHON_EXTENSION_ID}'. ` +
101+
`Caller paths: ${filePaths.slice(0, 3).join(', ')}`,
102+
);
103+
extensionIdCache.set(cacheKey, PYTHON_EXTENSION_ID);
87104
return PYTHON_EXTENSION_ID;
88105
}

0 commit comments

Comments
 (0)