forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinvalidPythonPathInDebugger.ts
More file actions
177 lines (164 loc) · 7.29 KB
/
invalidPythonPathInDebugger.ts
File metadata and controls
177 lines (164 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// eslint-disable-next-line max-classes-per-file
import { inject, injectable, named } from 'inversify';
import * as path from 'path';
import { DiagnosticSeverity, Uri, workspace as workspc, WorkspaceFolder } from 'vscode';
import { IDocumentManager, IWorkspaceService } from '../../../common/application/types';
import '../../../common/extensions';
import { IConfigurationService, IDisposableRegistry, Resource } from '../../../common/types';
import { Common, Diagnostics } from '../../../common/utils/localize';
import { SystemVariables } from '../../../common/variables/systemVariables';
import { PythonPathSource } from '../../../debugger/extension/types';
import { IInterpreterHelper } from '../../../interpreter/contracts';
import { IServiceContainer } from '../../../ioc/types';
import { traceError } from '../../../logging';
import { BaseDiagnostic, BaseDiagnosticsService } from '../base';
import { IDiagnosticsCommandFactory } from '../commands/types';
import { DiagnosticCodes } from '../constants';
import { DiagnosticCommandPromptHandlerServiceId, MessageCommandPrompt } from '../promptHandler';
import {
DiagnosticScope,
IDiagnostic,
IDiagnosticCommand,
IDiagnosticHandlerService,
IInvalidPythonPathInDebuggerService,
} from '../types';
const messages = {
[DiagnosticCodes.InvalidPythonPathInDebuggerSettingsDiagnostic]: Diagnostics.invalidPythonPathInDebuggerSettings,
[DiagnosticCodes.InvalidPythonPathInDebuggerLaunchDiagnostic]: Diagnostics.invalidPythonPathInDebuggerLaunch,
};
class InvalidPythonPathInDebuggerDiagnostic extends BaseDiagnostic {
constructor(
code:
| DiagnosticCodes.InvalidPythonPathInDebuggerLaunchDiagnostic
| DiagnosticCodes.InvalidPythonPathInDebuggerSettingsDiagnostic,
resource: Resource,
) {
super(
code,
messages[code],
DiagnosticSeverity.Error,
DiagnosticScope.WorkspaceFolder,
resource,
undefined,
'always',
);
}
}
export const InvalidPythonPathInDebuggerServiceId = 'InvalidPythonPathInDebuggerServiceId';
@injectable()
export class InvalidPythonPathInDebuggerService
extends BaseDiagnosticsService
implements IInvalidPythonPathInDebuggerService
{
constructor(
@inject(IServiceContainer) serviceContainer: IServiceContainer,
@inject(IWorkspaceService) private readonly workspace: IWorkspaceService,
@inject(IDiagnosticsCommandFactory) private readonly commandFactory: IDiagnosticsCommandFactory,
@inject(IInterpreterHelper) private readonly interpreterHelper: IInterpreterHelper,
@inject(IDocumentManager) private readonly documentManager: IDocumentManager,
@inject(IConfigurationService) private readonly configService: IConfigurationService,
@inject(IDisposableRegistry) disposableRegistry: IDisposableRegistry,
@inject(IDiagnosticHandlerService)
@named(DiagnosticCommandPromptHandlerServiceId)
protected readonly messageService: IDiagnosticHandlerService<MessageCommandPrompt>,
) {
super(
[
DiagnosticCodes.InvalidPythonPathInDebuggerSettingsDiagnostic,
DiagnosticCodes.InvalidPythonPathInDebuggerLaunchDiagnostic,
],
serviceContainer,
disposableRegistry,
true,
);
}
// eslint-disable-next-line class-methods-use-this
public async diagnose(): Promise<IDiagnostic[]> {
return [];
}
public async validatePythonPath(
pythonPath?: string,
pythonPathSource?: PythonPathSource,
resource?: Uri,
): Promise<boolean> {
pythonPath = pythonPath ? this.resolveVariables(pythonPath, resource) : undefined;
if (pythonPath === '${command:python.interpreterPath}' || !pythonPath) {
pythonPath = this.configService.getSettings(resource).pythonPath;
}
if (await this.interpreterHelper.getInterpreterInformation(pythonPath).catch(() => undefined)) {
return true;
}
traceError(`Invalid Python Path '${pythonPath}'`);
if (pythonPathSource === PythonPathSource.launchJson) {
this.handle([
new InvalidPythonPathInDebuggerDiagnostic(
DiagnosticCodes.InvalidPythonPathInDebuggerLaunchDiagnostic,
resource,
),
])
.catch((ex) => traceError('Failed to handle invalid python path in launch.json debugger', ex))
.ignoreErrors();
} else {
this.handle([
new InvalidPythonPathInDebuggerDiagnostic(
DiagnosticCodes.InvalidPythonPathInDebuggerSettingsDiagnostic,
resource,
),
])
.catch((ex) => traceError('Failed to handle invalid python path in settings.json debugger', ex))
.ignoreErrors();
}
return false;
}
protected async onHandle(diagnostics: IDiagnostic[]): Promise<void> {
// This class can only handle one type of diagnostic, hence just use first item in list.
if (diagnostics.length === 0 || !this.canHandle(diagnostics[0])) {
return;
}
const diagnostic = diagnostics[0];
const commandPrompts = this.getCommandPrompts(diagnostic);
await this.messageService.handle(diagnostic, { commandPrompts });
}
protected resolveVariables(pythonPath: string, resource: Uri | undefined): string {
const systemVariables = new SystemVariables(resource, undefined, this.workspace);
return systemVariables.resolveAny(pythonPath);
}
private getCommandPrompts(diagnostic: IDiagnostic): { prompt: string; command?: IDiagnosticCommand }[] {
switch (diagnostic.code) {
case DiagnosticCodes.InvalidPythonPathInDebuggerSettingsDiagnostic: {
return [
{
prompt: Common.selectPythonInterpreter,
command: this.commandFactory.createCommand(diagnostic, {
type: 'executeVSCCommand',
options: 'python.setInterpreter',
}),
},
];
}
case DiagnosticCodes.InvalidPythonPathInDebuggerLaunchDiagnostic: {
return [
{
prompt: Common.openLaunch,
command: {
diagnostic,
invoke: async (): Promise<void> => {
const launchJson = getLaunchJsonFile(workspc.workspaceFolders![0]);
const doc = await this.documentManager.openTextDocument(launchJson);
await this.documentManager.showTextDocument(doc);
},
},
},
];
}
default: {
throw new Error("Invalid diagnostic for 'InvalidPythonPathInDebuggerService'");
}
}
}
}
function getLaunchJsonFile(workspaceFolder: WorkspaceFolder) {
return path.join(workspaceFolder.uri.fsPath, '.vscode', 'launch.json');
}