Skip to content

Commit 150cf62

Browse files
Kartik RajDonJayamanne
authored andcommitted
Fixing validation of 'Improvements to message displayed when python path is invalid (in launch.json)' (microsoft#4136)
* added pythonPathSource information * Code reviews
1 parent 42f7c01 commit 150cf62

File tree

7 files changed

+23
-16
lines changed

7 files changed

+23
-16
lines changed

src/client/application/diagnostics/checks/invalidPythonPathInDebugger.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { traceError } from '../../../common/logger';
1313
import { IConfigurationService, Resource } from '../../../common/types';
1414
import { Diagnostics } from '../../../common/utils/localize';
1515
import { SystemVariables } from '../../../common/variables/systemVariables';
16+
import { PythonPathSource } from '../../../debugger/extension/types';
1617
import { IInterpreterHelper } from '../../../interpreter/contracts';
1718
import { IServiceContainer } from '../../../ioc/types';
1819
import { BaseDiagnostic, BaseDiagnosticsService } from '../base';
@@ -74,19 +75,17 @@ export class InvalidPythonPathInDebuggerService extends BaseDiagnosticsService
7475
public async diagnose(_resource: Resource): Promise<IDiagnostic[]> {
7576
return [];
7677
}
77-
public async validatePythonPath(pythonPath?: string, resource?: Uri) {
78+
public async validatePythonPath(pythonPath?: string, pythonPathSource?: PythonPathSource, resource?: Uri) {
7879
pythonPath = pythonPath ? this.resolveVariables(pythonPath, resource) : undefined;
79-
let pathInLaunchJson = true;
8080
// tslint:disable-next-line:no-invalid-template-strings
8181
if (pythonPath === '${config:python.pythonPath}' || !pythonPath) {
82-
pathInLaunchJson = false;
8382
pythonPath = this.configService.getSettings(resource).pythonPath;
8483
}
8584
if (await this.interpreterHelper.getInterpreterInformation(pythonPath).catch(() => undefined)) {
8685
return true;
8786
}
8887
traceError(`Invalid Python Path '${pythonPath}'`);
89-
if (pathInLaunchJson) {
88+
if (pythonPathSource === PythonPathSource.launchJson) {
9089
this.handle([new InvalidPythonPathInDebuggerDiagnostic(DiagnosticCodes.InvalidPythonPathInDebuggerLaunchDiagnostic, resource)])
9190
.catch(ex => traceError('Failed to handle invalid python path in launch.json debugger', ex))
9291
.ignoreErrors();

src/client/application/diagnostics/types.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import { DiagnosticSeverity, Uri } from 'vscode';
77
import { Resource } from '../../common/types';
8+
import { PythonPathSource } from '../../debugger/extension/types';
89
import { DiagnosticCodes } from './constants';
910

1011
export enum DiagnosticScope {
@@ -35,10 +36,6 @@ export interface IDiagnosticsService {
3536
handle(diagnostics: IDiagnostic[]): Promise<void>;
3637
}
3738

38-
export interface IInvalidPythonPathInDebuggerService extends IDiagnosticsService {
39-
validatePythonPath(pythonPath?: string, resource?: Uri): Promise<boolean>;
40-
}
41-
4239
export const IDiagnosticFilterService = Symbol('IDiagnosticFilterService');
4340

4441
export interface IDiagnosticFilterService {
@@ -60,7 +57,7 @@ export interface IDiagnosticCommand {
6057
export const IInvalidPythonPathInDebuggerService = Symbol('IInvalidPythonPathInDebuggerService');
6158

6259
export interface IInvalidPythonPathInDebuggerService extends IDiagnosticsService {
63-
validatePythonPath(pythonPath?: string, resource?: Uri): Promise<boolean>;
60+
validatePythonPath(pythonPath?: string, pythonPathSource?: PythonPathSource, resource?: Uri): Promise<boolean>;
6461
}
6562
export const ISourceMapSupportService = Symbol('ISourceMapSupportService');
6663
export interface ISourceMapSupportService {

src/client/debugger/extension/configuration/resolvers/base.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import { sendTelemetryEvent } from '../../../../telemetry';
1515
import { EventName } from '../../../../telemetry/constants';
1616
import { DebuggerTelemetry } from '../../../../telemetry/types';
1717
import { AttachRequestArguments, DebugOptions, LaunchRequestArguments } from '../../../types';
18+
import { PythonPathSource } from '../../types';
1819
import { IDebugConfigurationResolver } from '../types';
1920

2021
@injectable()
2122
export abstract class BaseConfigurationResolver<T extends DebugConfiguration> implements IDebugConfigurationResolver<T> {
23+
protected pythonPathSource: PythonPathSource;
2224
constructor(protected readonly workspaceService: IWorkspaceService,
2325
protected readonly documentManager: IDocumentManager,
2426
protected readonly configurationService: IConfigurationService) { }
@@ -54,6 +56,9 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration> im
5456
if (debugConfiguration.pythonPath === '${config:python.pythonPath}' || !debugConfiguration.pythonPath) {
5557
const pythonPath = this.configurationService.getSettings(workspaceFolder).pythonPath;
5658
debugConfiguration.pythonPath = pythonPath;
59+
this.pythonPathSource = PythonPathSource.settingsJson;
60+
} else{
61+
this.pythonPathSource = PythonPathSource.launchJson;
5762
}
5863
}
5964
protected debugOption(debugOptions: DebugOptions[], debugOption: DebugOptions) {

src/client/debugger/extension/configuration/resolvers/launch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,6 @@ export class LaunchConfigurationResolver extends BaseConfigurationResolver<Launc
124124

125125
protected async validateLaunchConfiguration(folder: WorkspaceFolder | undefined, debugConfiguration: LaunchRequestArguments): Promise<boolean> {
126126
const diagnosticService = this.invalidPythonPathInDebuggerService;
127-
return diagnosticService.validatePythonPath(debugConfiguration.pythonPath, folder ? folder.uri : undefined);
127+
return diagnosticService.validatePythonPath(debugConfiguration.pythonPath, this.pythonPathSource, folder ? folder.uri : undefined);
128128
}
129129
}

src/client/debugger/extension/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ export enum DebugConfigurationType {
2929
launchModule = 'launchModule',
3030
launchPyramid = 'launchPyramid'
3131
}
32+
33+
export enum PythonPathSource {
34+
launchJson = 'launch.json',
35+
settingsJson = 'settings.json'
36+
}

src/test/application/diagnostics/checks/invalidPythonPathInDebugger.unit.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from '../../../../client/application/diagnostics/types';
2626
import { IWorkspaceService } from '../../../../client/common/application/types';
2727
import { IConfigurationService, IPythonSettings } from '../../../../client/common/types';
28+
import { PythonPathSource } from '../../../../client/debugger/extension/types';
2829
import { IInterpreterHelper } from '../../../../client/interpreter/contracts';
2930
import { IServiceContainer } from '../../../../client/ioc/types';
3031

@@ -271,7 +272,7 @@ suite('Application Diagnostics - Checks Python Path in debugger', () => {
271272
.returns(() => Promise.resolve({}))
272273
.verifiable(typemoq.Times.once());
273274

274-
const valid = await diagnosticService.validatePythonPath(pythonPath, Uri.parse('something'));
275+
const valid = await diagnosticService.validatePythonPath(pythonPath, PythonPathSource.settingsJson, Uri.parse('something'));
275276

276277
configService.verifyAll();
277278
helper.verifyAll();
@@ -362,7 +363,7 @@ suite('Application Diagnostics - Checks Python Path in debugger', () => {
362363
.returns(() => Promise.resolve(undefined))
363364
.verifiable(typemoq.Times.once());
364365

365-
const valid = await diagnosticService.validatePythonPath(pythonPath);
366+
const valid = await diagnosticService.validatePythonPath(pythonPath, PythonPathSource.launchJson);
366367

367368
helper.verifyAll();
368369
expect(valid).to.be.equal(false, 'should be invalid');
@@ -394,7 +395,7 @@ suite('Application Diagnostics - Checks Python Path in debugger', () => {
394395
.returns(() => Promise.resolve(undefined))
395396
.verifiable(typemoq.Times.once());
396397

397-
const valid = await diagnosticService.validatePythonPath(pythonPath);
398+
const valid = await diagnosticService.validatePythonPath(pythonPath, PythonPathSource.settingsJson);
398399

399400
helper.verifyAll();
400401
expect(valid).to.be.equal(false, 'should be invalid');

src/test/debugger/extension/configuration/resolvers/launch.unit.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ suite('Debugging - Config Resolver Launch', () => {
6060
factory.setup(f => f.create(TypeMoq.It.isAny())).returns(() => Promise.resolve(pythonExecutionService.object));
6161
helper.setup(h => h.getInterpreterInformation(TypeMoq.It.isAny())).returns(() => Promise.resolve({}));
6262
diagnosticsService
63-
.setup(h => h.validatePythonPath(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
63+
.setup(h => h.validatePythonPath(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
6464
.returns(() => Promise.resolve(true));
6565

6666
const configProviderUtils = new ConfigurationProviderUtils(factory.object, fileSystem.object, appShell.object);
@@ -425,7 +425,7 @@ suite('Debugging - Config Resolver Launch', () => {
425425

426426
diagnosticsService.reset();
427427
diagnosticsService
428-
.setup(h => h.validatePythonPath(TypeMoq.It.isValue(pythonPath), TypeMoq.It.isAny()))
428+
.setup(h => h.validatePythonPath(TypeMoq.It.isValue(pythonPath), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
429429
.returns(() => Promise.resolve(false))
430430
.verifiable(TypeMoq.Times.once());
431431

@@ -443,7 +443,7 @@ suite('Debugging - Config Resolver Launch', () => {
443443

444444
diagnosticsService.reset();
445445
diagnosticsService
446-
.setup(h => h.validatePythonPath(TypeMoq.It.isValue(pythonPath), TypeMoq.It.isAny()))
446+
.setup(h => h.validatePythonPath(TypeMoq.It.isValue(pythonPath), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
447447
.returns(() => Promise.resolve(true))
448448
.verifiable(TypeMoq.Times.once());
449449

0 commit comments

Comments
 (0)