Skip to content

Commit 83be83c

Browse files
paulacamargo25wesm
authored andcommitted
19767 remove di in resolvers (microsoft/vscode-python#20048)
Closed: microsoft/vscode-python#19767
1 parent 182f449 commit 83be83c

File tree

12 files changed

+172
-202
lines changed

12 files changed

+172
-202
lines changed

extensions/positron-python/src/client/debugger/extension/configuration/providers/djangoLaunch.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ export async function validateManagePy(
6565
return error;
6666
}
6767
const resolvedPath = resolveVariables(selected, undefined, folder);
68-
69-
if (selected !== defaultValue && !(await fs.pathExists(resolvedPath))) {
70-
return error;
71-
}
72-
if (!resolvedPath.trim().toLowerCase().endsWith('.py')) {
73-
return error;
68+
if (resolvedPath) {
69+
if (selected !== defaultValue && !(await fs.pathExists(resolvedPath))) {
70+
return error;
71+
}
72+
if (!resolvedPath.trim().toLowerCase().endsWith('.py')) {
73+
return error;
74+
}
7475
}
7576
return;
7677
}

extensions/positron-python/src/client/debugger/extension/configuration/providers/pyramidLaunch.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ export async function validateIniPath(
7777
return error;
7878
}
7979
const resolvedPath = resolveVariables(selected, undefined, folder);
80-
if (selected !== defaultValue && !fs.pathExists(resolvedPath)) {
81-
return error;
82-
}
83-
if (!resolvedPath.trim().toLowerCase().endsWith('.ini')) {
84-
return error;
80+
if (resolvedPath) {
81+
if (selected !== defaultValue && !fs.pathExists(resolvedPath)) {
82+
return error;
83+
}
84+
if (!resolvedPath.trim().toLowerCase().endsWith('.ini')) {
85+
return error;
86+
}
8587
}
8688
}
8789

extensions/positron-python/src/client/debugger/extension/configuration/resolvers/attach.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,14 @@
33

44
'use strict';
55

6-
import { inject, injectable } from 'inversify';
6+
import { injectable } from 'inversify';
77
import { CancellationToken, Uri, WorkspaceFolder } from 'vscode';
8-
import { IDocumentManager, IWorkspaceService } from '../../../../common/application/types';
9-
import { IPlatformService } from '../../../../common/platform/types';
10-
import { IConfigurationService } from '../../../../common/types';
11-
import { IInterpreterService } from '../../../../interpreter/contracts';
8+
import { getOSType, OSType } from '../../../../common/utils/platform';
129
import { AttachRequestArguments, DebugOptions, PathMapping } from '../../../types';
1310
import { BaseConfigurationResolver } from './base';
1411

1512
@injectable()
1613
export class AttachConfigurationResolver extends BaseConfigurationResolver<AttachRequestArguments> {
17-
constructor(
18-
@inject(IWorkspaceService) workspaceService: IWorkspaceService,
19-
@inject(IDocumentManager) documentManager: IDocumentManager,
20-
@inject(IPlatformService) platformService: IPlatformService,
21-
@inject(IConfigurationService) configurationService: IConfigurationService,
22-
@inject(IInterpreterService) interpreterService: IInterpreterService,
23-
) {
24-
super(workspaceService, documentManager, platformService, configurationService, interpreterService);
25-
}
26-
2714
public async resolveDebugConfigurationWithSubstitutedVariables(
2815
folder: WorkspaceFolder | undefined,
2916
debugConfiguration: AttachRequestArguments,
@@ -87,10 +74,10 @@ export class AttachConfigurationResolver extends BaseConfigurationResolver<Attac
8774
// We'll need paths to be fixed only in the case where local and remote hosts are the same
8875
// I.e. only if hostName === 'localhost' or '127.0.0.1' or ''
8976
const isLocalHost = this.isLocalHost(debugConfiguration.host);
90-
if (this.platformService.isWindows && isLocalHost) {
77+
if (getOSType() == OSType.Windows && isLocalHost) {
9178
this.debugOption(debugOptions, DebugOptions.FixFilePathCase);
9279
}
93-
if (this.platformService.isWindows) {
80+
if (getOSType() == OSType.Windows) {
9481
this.debugOption(debugOptions, DebugOptions.WindowsClient);
9582
} else {
9683
this.debugOption(debugOptions, DebugOptions.UnixClient);

extensions/positron-python/src/client/debugger/extension/configuration/resolvers/base.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,25 @@
66
import { injectable } from 'inversify';
77
import * as path from 'path';
88
import { CancellationToken, DebugConfiguration, Uri, WorkspaceFolder } from 'vscode';
9-
import { IDocumentManager, IWorkspaceService } from '../../../../common/application/types';
109
import { PYTHON_LANGUAGE } from '../../../../common/constants';
11-
import { IPlatformService } from '../../../../common/platform/types';
1210
import { IConfigurationService } from '../../../../common/types';
13-
import { SystemVariables } from '../../../../common/variables/systemVariables';
11+
import { getOSType, OSType } from '../../../../common/utils/platform';
1412
import { IInterpreterService } from '../../../../interpreter/contracts';
1513
import { sendTelemetryEvent } from '../../../../telemetry';
1614
import { EventName } from '../../../../telemetry/constants';
1715
import { DebuggerTelemetry } from '../../../../telemetry/types';
1816
import { AttachRequestArguments, DebugOptions, LaunchRequestArguments, PathMapping } from '../../../types';
1917
import { PythonPathSource } from '../../types';
2018
import { IDebugConfigurationResolver } from '../types';
19+
import { getActiveTextEditor, resolveVariables } from '../utils/common';
20+
import { getWorkspaceFolder as getVSCodeWorkspaceFolder, getWorkspaceFolders } from '../utils/workspaceFolder';
2121

2222
@injectable()
2323
export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
2424
implements IDebugConfigurationResolver<T> {
2525
protected pythonPathSource: PythonPathSource = PythonPathSource.launchJson;
2626

2727
constructor(
28-
protected readonly workspaceService: IWorkspaceService,
29-
protected readonly documentManager: IDocumentManager,
30-
protected readonly platformService: IPlatformService,
3128
protected readonly configurationService: IConfigurationService,
3229
protected readonly interpreterService: IInterpreterService,
3330
) {}
@@ -59,27 +56,26 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
5956
return folder.uri;
6057
}
6158
const program = this.getProgram();
62-
if (
63-
!Array.isArray(this.workspaceService.workspaceFolders) ||
64-
this.workspaceService.workspaceFolders.length === 0
65-
) {
59+
let workspaceFolders = getWorkspaceFolders();
60+
61+
if (!Array.isArray(workspaceFolders) || workspaceFolders.length === 0) {
6662
return program ? Uri.file(path.dirname(program)) : undefined;
6763
}
68-
if (this.workspaceService.workspaceFolders.length === 1) {
69-
return this.workspaceService.workspaceFolders[0].uri;
64+
if (workspaceFolders.length === 1) {
65+
return workspaceFolders[0].uri;
7066
}
7167
if (program) {
72-
const workspaceFolder = this.workspaceService.getWorkspaceFolder(Uri.file(program));
68+
const workspaceFolder = getVSCodeWorkspaceFolder(Uri.file(program));
7369
if (workspaceFolder) {
7470
return workspaceFolder.uri;
7571
}
7672
}
7773
}
7874

7975
protected getProgram(): string | undefined {
80-
const editor = this.documentManager.activeTextEditor;
81-
if (editor && editor.document.languageId === PYTHON_LANGUAGE) {
82-
return editor.document.fileName;
76+
const activeTextEditor = getActiveTextEditor();
77+
if (activeTextEditor && activeTextEditor.document.languageId === PYTHON_LANGUAGE) {
78+
return activeTextEditor.document.fileName;
8379
}
8480
}
8581

@@ -99,11 +95,11 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
9995
return;
10096
}
10197
if (debugConfiguration.envFile && (workspaceFolder || debugConfiguration.cwd)) {
102-
const systemVariables = new SystemVariables(
103-
undefined,
98+
debugConfiguration.envFile = resolveVariables(
99+
debugConfiguration.envFile,
104100
(workspaceFolder ? workspaceFolder.fsPath : undefined) || debugConfiguration.cwd,
101+
undefined,
105102
);
106-
debugConfiguration.envFile = systemVariables.resolveAny(debugConfiguration.envFile);
107103
}
108104
}
109105

@@ -114,25 +110,28 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
114110
if (!debugConfiguration) {
115111
return;
116112
}
117-
const systemVariables: SystemVariables = new SystemVariables(
118-
undefined,
119-
workspaceFolder?.fsPath,
120-
this.workspaceService,
121-
);
122113
if (debugConfiguration.pythonPath === '${command:python.interpreterPath}' || !debugConfiguration.pythonPath) {
123114
const interpreterPath =
124115
(await this.interpreterService.getActiveInterpreter(workspaceFolder))?.path ??
125116
this.configurationService.getSettings(workspaceFolder).pythonPath;
126117
debugConfiguration.pythonPath = interpreterPath;
127118
} else {
128-
debugConfiguration.pythonPath = systemVariables.resolveAny(debugConfiguration.pythonPath);
119+
debugConfiguration.pythonPath = resolveVariables(
120+
debugConfiguration.pythonPath ? debugConfiguration.pythonPath : undefined,
121+
workspaceFolder?.fsPath,
122+
undefined,
123+
);
129124
}
130125
if (debugConfiguration.python === '${command:python.interpreterPath}' || !debugConfiguration.python) {
131126
this.pythonPathSource = PythonPathSource.settingsJson;
132127
} else {
133128
this.pythonPathSource = PythonPathSource.launchJson;
134129
}
135-
debugConfiguration.python = systemVariables.resolveAny(debugConfiguration.python);
130+
debugConfiguration.python = resolveVariables(
131+
debugConfiguration.python ? debugConfiguration.python : undefined,
132+
workspaceFolder?.fsPath,
133+
undefined,
134+
);
136135
}
137136

138137
protected debugOption(debugOptions: DebugOptions[], debugOption: DebugOptions) {
@@ -168,17 +167,19 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
168167
];
169168
} else {
170169
// Expand ${workspaceFolder} variable first if necessary.
171-
const systemVariables = new SystemVariables(undefined, defaultLocalRoot);
172-
pathMappings = pathMappings.map(({ localRoot: mappedLocalRoot, remoteRoot }) => ({
173-
localRoot: systemVariables.resolveAny(mappedLocalRoot),
174-
// TODO: Apply to remoteRoot too?
175-
remoteRoot,
176-
}));
170+
pathMappings = pathMappings.map(({ localRoot: mappedLocalRoot, remoteRoot }) => {
171+
let resolvedLocalRoot = resolveVariables(mappedLocalRoot, defaultLocalRoot, undefined);
172+
return {
173+
localRoot: resolvedLocalRoot ? resolvedLocalRoot : '',
174+
// TODO: Apply to remoteRoot too?
175+
remoteRoot,
176+
};
177+
});
177178
}
178179

179180
// If on Windows, lowercase the drive letter for path mappings.
180181
// TODO: Apply even if no localRoot?
181-
if (this.platformService.isWindows) {
182+
if (getOSType() == OSType.Windows) {
182183
// TODO: Apply to remoteRoot too?
183184
pathMappings = pathMappings.map(({ localRoot: windowsLocalRoot, remoteRoot }) => {
184185
let localRoot = windowsLocalRoot;

extensions/positron-python/src/client/debugger/extension/configuration/resolvers/launch.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import { inject, injectable, named } from 'inversify';
77
import { CancellationToken, Uri, WorkspaceFolder } from 'vscode';
88
import { InvalidPythonPathInDebuggerServiceId } from '../../../../application/diagnostics/checks/invalidPythonPathInDebugger';
99
import { IDiagnosticsService, IInvalidPythonPathInDebuggerService } from '../../../../application/diagnostics/types';
10-
import { IDocumentManager, IWorkspaceService } from '../../../../common/application/types';
11-
import { IPlatformService } from '../../../../common/platform/types';
1210
import { IConfigurationService } from '../../../../common/types';
11+
import { getOSType, OSType } from '../../../../common/utils/platform';
1312
import { IInterpreterService } from '../../../../interpreter/contracts';
1413
import { DebuggerTypeName } from '../../../constants';
1514
import { DebugOptions, DebugPurpose, LaunchRequestArguments } from '../../../types';
@@ -19,17 +18,14 @@ import { IDebugEnvironmentVariablesService } from './helper';
1918
@injectable()
2019
export class LaunchConfigurationResolver extends BaseConfigurationResolver<LaunchRequestArguments> {
2120
constructor(
22-
@inject(IWorkspaceService) workspaceService: IWorkspaceService,
23-
@inject(IDocumentManager) documentManager: IDocumentManager,
2421
@inject(IDiagnosticsService)
2522
@named(InvalidPythonPathInDebuggerServiceId)
2623
private readonly invalidPythonPathInDebuggerService: IInvalidPythonPathInDebuggerService,
27-
@inject(IPlatformService) platformService: IPlatformService,
2824
@inject(IConfigurationService) configurationService: IConfigurationService,
2925
@inject(IDebugEnvironmentVariablesService) private readonly debugEnvHelper: IDebugEnvironmentVariablesService,
3026
@inject(IInterpreterService) interpreterService: IInterpreterService,
3127
) {
32-
super(workspaceService, documentManager, platformService, configurationService, interpreterService);
28+
super(configurationService, interpreterService);
3329
}
3430

3531
public async resolveDebugConfiguration(
@@ -153,7 +149,7 @@ export class LaunchConfigurationResolver extends BaseConfigurationResolver<Launc
153149
if (debugConfiguration.subProcess === true) {
154150
this.debugOption(debugOptions, DebugOptions.SubProcess);
155151
}
156-
if (this.platformService.isWindows) {
152+
if (getOSType() == OSType.Windows) {
157153
this.debugOption(debugOptions, DebugOptions.FixFilePathCase);
158154
}
159155
const isFastAPI = this.isDebuggingFastAPI(debugConfiguration);

extensions/positron-python/src/client/debugger/extension/configuration/utils/common.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
'use strict';
88

9-
import { WorkspaceFolder } from 'vscode';
9+
import { WorkspaceFolder, window, TextEditor } from 'vscode';
1010
import { getWorkspaceFolder } from './workspaceFolder';
1111

1212
/**
@@ -21,20 +21,28 @@ function isString(str: any): str is string {
2121
}
2222

2323
export function resolveVariables(
24-
value: string,
24+
value: string | undefined,
2525
rootFolder: string | undefined,
2626
folder: WorkspaceFolder | undefined,
27-
): string {
28-
const workspace = folder ? getWorkspaceFolder(folder.uri) : undefined;
29-
const variablesObject: { [key: string]: any } = {};
30-
variablesObject.workspaceFolder = workspace ? workspace.uri.fsPath : rootFolder;
27+
): string | undefined {
28+
if (value) {
29+
const workspace = folder ? getWorkspaceFolder(folder.uri) : undefined;
30+
const variablesObject: { [key: string]: any } = {};
31+
variablesObject.workspaceFolder = workspace ? workspace.uri.fsPath : rootFolder;
3132

32-
const regexp = /\$\{(.*?)\}/g;
33-
return value.replace(regexp, (match: string, name: string) => {
34-
const newValue = variablesObject[name];
35-
if (isString(newValue)) {
36-
return newValue;
37-
}
38-
return match && (match.indexOf('env.') > 0 || match.indexOf('env:') > 0) ? '' : match;
39-
});
33+
const regexp = /\$\{(.*?)\}/g;
34+
return value.replace(regexp, (match: string, name: string) => {
35+
const newValue = variablesObject[name];
36+
if (isString(newValue)) {
37+
return newValue;
38+
}
39+
return match && (match.indexOf('env.') > 0 || match.indexOf('env:') > 0) ? '' : match;
40+
});
41+
}
42+
return value;
43+
}
44+
45+
export function getActiveTextEditor(): TextEditor | undefined {
46+
const { activeTextEditor } = window;
47+
return activeTextEditor;
4048
}

extensions/positron-python/src/client/debugger/extension/configuration/utils/workspaceFolder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ import * as vscode from 'vscode';
1111
export function getWorkspaceFolder(uri: vscode.Uri): vscode.WorkspaceFolder | undefined {
1212
return vscode.workspace.getWorkspaceFolder(uri);
1313
}
14+
15+
export function getWorkspaceFolders(): readonly vscode.WorkspaceFolder[] | undefined {
16+
return vscode.workspace.workspaceFolders;
17+
}

0 commit comments

Comments
 (0)