Skip to content

Commit 33505d2

Browse files
authored
Add path mappings for remote debugging when attaching to the localhost (microsoft#1830)
1 parent 72789f6 commit 33505d2

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

news/2 Fixes/1829.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Automatically add path mappings for remote debugging when attaching to the localhost.

src/client/debugger/configProviders/pythonV2Provider.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class PythonV2DebugConfigurationProvider extends BaseConfigurationProvide
1616
constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) {
1717
super('pythonExperimental', serviceContainer);
1818
}
19-
protected async provideLaunchDefaults(workspaceFolder: Uri, debugConfiguration: PythonLaunchDebugConfiguration<LaunchRequestArguments>): Promise<void> {
19+
protected async provideLaunchDefaults(workspaceFolder: Uri | undefined, debugConfiguration: PythonLaunchDebugConfiguration<LaunchRequestArguments>): Promise<void> {
2020
await super.provideLaunchDefaults(workspaceFolder, debugConfiguration);
2121
const debugOptions = debugConfiguration.debugOptions!;
2222
if (debugConfiguration.debugStdLib) {
@@ -48,7 +48,8 @@ export class PythonV2DebugConfigurationProvider extends BaseConfigurationProvide
4848
debugConfiguration.program = (await utils.getPyramidStartupScriptFilePath(workspaceFolder))!;
4949
}
5050
}
51-
protected async provideAttachDefaults(workspaceFolder: Uri, debugConfiguration: PythonAttachDebugConfiguration<AttachRequestArguments>): Promise<void> {
51+
// tslint:disable-next-line:cyclomatic-complexity
52+
protected async provideAttachDefaults(workspaceFolder: Uri | undefined, debugConfiguration: PythonAttachDebugConfiguration<AttachRequestArguments>): Promise<void> {
5253
await super.provideAttachDefaults(workspaceFolder, debugConfiguration);
5354
const debugOptions = debugConfiguration.debugOptions!;
5455
if (debugConfiguration.debugStdLib) {
@@ -82,12 +83,22 @@ export class PythonV2DebugConfigurationProvider extends BaseConfigurationProvide
8283
if (!debugConfiguration.pathMappings) {
8384
debugConfiguration.pathMappings = [];
8485
}
86+
// This is for backwards compatibility.
8587
if (debugConfiguration.localRoot && debugConfiguration.remoteRoot) {
8688
debugConfiguration.pathMappings!.push({
8789
localRoot: debugConfiguration.localRoot,
8890
remoteRoot: debugConfiguration.remoteRoot
8991
});
9092
}
93+
// If attaching to local host, then always map local root and remote roots.
94+
if (workspaceFolder && debugConfiguration.host &&
95+
debugConfiguration.pathMappings!.length === 0 &&
96+
['LOCALHOST', '127.0.0.1', '::1'].indexOf(debugConfiguration.host.toUpperCase()) >= 0) {
97+
debugConfiguration.pathMappings!.push({
98+
localRoot: workspaceFolder.fsPath,
99+
remoteRoot: workspaceFolder.fsPath
100+
});
101+
}
91102
}
92103
private debugOption(debugOptions: DebugOptions[], debugOption: DebugOptions) {
93104
if (debugOptions.indexOf(debugOption) >= 0) {

src/test/debugger/configProvider/provider.attach.test.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { PYTHON_LANGUAGE } from '../../../client/common/constants';
1414
import { EnumEx } from '../../../client/common/enumUtils';
1515
import { IFileSystem, IPlatformService } from '../../../client/common/platform/types';
1616
import { PythonDebugConfigurationProvider, PythonV2DebugConfigurationProvider } from '../../../client/debugger';
17-
import { DebugOptions } from '../../../client/debugger/Common/Contracts';
17+
import { AttachRequestArguments, DebugOptions } from '../../../client/debugger/Common/Contracts';
1818
import { IServiceContainer } from '../../../client/ioc/types';
1919

2020
enum OS {
@@ -164,9 +164,44 @@ enum OS {
164164
const debugConfig = await debugProvider.resolveDebugConfiguration!(workspaceFolder, { localRoot, request: 'attach' } as any as DebugConfiguration);
165165

166166
expect(debugConfig).to.have.property('localRoot', localRoot);
167-
if (provider.debugType === 'pythonExperimental') {
168-
expect(debugConfig!.pathMappings).to.be.lengthOf(0);
169-
}
167+
});
168+
['localhost', '127.0.0.1', '::1'].forEach(host => {
169+
test(`Ensure path mappings are automatically added when host is '${host}'`, async () => {
170+
const activeFile = 'xyz.py';
171+
const workspaceFolder = createMoqWorkspaceFolder(__dirname);
172+
setupActiveEditor(activeFile, PYTHON_LANGUAGE);
173+
const defaultWorkspace = path.join('usr', 'desktop');
174+
setupWorkspaces([defaultWorkspace]);
175+
176+
const localRoot = `Debug_PythonPath_${new Date().toString()}`;
177+
const debugConfig = await debugProvider.resolveDebugConfiguration!(workspaceFolder, { localRoot, host, request: 'attach' } as any as DebugConfiguration);
178+
179+
expect(debugConfig).to.have.property('localRoot', localRoot);
180+
if (provider.debugType === 'pythonExperimental') {
181+
const pathMappings = (debugConfig as AttachRequestArguments).pathMappings;
182+
expect(pathMappings).to.be.lengthOf(1);
183+
expect(pathMappings![0].localRoot).to.be.equal(workspaceFolder.uri.fsPath);
184+
expect(pathMappings![0].remoteRoot).to.be.equal(workspaceFolder.uri.fsPath);
185+
}
186+
});
187+
});
188+
['192.168.1.123', 'don.debugger.com'].forEach(host => {
189+
test(`Ensure path mappings are not automatically added when host is '${host}'`, async () => {
190+
const activeFile = 'xyz.py';
191+
const workspaceFolder = createMoqWorkspaceFolder(__dirname);
192+
setupActiveEditor(activeFile, PYTHON_LANGUAGE);
193+
const defaultWorkspace = path.join('usr', 'desktop');
194+
setupWorkspaces([defaultWorkspace]);
195+
196+
const localRoot = `Debug_PythonPath_${new Date().toString()}`;
197+
const debugConfig = await debugProvider.resolveDebugConfiguration!(workspaceFolder, { localRoot, host, request: 'attach' } as any as DebugConfiguration);
198+
199+
expect(debugConfig).to.have.property('localRoot', localRoot);
200+
if (provider.debugType === 'pythonExperimental') {
201+
const pathMappings = (debugConfig as AttachRequestArguments).pathMappings;
202+
expect(pathMappings).to.be.lengthOf(0);
203+
}
204+
});
170205
});
171206
test('Ensure \'localRoot\' and \'remoteRoot\' is used', async function () {
172207
if (provider.debugType !== 'pythonExperimental') {

0 commit comments

Comments
 (0)