Skip to content

Commit 29652d3

Browse files
committed
Separate output channel for native finder (#23648)
Will be very useful for diagnostics, and the likel <img width="1088" alt="Screenshot 2024-06-20 at 18 02 57" src="https://github.com/user-attachments/assets/afee4985-3f8f-4959-8c31-43d8f9e3aedc">
1 parent 1503558 commit 29652d3

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/client/pythonEnvironments/base/locators/common/nativePythonFinder.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { Disposable, EventEmitter, Event, workspace, Uri } from 'vscode';
4+
import { Disposable, EventEmitter, Event, workspace, window, Uri } from 'vscode';
55
import * as ch from 'child_process';
66
import * as path from 'path';
77
import * as rpc from 'vscode-jsonrpc/node';
88
import { PassThrough } from 'stream';
99
import { isWindows } from '../../../../common/platform/platformService';
1010
import { EXTENSION_ROOT_DIR } from '../../../../constants';
11-
import { traceError, traceInfo, traceLog, traceVerbose, traceWarn } from '../../../../logging';
1211
import { createDeferred, createDeferredFrom } from '../../../../common/utils/async';
1312
import { DisposableBase, DisposableStore } from '../../../../common/utils/resourceLifecycle';
1413
import { DEFAULT_INTERPRETER_PATH_SETTING_KEY } from '../lowLevel/customWorkspaceLocator';
@@ -61,6 +60,8 @@ class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGloba
6160

6261
private firstRefreshResults: undefined | (() => AsyncGenerator<NativeEnvInfo, void, unknown>);
6362

63+
private readonly outputChannel = this._register(window.createOutputChannel('Python Locator', { log: true }));
64+
6465
constructor() {
6566
super();
6667
this.connection = this.start();
@@ -75,7 +76,7 @@ class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGloba
7576
executable,
7677
});
7778

78-
traceInfo(`Resolved Python Environment ${environment.executable} in ${duration}ms`);
79+
this.outputChannel.info(`Resolved Python Environment ${environment.executable} in ${duration}ms`);
7980
return environment;
8081
}
8182

@@ -152,17 +153,15 @@ class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGloba
152153

153154
// eslint-disable-next-line class-methods-use-this
154155
private start(): rpc.MessageConnection {
156+
this.outputChannel.info(`Starting Python Locator ${NATIVE_LOCATOR} server`);
155157
const proc = ch.spawn(NATIVE_LOCATOR, ['server'], { env: process.env });
156158
const disposables: Disposable[] = [];
157159
// jsonrpc package cannot handle messages coming through too quickly.
158160
// Lets handle the messages and close the stream only when
159161
// we have got the exit event.
160162
const readable = new PassThrough();
161163
proc.stdout.pipe(readable, { end: false });
162-
proc.stderr.on('data', (data) => {
163-
const err = data.toString();
164-
traceError('Native Python Finder', err);
165-
});
164+
proc.stderr.on('data', (data) => this.outputChannel.error(data.toString()));
166165
const writable = new PassThrough();
167166
writable.pipe(proc.stdin, { end: false });
168167
const disposeStreams = new Disposable(() => {
@@ -178,24 +177,24 @@ class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGloba
178177
disposeStreams,
179178
connection.onError((ex) => {
180179
disposeStreams.dispose();
181-
traceError('Error in Native Python Finder', ex);
180+
this.outputChannel.error('Connection Error:', ex);
182181
}),
183182
connection.onNotification('log', (data: NativeLog) => {
184183
switch (data.level) {
185184
case 'info':
186-
traceInfo(`Native Python Finder: ${data.message}`);
185+
this.outputChannel.info(data.message);
187186
break;
188187
case 'warning':
189-
traceWarn(`Native Python Finder: ${data.message}`);
188+
this.outputChannel.warn(data.message);
190189
break;
191190
case 'error':
192-
traceError(`Native Python Finder: ${data.message}`);
191+
this.outputChannel.error(data.message);
193192
break;
194193
case 'debug':
195-
traceVerbose(`Native Python Finder: ${data.message}`);
194+
this.outputChannel.debug(data.message);
196195
break;
197196
default:
198-
traceLog(`Native Python Finder: ${data.message}`);
197+
this.outputChannel.trace(data.message);
199198
}
200199
}),
201200
connection.onClose(() => {
@@ -208,7 +207,7 @@ class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGloba
208207
proc.kill();
209208
}
210209
} catch (ex) {
211-
traceVerbose('Error while disposing Native Python Finder', ex);
210+
this.outputChannel.error('Error disposing finder', ex);
212211
}
213212
},
214213
},
@@ -244,6 +243,8 @@ class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGloba
244243

245244
disposable.add(
246245
this.connection.onNotification('environment', (data: NativeEnvInfo) => {
246+
this.outputChannel.info(`Discovered env: ${data.executable || data.executable}`);
247+
this.outputChannel.trace(`Discovered env info:\n ${JSON.stringify(data, undefined, 4)}`);
247248
// We know that in the Python extension if either Version of Prefix is not provided by locator
248249
// Then we end up resolving the information.
249250
// Lets do that here,
@@ -259,10 +260,11 @@ class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGloba
259260
executable: data.executable,
260261
})
261262
.then(({ environment, duration }) => {
262-
traceInfo(`Resolved Python Environment ${environment.executable} in ${duration}ms`);
263+
this.outputChannel.info(`Resolved ${environment.executable} in ${duration}ms`);
264+
this.outputChannel.trace(`Environment resolved:\n ${JSON.stringify(data, undefined, 4)}`);
263265
discovered.fire(environment);
264266
})
265-
.catch((ex) => traceError(`Error in Resolving Python Environment ${JSON.stringify(data)}`, ex));
267+
.catch((ex) => this.outputChannel.error(`Error in Resolving ${JSON.stringify(data)}`, ex));
266268
trackPromiseAndNotifyOnCompletion(promise);
267269
} else {
268270
discovered.fire(data);
@@ -272,8 +274,8 @@ class NativeGlobalPythonFinderImpl extends DisposableBase implements NativeGloba
272274

273275
trackPromiseAndNotifyOnCompletion(
274276
this.sendRefreshRequest()
275-
.then(({ duration }) => traceInfo(`Native Python Finder completed in ${duration}ms`))
276-
.catch((ex) => traceError('Error in Native Python Finder', ex)),
277+
.then(({ duration }) => this.outputChannel.info(`Refresh completed in ${duration}ms`))
278+
.catch((ex) => this.outputChannel.error('Refresh error', ex)),
277279
);
278280

279281
completed.promise.finally(() => disposable.dispose());

0 commit comments

Comments
 (0)