Skip to content

Commit

Permalink
19767 remove di in resolvers (microsoft/vscode-python#20048)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulacamargo25 authored and wesm committed Mar 28, 2024
1 parent 182f449 commit 83be83c
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ export async function validateManagePy(
return error;
}
const resolvedPath = resolveVariables(selected, undefined, folder);

if (selected !== defaultValue && !(await fs.pathExists(resolvedPath))) {
return error;
}
if (!resolvedPath.trim().toLowerCase().endsWith('.py')) {
return error;
if (resolvedPath) {
if (selected !== defaultValue && !(await fs.pathExists(resolvedPath))) {
return error;
}
if (!resolvedPath.trim().toLowerCase().endsWith('.py')) {
return error;
}
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ export async function validateIniPath(
return error;
}
const resolvedPath = resolveVariables(selected, undefined, folder);
if (selected !== defaultValue && !fs.pathExists(resolvedPath)) {
return error;
}
if (!resolvedPath.trim().toLowerCase().endsWith('.ini')) {
return error;
if (resolvedPath) {
if (selected !== defaultValue && !fs.pathExists(resolvedPath)) {
return error;
}
if (!resolvedPath.trim().toLowerCase().endsWith('.ini')) {
return error;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,14 @@

'use strict';

import { inject, injectable } from 'inversify';
import { injectable } from 'inversify';
import { CancellationToken, Uri, WorkspaceFolder } from 'vscode';
import { IDocumentManager, IWorkspaceService } from '../../../../common/application/types';
import { IPlatformService } from '../../../../common/platform/types';
import { IConfigurationService } from '../../../../common/types';
import { IInterpreterService } from '../../../../interpreter/contracts';
import { getOSType, OSType } from '../../../../common/utils/platform';
import { AttachRequestArguments, DebugOptions, PathMapping } from '../../../types';
import { BaseConfigurationResolver } from './base';

@injectable()
export class AttachConfigurationResolver extends BaseConfigurationResolver<AttachRequestArguments> {
constructor(
@inject(IWorkspaceService) workspaceService: IWorkspaceService,
@inject(IDocumentManager) documentManager: IDocumentManager,
@inject(IPlatformService) platformService: IPlatformService,
@inject(IConfigurationService) configurationService: IConfigurationService,
@inject(IInterpreterService) interpreterService: IInterpreterService,
) {
super(workspaceService, documentManager, platformService, configurationService, interpreterService);
}

public async resolveDebugConfigurationWithSubstitutedVariables(
folder: WorkspaceFolder | undefined,
debugConfiguration: AttachRequestArguments,
Expand Down Expand Up @@ -87,10 +74,10 @@ export class AttachConfigurationResolver extends BaseConfigurationResolver<Attac
// We'll need paths to be fixed only in the case where local and remote hosts are the same
// I.e. only if hostName === 'localhost' or '127.0.0.1' or ''
const isLocalHost = this.isLocalHost(debugConfiguration.host);
if (this.platformService.isWindows && isLocalHost) {
if (getOSType() == OSType.Windows && isLocalHost) {
this.debugOption(debugOptions, DebugOptions.FixFilePathCase);
}
if (this.platformService.isWindows) {
if (getOSType() == OSType.Windows) {
this.debugOption(debugOptions, DebugOptions.WindowsClient);
} else {
this.debugOption(debugOptions, DebugOptions.UnixClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@
import { injectable } from 'inversify';
import * as path from 'path';
import { CancellationToken, DebugConfiguration, Uri, WorkspaceFolder } from 'vscode';
import { IDocumentManager, IWorkspaceService } from '../../../../common/application/types';
import { PYTHON_LANGUAGE } from '../../../../common/constants';
import { IPlatformService } from '../../../../common/platform/types';
import { IConfigurationService } from '../../../../common/types';
import { SystemVariables } from '../../../../common/variables/systemVariables';
import { getOSType, OSType } from '../../../../common/utils/platform';
import { IInterpreterService } from '../../../../interpreter/contracts';
import { sendTelemetryEvent } from '../../../../telemetry';
import { EventName } from '../../../../telemetry/constants';
import { DebuggerTelemetry } from '../../../../telemetry/types';
import { AttachRequestArguments, DebugOptions, LaunchRequestArguments, PathMapping } from '../../../types';
import { PythonPathSource } from '../../types';
import { IDebugConfigurationResolver } from '../types';
import { getActiveTextEditor, resolveVariables } from '../utils/common';
import { getWorkspaceFolder as getVSCodeWorkspaceFolder, getWorkspaceFolders } from '../utils/workspaceFolder';

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

constructor(
protected readonly workspaceService: IWorkspaceService,
protected readonly documentManager: IDocumentManager,
protected readonly platformService: IPlatformService,
protected readonly configurationService: IConfigurationService,
protected readonly interpreterService: IInterpreterService,
) {}
Expand Down Expand Up @@ -59,27 +56,26 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
return folder.uri;
}
const program = this.getProgram();
if (
!Array.isArray(this.workspaceService.workspaceFolders) ||
this.workspaceService.workspaceFolders.length === 0
) {
let workspaceFolders = getWorkspaceFolders();

if (!Array.isArray(workspaceFolders) || workspaceFolders.length === 0) {
return program ? Uri.file(path.dirname(program)) : undefined;
}
if (this.workspaceService.workspaceFolders.length === 1) {
return this.workspaceService.workspaceFolders[0].uri;
if (workspaceFolders.length === 1) {
return workspaceFolders[0].uri;
}
if (program) {
const workspaceFolder = this.workspaceService.getWorkspaceFolder(Uri.file(program));
const workspaceFolder = getVSCodeWorkspaceFolder(Uri.file(program));
if (workspaceFolder) {
return workspaceFolder.uri;
}
}
}

protected getProgram(): string | undefined {
const editor = this.documentManager.activeTextEditor;
if (editor && editor.document.languageId === PYTHON_LANGUAGE) {
return editor.document.fileName;
const activeTextEditor = getActiveTextEditor();
if (activeTextEditor && activeTextEditor.document.languageId === PYTHON_LANGUAGE) {
return activeTextEditor.document.fileName;
}
}

Expand All @@ -99,11 +95,11 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
return;
}
if (debugConfiguration.envFile && (workspaceFolder || debugConfiguration.cwd)) {
const systemVariables = new SystemVariables(
undefined,
debugConfiguration.envFile = resolveVariables(
debugConfiguration.envFile,
(workspaceFolder ? workspaceFolder.fsPath : undefined) || debugConfiguration.cwd,
undefined,
);
debugConfiguration.envFile = systemVariables.resolveAny(debugConfiguration.envFile);
}
}

Expand All @@ -114,25 +110,28 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
if (!debugConfiguration) {
return;
}
const systemVariables: SystemVariables = new SystemVariables(
undefined,
workspaceFolder?.fsPath,
this.workspaceService,
);
if (debugConfiguration.pythonPath === '${command:python.interpreterPath}' || !debugConfiguration.pythonPath) {
const interpreterPath =
(await this.interpreterService.getActiveInterpreter(workspaceFolder))?.path ??
this.configurationService.getSettings(workspaceFolder).pythonPath;
debugConfiguration.pythonPath = interpreterPath;
} else {
debugConfiguration.pythonPath = systemVariables.resolveAny(debugConfiguration.pythonPath);
debugConfiguration.pythonPath = resolveVariables(
debugConfiguration.pythonPath ? debugConfiguration.pythonPath : undefined,
workspaceFolder?.fsPath,
undefined,
);
}
if (debugConfiguration.python === '${command:python.interpreterPath}' || !debugConfiguration.python) {
this.pythonPathSource = PythonPathSource.settingsJson;
} else {
this.pythonPathSource = PythonPathSource.launchJson;
}
debugConfiguration.python = systemVariables.resolveAny(debugConfiguration.python);
debugConfiguration.python = resolveVariables(
debugConfiguration.python ? debugConfiguration.python : undefined,
workspaceFolder?.fsPath,
undefined,
);
}

protected debugOption(debugOptions: DebugOptions[], debugOption: DebugOptions) {
Expand Down Expand Up @@ -168,17 +167,19 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
];
} else {
// Expand ${workspaceFolder} variable first if necessary.
const systemVariables = new SystemVariables(undefined, defaultLocalRoot);
pathMappings = pathMappings.map(({ localRoot: mappedLocalRoot, remoteRoot }) => ({
localRoot: systemVariables.resolveAny(mappedLocalRoot),
// TODO: Apply to remoteRoot too?
remoteRoot,
}));
pathMappings = pathMappings.map(({ localRoot: mappedLocalRoot, remoteRoot }) => {
let resolvedLocalRoot = resolveVariables(mappedLocalRoot, defaultLocalRoot, undefined);
return {
localRoot: resolvedLocalRoot ? resolvedLocalRoot : '',
// TODO: Apply to remoteRoot too?
remoteRoot,
};
});
}

// If on Windows, lowercase the drive letter for path mappings.
// TODO: Apply even if no localRoot?
if (this.platformService.isWindows) {
if (getOSType() == OSType.Windows) {
// TODO: Apply to remoteRoot too?
pathMappings = pathMappings.map(({ localRoot: windowsLocalRoot, remoteRoot }) => {
let localRoot = windowsLocalRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import { inject, injectable, named } from 'inversify';
import { CancellationToken, Uri, WorkspaceFolder } from 'vscode';
import { InvalidPythonPathInDebuggerServiceId } from '../../../../application/diagnostics/checks/invalidPythonPathInDebugger';
import { IDiagnosticsService, IInvalidPythonPathInDebuggerService } from '../../../../application/diagnostics/types';
import { IDocumentManager, IWorkspaceService } from '../../../../common/application/types';
import { IPlatformService } from '../../../../common/platform/types';
import { IConfigurationService } from '../../../../common/types';
import { getOSType, OSType } from '../../../../common/utils/platform';
import { IInterpreterService } from '../../../../interpreter/contracts';
import { DebuggerTypeName } from '../../../constants';
import { DebugOptions, DebugPurpose, LaunchRequestArguments } from '../../../types';
Expand All @@ -19,17 +18,14 @@ import { IDebugEnvironmentVariablesService } from './helper';
@injectable()
export class LaunchConfigurationResolver extends BaseConfigurationResolver<LaunchRequestArguments> {
constructor(
@inject(IWorkspaceService) workspaceService: IWorkspaceService,
@inject(IDocumentManager) documentManager: IDocumentManager,
@inject(IDiagnosticsService)
@named(InvalidPythonPathInDebuggerServiceId)
private readonly invalidPythonPathInDebuggerService: IInvalidPythonPathInDebuggerService,
@inject(IPlatformService) platformService: IPlatformService,
@inject(IConfigurationService) configurationService: IConfigurationService,
@inject(IDebugEnvironmentVariablesService) private readonly debugEnvHelper: IDebugEnvironmentVariablesService,
@inject(IInterpreterService) interpreterService: IInterpreterService,
) {
super(workspaceService, documentManager, platformService, configurationService, interpreterService);
super(configurationService, interpreterService);
}

public async resolveDebugConfiguration(
Expand Down Expand Up @@ -153,7 +149,7 @@ export class LaunchConfigurationResolver extends BaseConfigurationResolver<Launc
if (debugConfiguration.subProcess === true) {
this.debugOption(debugOptions, DebugOptions.SubProcess);
}
if (this.platformService.isWindows) {
if (getOSType() == OSType.Windows) {
this.debugOption(debugOptions, DebugOptions.FixFilePathCase);
}
const isFastAPI = this.isDebuggingFastAPI(debugConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

'use strict';

import { WorkspaceFolder } from 'vscode';
import { WorkspaceFolder, window, TextEditor } from 'vscode';
import { getWorkspaceFolder } from './workspaceFolder';

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

export function resolveVariables(
value: string,
value: string | undefined,
rootFolder: string | undefined,
folder: WorkspaceFolder | undefined,
): string {
const workspace = folder ? getWorkspaceFolder(folder.uri) : undefined;
const variablesObject: { [key: string]: any } = {};
variablesObject.workspaceFolder = workspace ? workspace.uri.fsPath : rootFolder;
): string | undefined {
if (value) {
const workspace = folder ? getWorkspaceFolder(folder.uri) : undefined;
const variablesObject: { [key: string]: any } = {};
variablesObject.workspaceFolder = workspace ? workspace.uri.fsPath : rootFolder;

const regexp = /\$\{(.*?)\}/g;
return value.replace(regexp, (match: string, name: string) => {
const newValue = variablesObject[name];
if (isString(newValue)) {
return newValue;
}
return match && (match.indexOf('env.') > 0 || match.indexOf('env:') > 0) ? '' : match;
});
const regexp = /\$\{(.*?)\}/g;
return value.replace(regexp, (match: string, name: string) => {
const newValue = variablesObject[name];
if (isString(newValue)) {
return newValue;
}
return match && (match.indexOf('env.') > 0 || match.indexOf('env:') > 0) ? '' : match;
});
}
return value;
}

export function getActiveTextEditor(): TextEditor | undefined {
const { activeTextEditor } = window;
return activeTextEditor;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ import * as vscode from 'vscode';
export function getWorkspaceFolder(uri: vscode.Uri): vscode.WorkspaceFolder | undefined {
return vscode.workspace.getWorkspaceFolder(uri);
}

export function getWorkspaceFolders(): readonly vscode.WorkspaceFolder[] | undefined {
return vscode.workspace.workspaceFolders;
}
Loading

0 comments on commit 83be83c

Please sign in to comment.