Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python config with args #56

Merged
merged 8 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,19 @@
"items": {
"type": "string"
},
"type": [
"array",
"string"
"anyOf": [
{
"default": "${command:pickArgs}",
"enum": [
"${command:pickArgs}"
]
},
{
"type": [
"array",
"string"
]
}
]
},
"autoReload": {
Expand Down Expand Up @@ -415,7 +425,8 @@
],
"type": "debugpy",
"variables": {
"pickProcess": "debugpy.pickLocalProcess"
"pickProcess": "debugpy.pickLocalProcess",
"pickArgs": "debugpy.pickArgs"
},
"when": "!virtualWorkspace && shellExecutionSupported"
}
Expand Down
1 change: 1 addition & 0 deletions src/extension/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export namespace Commands {
export const Debug_In_Terminal = 'debugpy.debugInTerminal';
export const TriggerEnvironmentSelection = 'debugpy.triggerEnvSelection';
export const PickLocalProcess = 'debugpy.pickLocalProcess';
export const PickArguments = 'debugpy.pickArgs';
export const ViewOutput = 'debugpy.viewOutput';
export const ClearStorage = 'debugpy.clearCacheAndReload';
export const Enable_SourceMap_Support = 'debugpy.enableSourceMapSupport';
Expand Down
14 changes: 14 additions & 0 deletions src/extension/common/utils/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export namespace DebugConfigStrings {
description: l10n.t('Debug the currently active Python file'),
};
}
export namespace fileWithArgs {
export const snippet = {
name: l10n.t('Debugpy: Current File with Arguments'),
};
export const selectConfiguration = {
label: l10n.t('Python File with Arguments'),
description: l10n.t('Debug the currently active Python file with arguments'),
};
}
export namespace module {
export const snippet = {
name: l10n.t('Debugpy: Module'),
Expand Down Expand Up @@ -193,3 +202,8 @@ export namespace OutdatedDebugger {
export namespace Logging {
export const currentWorkingDirectory = l10n.t('cwd:');
}

export namespace pickArgsInput {
export const title = l10n.t('Command Line Arguments');
export const prompt = l10n.t('Enter the command line arguments you want to pass to the program');
}
6 changes: 0 additions & 6 deletions src/extension/debugger/attachQuickPick/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ export interface IAttachProcessProvider {
getAttachItems(): Promise<IAttachItem[]>;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
// export const IAttachProcessProviderFactory = Symbol('IAttachProcessProviderFactory');
// export interface IAttachProcessProviderFactory {
// registerCommands(): void;
// }

export interface IAttachPicker {
showQuickPick(): Promise<string>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { buildPidAttachConfiguration } from './providers/pidAttach';
import { buildPyramidLaunchConfiguration } from './providers/pyramidLaunch';
import { buildRemoteAttachConfiguration } from './providers/remoteAttach';
import { IDebugConfigurationResolver } from './types';
import { buildFileWithArgsLaunchDebugConfiguration } from './providers/fileLaunchWithArgs';

@injectable()
export class PythonDebugConfigurationService implements IDebugConfigurationService {
Expand Down Expand Up @@ -116,6 +117,11 @@ export class PythonDebugConfigurationService implements IDebugConfigurationServi
type: DebugConfigurationType.launchFile,
description: DebugConfigStrings.file.selectConfiguration.description,
},
{
label: DebugConfigStrings.fileWithArgs.selectConfiguration.label,
type: DebugConfigurationType.launchFileWithArgs,
description: DebugConfigStrings.fileWithArgs.selectConfiguration.description,
},
{
label: DebugConfigStrings.module.selectConfiguration.label,
type: DebugConfigurationType.launchModule,
Expand Down Expand Up @@ -162,6 +168,7 @@ export class PythonDebugConfigurationService implements IDebugConfigurationServi
debugConfigurations.set(DebugConfigurationType.launchDjango, buildDjangoLaunchDebugConfiguration);
debugConfigurations.set(DebugConfigurationType.launchFastAPI, buildFastAPILaunchDebugConfiguration);
debugConfigurations.set(DebugConfigurationType.launchFile, buildFileLaunchDebugConfiguration);
debugConfigurations.set(DebugConfigurationType.launchFileWithArgs, buildFileWithArgsLaunchDebugConfiguration);
debugConfigurations.set(DebugConfigurationType.launchFlask, buildFlaskLaunchDebugConfiguration);
debugConfigurations.set(DebugConfigurationType.launchModule, buildModuleLaunchConfiguration);
debugConfigurations.set(DebugConfigurationType.pidAttach, buildPidAttachConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function buildFastAPILaunchDebugConfiguration(
justMyCode: true,
};

if (!application && config.args) {
if (!application) {
const selectedPath = await input.showInputBox({
title: DebugConfigStrings.fastapi.enterAppPathOrNamePath.title,
value: 'main.py',
Expand All @@ -44,7 +44,7 @@ export async function buildFastAPILaunchDebugConfiguration(
});
if (selectedPath) {
manuallyEnteredAValue = true;
config.args[0] = `${path.basename(selectedPath, '.py').replace('/', '.')}:app`;
config.args = [`${path.basename(selectedPath, '.py').replace('/', '.')}:app`, '--reload'];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { DebugConfigStrings } from '../../../common/utils/localize';
import { MultiStepInput } from '../../../common/multiStepInput';
import { sendTelemetryEvent } from '../../../telemetry';
import { EventName } from '../../../telemetry/constants';
import { DebuggerTypeName } from '../../../constants';
import { LaunchRequestArguments } from '../../../types';
import { DebugConfigurationState, DebugConfigurationType } from '../../types';

export async function buildFileWithArgsLaunchDebugConfiguration(
_input: MultiStepInput<DebugConfigurationState>,
state: DebugConfigurationState,
): Promise<void> {
const config: Partial<LaunchRequestArguments> = {
name: DebugConfigStrings.fileWithArgs.snippet.name,
type: DebuggerTypeName,
request: 'launch',
program: '${file}',
console: 'integratedTerminal',
args: '${command:pickArgs}',
justMyCode: true,
};
sendTelemetryEvent(EventName.DEBUGGER_CONFIGURATION_PROMPTS, undefined, {
configurationType: DebugConfigurationType.launchFileWithArgs,
});
Object.assign(state.config, config);
}
1 change: 1 addition & 0 deletions src/extension/debugger/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type DebugConfigurationState = {

export enum DebugConfigurationType {
launchFile = 'launchFile',
launchFileWithArgs = 'launchFileWithArgs',
remoteAttach = 'remoteAttach',
launchDjango = 'launchDjango',
launchFastAPI = 'launchFastAPI',
Expand Down
8 changes: 7 additions & 1 deletion src/extension/extensionInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

'use strict';

import { debug, DebugConfigurationProviderTriggerKind, languages, Uri } from 'vscode';
import { debug, DebugConfigurationProviderTriggerKind, languages, Uri, window } from 'vscode';
import { executeCommand, getConfiguration, registerCommand, startDebugging } from './common/vscodeapi';
import { DebuggerTypeName } from './constants';
import { DynamicPythonDebugConfigurationService } from './debugger/configuration/dynamicdebugConfigurationService';
Expand Down Expand Up @@ -31,6 +31,7 @@ import { JsonLanguages, LaunchJsonCompletionProvider } from './debugger/configur
import { InterpreterPathCommand } from './debugger/configuration/launch.json/interpreterPathCommand';
import { LaunchJsonUpdaterServiceHelper } from './debugger/configuration/launch.json/updaterServiceHelper';
import { ignoreErrors } from './common/promiseUtils';
import { pickArgsInput } from './common/utils/localize';

export async function registerDebugger(context: IExtensionContext): Promise<void> {
const childProcessAttachService = new ChildProcessAttachService();
Expand Down Expand Up @@ -80,6 +81,11 @@ export async function registerDebugger(context: IExtensionContext): Promise<void
const attachProcessProvider = new AttachProcessProvider();
const attachPicker = new AttachPicker(attachProcessProvider);
context.subscriptions.push(registerCommand(Commands.PickLocalProcess, () => attachPicker.showQuickPick()));
context.subscriptions.push(
registerCommand(Commands.PickArguments, () => {
return window.showInputBox({ title: pickArgsInput.title, prompt: pickArgsInput.prompt });
}),
);

const debugAdapterDescriptorFactory = new DebugAdapterDescriptorFactory(persistantState);
const debugSessionLoggingFactory = new DebugSessionLoggingFactory();
Expand Down
2 changes: 1 addition & 1 deletion src/extension/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ interface IKnownLaunchRequestArguments extends ICommonDebugArguments {
python?: string;
// Automatically stop target after launch. If not specified, target does not stop.
stopOnEntry?: boolean;
args?: string[];
args?: string[] | String;
cwd?: string;
debugOptions?: DebugOptions[];
env?: Record<string, string | undefined>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { expect } from 'chai';
import * as path from 'path';
import { Uri } from 'vscode';
import { MultiStepInput } from '../../../../extension/common/multiStepInput';
import { DebugConfigStrings } from '../../../../extension/common/utils/localize';
import { DebuggerTypeName } from '../../../../extension/constants';
import { buildFileWithArgsLaunchDebugConfiguration } from '../../../../extension/debugger/configuration/providers/fileLaunchWithArgs';
import { DebugConfigurationState } from '../../../../extension/debugger/types';

suite('Debugging - Configuration Provider File with Arguments', () => {
test('Launch JSON with default config', async () => {
const folder = { uri: Uri.parse(path.join('one', 'two')), name: '1', index: 0 };
const state = { config: {}, folder };

await buildFileWithArgsLaunchDebugConfiguration(undefined as unknown as MultiStepInput<DebugConfigurationState>, state);

const config = {
name: DebugConfigStrings.fileWithArgs.snippet.name,
type: DebuggerTypeName,
request: 'launch',
program: '${file}',
console: 'integratedTerminal',
args: '${command:pickArgs}',
justMyCode: true,
};

expect(state.config).to.be.deep.equal(config);
});
});
3 changes: 2 additions & 1 deletion src/test/unittest/extensionInit.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ suite('Debugging - register Debugging', () => {

sinon.assert.calledWithExactly(registerCommandStub, Commands.Debug_In_Terminal, sinon.match.any);
sinon.assert.calledWithExactly(registerCommandStub, Commands.PickLocalProcess, sinon.match.any);
sinon.assert.calledWithExactly(registerCommandStub, Commands.PickArguments, sinon.match.any);
sinon.assert.calledWithExactly(
registerCommandStub,
Commands.SelectDebugConfig,
Expand All @@ -69,7 +70,7 @@ suite('Debugging - register Debugging', () => {
);
sinon.assert.calledWithExactly(registerCommandStub, Commands.GetSelectedInterpreterPath, sinon.match.any);
sinon.assert.calledWithExactly(registerCommandStub, Commands.ClearStorage, sinon.match.any);
expect(registerCommandStub.callCount).to.be.equal(5);
expect(registerCommandStub.callCount).to.be.equal(6);
});
test('Activation will register the Debug adapter factories', async () => {
registerDebugger(context.object);
Expand Down
Loading