Skip to content

Commit

Permalink
Running python code without debugging using the experimental debugger (
Browse files Browse the repository at this point in the history
…#1373)

Fixes #882

Remove python file used to launch the PTVSD debugger
Added ability to run code without debugging using PTVSD
Launching PTVSD using -m (run as a python module)
  • Loading branch information
DonJayamanne authored Apr 17, 2018
1 parent 62d7b38 commit 157f392
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 122 deletions.
96 changes: 0 additions & 96 deletions pythonFiles/experimental/ptvsd_launcher.py

This file was deleted.

16 changes: 12 additions & 4 deletions src/client/debugger/DebugClients/DebugFactory.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { DebugSession } from 'vscode-debugadapter';
import { AttachRequestArguments, LaunchRequestArguments } from '../Common/Contracts';
import { IDebugLauncherScriptProvider } from '../types';
import { DebugClient } from './DebugClient';
import { DebuggerLauncherScriptProvider, DebuggerV2LauncherScriptProvider, NoDebugLauncherScriptProvider } from './launcherProvider';
import { DebuggerLauncherScriptProvider, NoDebugLauncherScriptProvider } from './launcherProvider';
import { LocalDebugClient } from './LocalDebugClient';
import { LocalDebugClientV2 } from './localDebugClientV2';
import { NonDebugClient } from './NonDebugClient';
import { NonDebugClientV2 } from './nonDebugClientV2';
import { RemoteDebugClient } from './RemoteDebugClient';

export function CreateLaunchDebugClient(launchRequestOptions: LaunchRequestArguments, debugSession: DebugSession, canLaunchTerminal: boolean): DebugClient<{}> {
let launchScriptProvider: IDebugLauncherScriptProvider;
let debugClientClass: typeof LocalDebugClient;
if (launchRequestOptions.noDebug === true) {
return new NonDebugClient(launchRequestOptions, debugSession, canLaunchTerminal, new NoDebugLauncherScriptProvider());
launchScriptProvider = new NoDebugLauncherScriptProvider();
debugClientClass = launchRequestOptions.type === 'pythonExperimental' ? NonDebugClientV2 : NonDebugClient;
} else {
launchScriptProvider = new DebuggerLauncherScriptProvider();
debugClientClass = launchRequestOptions.type === 'pythonExperimental' ? LocalDebugClientV2 : LocalDebugClient;
}
const launchScriptProvider = launchRequestOptions.type === 'pythonExperimental' ? new DebuggerV2LauncherScriptProvider() : new DebuggerLauncherScriptProvider();
return new LocalDebugClient(launchRequestOptions, debugSession, canLaunchTerminal, launchScriptProvider);
return new debugClientClass(launchRequestOptions, debugSession, canLaunchTerminal, launchScriptProvider);
}
export function CreateAttachDebugClient(attachRequestOptions: AttachRequestArguments, debugSession: DebugSession): DebugClient<{}> {
return new RemoteDebugClient(attachRequestOptions, debugSession);
Expand Down
23 changes: 14 additions & 9 deletions src/client/debugger/DebugClients/LocalDebugClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ export class LocalDebugClient extends DebugClient<LaunchRequestArguments> {
if (typeof this.args.pythonPath === 'string' && this.args.pythonPath.trim().length > 0) {
pythonPath = this.args.pythonPath;
}
const ptVSToolsFilePath = this.launcherScriptProvider.getLauncherFilePath();
const launcherArgs = this.buildLauncherArguments();

const args = [ptVSToolsFilePath, processCwd, dbgServer.port.toString(), '34806ad9-833a-4524-8cd6-18ca4aa74f14'].concat(launcherArgs);
const args = this.buildLaunchArguments(processCwd, dbgServer.port);
switch (this.args.console) {
case 'externalTerminal':
case 'integratedTerminal': {
Expand Down Expand Up @@ -161,8 +158,13 @@ export class LocalDebugClient extends DebugClient<LaunchRequestArguments> {
let x = 0;
});
}
private buildLaunchArguments(cwd: string, debugPort: number): string[] {
return [...this.buildDebugArguments(cwd, debugPort), ...this.buildStandardArguments()];
}

// tslint:disable-next-line:member-ordering
protected buildLauncherArguments(): string[] {
protected buildDebugArguments(cwd: string, debugPort: number): string[] {
const ptVSToolsFilePath = this.launcherScriptProvider.getLauncherFilePath();
const vsDebugOptions: string[] = [DebugOptions.RedirectOutput];
if (Array.isArray(this.args.debugOptions)) {
this.args.debugOptions.filter(opt => VALID_DEBUG_OPTIONS.indexOf(opt) >= 0)
Expand All @@ -173,15 +175,18 @@ export class LocalDebugClient extends DebugClient<LaunchRequestArguments> {
if (djangoIndex >= 0) {
vsDebugOptions[djangoIndex] = 'DjangoDebugging';
}
return [ptVSToolsFilePath, cwd, debugPort.toString(), '34806ad9-833a-4524-8cd6-18ca4aa74f14', vsDebugOptions.join(',')];
}
// tslint:disable-next-line:member-ordering
protected buildStandardArguments() {
const programArgs = Array.isArray(this.args.args) && this.args.args.length > 0 ? this.args.args : [];
if (typeof this.args.module === 'string' && this.args.module.length > 0) {
return [vsDebugOptions.join(','), '-m', this.args.module].concat(programArgs);
return ['-m', this.args.module, ...programArgs];
}
const args = [vsDebugOptions.join(',')];
if (this.args.program && this.args.program.length > 0) {
args.push(this.args.program);
return [this.args.program, ...programArgs];
}
return args.concat(programArgs);
return programArgs;
}
private launchExternalTerminal(sudo: boolean, cwd: string, pythonPath: string, args: string[], env: {}) {
return new Promise((resolve, reject) => {
Expand Down
10 changes: 4 additions & 6 deletions src/client/debugger/DebugClients/launcherProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

// tslint:disable:max-classes-per-file

import * as path from 'path';
import { IDebugLauncherScriptProvider } from '../types';

Expand All @@ -15,9 +19,3 @@ export class DebuggerLauncherScriptProvider implements IDebugLauncherScriptProvi
return path.join(path.dirname(__dirname), '..', '..', '..', 'pythonFiles', 'PythonTools', 'visualstudio_py_launcher.py');
}
}

export class DebuggerV2LauncherScriptProvider implements IDebugLauncherScriptProvider {
public getLauncherFilePath(): string {
return path.join(path.dirname(__dirname), '..', '..', '..', 'pythonFiles', 'experimental', 'ptvsd_launcher.py');
}
}
29 changes: 29 additions & 0 deletions src/client/debugger/DebugClients/localDebugClientV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { DebugSession } from 'vscode-debugadapter';
import { LaunchRequestArguments } from '../Common/Contracts';
import { IDebugLauncherScriptProvider } from '../types';
import { LocalDebugClient } from './LocalDebugClient';

export class LocalDebugClientV2 extends LocalDebugClient {
constructor(args: LaunchRequestArguments, debugSession: DebugSession, canLaunchTerminal: boolean, launcherScriptProvider: IDebugLauncherScriptProvider) {
super(args, debugSession, canLaunchTerminal, launcherScriptProvider);
}
protected buildDebugArguments(cwd: string, debugPort: number): string[] {
const noDebugArg = this.args.noDebug ? ['--nodebug'] : [];
return ['-m', 'ptvsd', ...noDebugArg, '--host', 'localhost', '--port', debugPort.toString()];
}
protected buildStandardArguments() {
const programArgs = Array.isArray(this.args.args) && this.args.args.length > 0 ? this.args.args : [];
if (typeof this.args.module === 'string' && this.args.module.length > 0) {
return ['-m', this.args.module, ...programArgs];
}
if (this.args.program && this.args.program.length > 0) {
return ['--file', this.args.program, ...programArgs];
}
return programArgs;
}
}
35 changes: 35 additions & 0 deletions src/client/debugger/DebugClients/nonDebugClientV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { ChildProcess } from 'child_process';
import { DebugSession } from 'vscode-debugadapter';
import { LaunchRequestArguments } from '../Common/Contracts';
import { IDebugLauncherScriptProvider } from '../types';
import { DebugType } from './DebugClient';
import { LocalDebugClientV2 } from './localDebugClientV2';

export class NonDebugClientV2 extends LocalDebugClientV2 {
constructor(args: LaunchRequestArguments, debugSession: DebugSession, canLaunchTerminal: boolean, launcherScriptProvider: IDebugLauncherScriptProvider) {
super(args, debugSession, canLaunchTerminal, launcherScriptProvider);
}

public get DebugType(): DebugType {
return DebugType.RunLocal;
}

public Stop() {
super.Stop();
if (this.pyProc) {
try {
this.pyProc!.kill();
// tslint:disable-next-line:no-empty
} catch { }
this.pyProc = undefined;
}
}
protected handleProcessOutput(proc: ChildProcess, _failedToLaunch: (error: Error | string | Buffer) => void) {
// Do nothing
}
}
5 changes: 5 additions & 0 deletions src/client/debugger/mainV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { DebugProtocol } from 'vscode-debugprotocol';
import '../../client/common/extensions';
import { noop, sleep } from '../common/core.utils';
import { createDeferred, Deferred, isNotInstalledError } from '../common/helpers';
import { IFileSystem } from '../common/platform/types';
import { ICurrentProcess } from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { AttachRequestArguments, LaunchRequestArguments } from './Common/Contracts';
Expand Down Expand Up @@ -98,6 +99,10 @@ export class PythonDebugger extends DebugSession {

}
protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
const fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
if ((typeof args.module !== 'string' || args.module.length === 0) && args.program && !fs.fileExistsSync(args.program)) {
return this.sendErrorResponse(response, { format: `File does not exist. "${args.program}"`, id: 1 }, undefined, undefined, ErrorDestination.User);
}
this.launchPTVSD(args)
.then(() => this.waitForPTVSDToConnect(args))
.then(() => this.emit('debugger_launched'))
Expand Down
8 changes: 1 addition & 7 deletions src/test/debugger/launcherScriptProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { expect } from 'chai';
import * as fs from 'fs';
import * as path from 'path';
import { DebuggerLauncherScriptProvider, DebuggerV2LauncherScriptProvider, NoDebugLauncherScriptProvider } from '../../client/debugger/DebugClients/launcherProvider';
import { DebuggerLauncherScriptProvider, NoDebugLauncherScriptProvider } from '../../client/debugger/DebugClients/launcherProvider';

suite('Debugger - Launcher Script Provider', () => {
test('Ensure stable debugger gets the old launcher from PythonTools directory', () => {
Expand All @@ -19,10 +19,4 @@ suite('Debugger - Launcher Script Provider', () => {
expect(launcherPath).to.be.equal(expectedPath);
expect(fs.existsSync(launcherPath)).to.be.equal(true, 'file does not exist');
});
test('Ensure experimental debugger gets the new launcher from experimentals directory', () => {
const launcherPath = new DebuggerV2LauncherScriptProvider().getLauncherFilePath();
const expectedPath = path.join(path.dirname(__dirname), '..', '..', 'pythonFiles', 'experimental', 'ptvsd_launcher.py');
expect(launcherPath).to.be.equal(expectedPath);
expect(fs.existsSync(launcherPath)).to.be.equal(true, 'file does not exist');
});
});
Loading

0 comments on commit 157f392

Please sign in to comment.