Skip to content

Commit 1896cde

Browse files
committed
add extra logging
1 parent 332d2ec commit 1896cde

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
lines changed

src/extension/common/application/commands/reportIssueCommand.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import { EXTENSION_ROOT_DIR } from '../../constants';
1010
import { sendTelemetryEvent } from '../../../telemetry';
1111
import { EventName } from '../../../telemetry/constants';
1212
import { PythonEnvironment } from '../../../envExtApi';
13+
import { traceLog } from '../../log/logging';
1314
import { getActiveEnvironmentPath, resolveEnvironment } from '../../python';
1415

1516
/**
1617
* Allows the user to report an issue related to the Python Debugger extension using our template.
1718
*/
1819
export async function openReportIssue(): Promise<void> {
20+
traceLog('openReportIssue: Starting report issue flow');
1921
const templatePath = path.join(EXTENSION_ROOT_DIR, 'resources', 'report_issue_template.md');
2022
const userDataTemplatePath = path.join(EXTENSION_ROOT_DIR, 'resources', 'report_issue_user_data_template.md');
2123
const template = await fs.readFile(templatePath, 'utf8');
@@ -30,11 +32,13 @@ export async function openReportIssue(): Promise<void> {
3032
}
3133
const virtualEnvKind = interpreter && interpreter.envId ? interpreter.envId.managerId : 'Unknown';
3234
const pythonVersion = interpreter?.version ?? 'unknown';
35+
traceLog(`openReportIssue: Resolved pythonVersion='${pythonVersion}' envKind='${virtualEnvKind}'`);
3336

3437
await executeCommand('workbench.action.openIssueReporter', {
3538
extensionId: 'ms-python.debugpy',
3639
issueBody: template,
3740
data: userTemplate.replace('{0}', pythonVersion).replace('{1}', virtualEnvKind),
3841
});
3942
sendTelemetryEvent(EventName.USE_REPORT_ISSUE_COMMAND, undefined, {});
43+
traceLog('openReportIssue: Issue reporter command executed');
4044
}

src/extension/common/legacyPython.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ export async function legacyInitializePython(
9999
onDidChangePythonInterpreterEvent: EventEmitter<LegacyIInterpreterDetails>,
100100
): Promise<void> {
101101
try {
102+
traceLog('legacyInitializePython: Starting initialization');
102103
const api = await legacyGetPythonExtensionEnviromentAPI();
103104

104105
if (api) {
105106
disposables.push(
106107
// This event is triggered when the active environment setting changes.
107108
api.environments.onDidChangeActiveEnvironmentPath((e: ActiveEnvironmentPathChangeEvent) => {
109+
traceLog(`legacyInitializePython: Active environment path changed to '${e.path}'`);
108110
let resourceUri: Uri | undefined;
109111
if (e.resource instanceof Uri) {
110112
resourceUri = e.resource;
@@ -119,6 +121,7 @@ export async function legacyInitializePython(
119121

120122
traceLog('Waiting for interpreter from python extension.');
121123
onDidChangePythonInterpreterEvent.fire(await legacyGetInterpreterDetails());
124+
traceLog('legacyInitializePython: Initial interpreter details fired');
122125
}
123126
} catch (error) {
124127
traceError('Error initializing python: ', error);
@@ -134,7 +137,9 @@ export async function legacyInitializePython(
134137
*/
135138
export async function legacyGetSettingsPythonPath(resource?: Uri): Promise<string[] | undefined> {
136139
const api = await legacyGetPythonExtensionAPI();
137-
return api?.settings.getExecutionDetails(resource).execCommand;
140+
const execCommand = api?.settings.getExecutionDetails(resource).execCommand;
141+
traceLog(`legacyGetSettingsPythonPath: execCommand='${execCommand?.join(' ')}' resource='${resource?.fsPath}'`);
142+
return execCommand;
138143
}
139144

140145
/**
@@ -157,7 +162,10 @@ export async function legacyResolveEnvironment(
157162
env: Environment | EnvironmentPath | string,
158163
): Promise<ResolvedEnvironment | undefined> {
159164
const api = await legacyGetPythonExtensionEnviromentAPI();
160-
return api.environments.resolveEnvironment(env);
165+
traceLog(`legacyResolveEnvironment: Resolving environment '${typeof env === 'string' ? env : (env as any).path}'`);
166+
const resolved = api.environments.resolveEnvironment(env);
167+
resolved.then((r) => traceLog(`legacyResolveEnvironment: Resolved executable='${r?.executable.uri?.fsPath}'`));
168+
return resolved;
161169
}
162170

163171
/**
@@ -168,7 +176,13 @@ export async function legacyResolveEnvironment(
168176
*/
169177
export async function legacyGetActiveEnvironmentPath(resource?: Resource): Promise<EnvironmentPath> {
170178
const api = await legacyGetPythonExtensionEnviromentAPI();
171-
return api.environments.getActiveEnvironmentPath(resource);
179+
const active = api.environments.getActiveEnvironmentPath(resource);
180+
traceLog(
181+
`legacyGetActiveEnvironmentPath: activePath='${active.path}' resource='${
182+
(resource as any)?.uri?.fsPath || (resource as Uri)?.fsPath || ''
183+
}'`,
184+
);
185+
return active;
172186
}
173187

174188
/**
@@ -180,8 +194,12 @@ export async function legacyGetInterpreterDetails(resource?: Uri): Promise<Legac
180194
const api = await legacyGetPythonExtensionEnviromentAPI();
181195
const environment = await api.environments.resolveEnvironment(api.environments.getActiveEnvironmentPath(resource));
182196
if (environment?.executable.uri) {
197+
traceLog(
198+
`legacyGetInterpreterDetails: executable='${environment.executable.uri.fsPath}' resource='${resource?.fsPath}'`,
199+
);
183200
return { path: [environment?.executable.uri.fsPath], resource };
184201
}
202+
traceLog('legacyGetInterpreterDetails: No executable found');
185203
return { path: undefined, resource };
186204
}
187205

@@ -199,12 +217,14 @@ export async function legacyHasInterpreters(): Promise<boolean> {
199217
});
200218
const initialEnvs = api.environments.known;
201219
if (initialEnvs.length > 0) {
220+
traceLog(`legacyHasInterpreters: Found ${initialEnvs.length} initial environments`);
202221
return true;
203222
}
204223
// Initiates a refresh of Python environments within the specified scope.
205224
await Promise.race([onAddedToCollection.promise, api?.environments.refreshEnvironments()]);
206-
207-
return api.environments.known.length > 0;
225+
const has = api.environments.known.length > 0;
226+
traceLog(`legacyHasInterpreters: After refresh count='${api.environments.known.length}' result='${has}'`);
227+
return has;
208228
}
209229

210230
/**
@@ -214,5 +234,7 @@ export async function legacyHasInterpreters(): Promise<boolean> {
214234
*/
215235
export async function legacyGetInterpreters(): Promise<readonly Environment[]> {
216236
const api = await legacyGetPythonExtensionEnviromentAPI();
217-
return api.environments.known || [];
237+
const known = api.environments.known || [];
238+
traceLog(`legacyGetInterpreters: returning ${known.length} environments`);
239+
return known;
218240
}

src/extension/common/python.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export async function getPythonEnvironmentExtensionAPI(): Promise<PythonEnvironm
6969
}
7070

7171
export async function initializePython(disposables: Disposable[]): Promise<void> {
72+
traceLog(`initializePython: usingEnvExt='${useEnvExtension()}'`);
7273
if (!useEnvExtension()) {
7374
await legacyInitializePython(disposables, onDidChangePythonInterpreterEvent);
7475
} else {
@@ -78,13 +79,16 @@ export async function initializePython(disposables: Disposable[]): Promise<void>
7879
disposables.push(
7980
api.onDidChangeEnvironments(async () => {
8081
// not sure if this is the right event....
81-
onDidChangePythonInterpreterEvent.fire(await getInterpreterDetails());
82-
traceLog('Python environments changed.');
82+
const details = await getInterpreterDetails();
83+
traceLog(`initializePython:onDidChangeEnvironments fired executable='${details.path?.[0]}'`);
84+
onDidChangePythonInterpreterEvent.fire(details);
85+
traceLog('Python environments changed event processed.');
8386
}),
8487
);
8588

8689
traceLog('Waiting for interpreter from python environments extension.');
8790
onDidChangePythonInterpreterEvent.fire(await getInterpreterDetails());
91+
traceLog('initializePython: Initial interpreter details fired (env extension path)');
8892
}
8993
} catch (error) {
9094
traceError('Error initializing python: ', error);
@@ -94,6 +98,7 @@ export async function initializePython(disposables: Disposable[]): Promise<void>
9498

9599
export async function runPythonExtensionCommand(command: string, ...rest: any[]) {
96100
await activateExtensions();
101+
traceLog(`runPythonExtensionCommand: executing command='${command}' argsCount='${rest.length}'`);
97102
return await commands.executeCommand(command, ...rest);
98103
}
99104

@@ -113,21 +118,33 @@ export async function getSettingsPythonPath(resource?: Uri): Promise<string[] |
113118
let pyEnv = await api.getEnvironment(resource);
114119

115120
if (!pyEnv) {
121+
traceLog(`getSettingsPythonPath: No environment for resource='${resource?.fsPath}'`);
116122
return undefined;
117123
}
118124

119125
// Resolve environment if execution info is not available
120126
if (!pyEnv.execInfo) {
121127
pyEnv = await api.resolveEnvironment(pyEnv.environmentPath);
128+
traceLog(
129+
`getSettingsPythonPath: Resolved environment execInfo for '${
130+
pyEnv?.environmentPath.fsPath || 'undefined'
131+
}'`,
132+
);
122133
}
123134

124135
// Extract execution command from resolved environment
125136
const execInfo = pyEnv?.execInfo;
126137
if (!execInfo) {
138+
traceLog('getSettingsPythonPath: Missing execInfo after resolution');
127139
return undefined;
128140
}
129141

130142
const runConfig = execInfo.activatedRun ?? execInfo.run;
143+
traceLog(
144+
`getSettingsPythonPath: Using executable='${runConfig.executable}' args='${
145+
runConfig.args?.join(' ') || ''
146+
}'`,
147+
);
131148
return runConfig.args ? [runConfig.executable, ...runConfig.args] : [runConfig.executable];
132149
}
133150
} // should I make this more async? rn it just becomes sync
@@ -157,10 +174,12 @@ export async function resolveEnvironment(
157174
: 'Unknown';
158175
const execUri = legacyResolvedEnv?.executable.uri;
159176
if (execUri === undefined) {
177+
traceLog('resolveEnvironment: legacy path invalid (no executable uri)');
160178
// Should return undefined for invalid environment
161179
return undefined;
162180
}
163181
if (legacyResolvedEnv) {
182+
traceLog(`resolveEnvironment: legacy resolved executable='${execUri.fsPath}' version='${pythonVersion}'`);
164183
const pythonEnv: PythonEnvironment = {
165184
envId: {
166185
id: execUri.fsPath,
@@ -187,11 +206,14 @@ export async function resolveEnvironment(
187206
// Handle different input types for the new API
188207
if (typeof env === 'string') {
189208
// Convert string path to Uri for the new API
209+
traceLog(`resolveEnvironment: new API resolving from string='${env}'`);
190210
return api.resolveEnvironment(Uri.file(env));
191211
} else if (typeof env === 'object' && 'path' in env) {
192212
// EnvironmentPath has a uri property
213+
traceLog(`resolveEnvironment: new API resolving from EnvironmentPath='${env.path}'`);
193214
return api.resolveEnvironment(Uri.file(env.path));
194215
} else {
216+
traceLog('resolveEnvironment: new API unsupported env input');
195217
return undefined;
196218
}
197219
}
@@ -204,6 +226,7 @@ export async function getActiveEnvironmentPath(
204226
//TODO: fix this return type??
205227
if (!useEnvExtension()) {
206228
const envPath: EnvironmentPath = await legacyGetActiveEnvironmentPath(resource);
229+
traceLog(`getActiveEnvironmentPath: legacy active path='${envPath.path}'`);
207230
return envPath;
208231
} else {
209232
const api = await getPythonEnvironmentExtensionAPI();
@@ -213,6 +236,9 @@ export async function getActiveEnvironmentPath(
213236
resource instanceof Uri ? resource : resource && 'uri' in resource ? resource.uri : undefined;
214237

215238
const env = await api.getEnvironment(resourceUri);
239+
traceLog(
240+
`getActiveEnvironmentPath: new API envPath='${env?.environmentPath.fsPath}' resource='${resourceUri?.fsPath}'`,
241+
);
216242
return env;
217243
}
218244
}
@@ -245,6 +271,7 @@ export async function getInterpreterDetails(resource?: Uri): Promise<IInterprete
245271
path: executablePath ? [executablePath] : undefined,
246272
resource,
247273
};
274+
traceLog(`getInterpreterDetails: resource='${resource?.fsPath}' executable='${executablePath}'`);
248275
return a;
249276
}
250277
}

src/extension/debugger/adapter/factory.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class DebugAdapterDescriptorFactory implements IDebugAdapterDescriptorFac
3838
session: DebugSession,
3939
_executable: DebugAdapterExecutable | undefined,
4040
): Promise<DebugAdapterDescriptor | undefined> {
41+
traceLog(`createDebugAdapterDescriptor: request='${session.configuration.request}' name='${session.name}'`);
4142
const configuration = session.configuration as LaunchRequestArguments | AttachRequestArguments;
4243

4344
// There are four distinct scenarios here:
@@ -67,9 +68,11 @@ export class DebugAdapterDescriptorFactory implements IDebugAdapterDescriptorFac
6768
} else if (configuration.listen === undefined && configuration.processId === undefined) {
6869
throw new Error('"request":"attach" requires either "connect", "listen", or "processId"');
6970
}
71+
traceLog('createDebugAdapterDescriptor: attach scenario using spawned adapter');
7072
}
7173

7274
const command = await this.getDebugAdapterPython(configuration, session.workspaceFolder);
75+
traceLog(`createDebugAdapterDescriptor: python command parts='${command.join(' ')}'`);
7376
if (command.length !== 0) {
7477
if (configuration.request === 'attach' && configuration.processId !== undefined) {
7578
sendTelemetryEvent(EventName.DEBUGGER_ATTACH_TO_LOCAL_PROCESS);
@@ -114,6 +117,7 @@ export class DebugAdapterDescriptorFactory implements IDebugAdapterDescriptorFac
114117
configuration: LaunchRequestArguments | AttachRequestArguments,
115118
workspaceFolder?: WorkspaceFolder,
116119
): Promise<string[]> {
120+
traceVerbose('getDebugAdapterPython: Resolving interpreter for debug adapter');
117121
if (configuration.debugAdapterPython !== undefined) {
118122
return this.getExecutableCommand(await resolveEnvironment(configuration.debugAdapterPython));
119123
} else if (configuration.pythonPath) {
@@ -202,6 +206,7 @@ export class DebugAdapterDescriptorFactory implements IDebugAdapterDescriptorFac
202206
if (major < 3 || (major <= 3 && minor < 9)) {
203207
this.showDeprecatedPythonMessage();
204208
}
209+
traceLog(`getExecutableCommand: executable='${executablePath}' version='${version}'`);
205210
return executablePath ? [executablePath] : [];
206211
}
207212
return [];

src/extension/debugger/configuration/debugConfigurationService.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { buildRemoteAttachConfiguration } from './providers/remoteAttach';
1818
import { IDebugConfigurationResolver } from './types';
1919
import { buildFileWithArgsLaunchDebugConfiguration } from './providers/fileLaunchWithArgs';
2020
import { getInterpreterDetails } from '../../common/python';
21+
import { traceLog } from '../../common/log/logging';
2122

2223
export class PythonDebugConfigurationService implements IDebugConfigurationService {
2324
private cacheDebugConfig: DebugConfiguration | undefined = undefined;
@@ -96,8 +97,14 @@ export class PythonDebugConfigurationService implements IDebugConfigurationServi
9697
if (debugConfiguration.python === undefined) {
9798
// If program is a valid file, get interpreter for that file
9899
if (fs.existsSync(debugConfiguration.program) && fs.statSync(debugConfiguration.program).isFile()) {
100+
traceLog(
101+
`resolveDebugConfigurationWithSubstitutedVariables: resolving interpreter for program='${debugConfiguration.program}'`,
102+
);
99103
const interpreter = await getInterpreterDetails(Uri.file(debugConfiguration.program));
100104
if (interpreter?.path && interpreter.path.length > 0) {
105+
traceLog(
106+
`resolveDebugConfigurationWithSubstitutedVariables: setting debugConfiguration.python='${interpreter.path[0]}'`,
107+
);
101108
debugConfiguration.python = interpreter.path[0];
102109
}
103110
}

0 commit comments

Comments
 (0)