Skip to content

Commit 43fde7c

Browse files
authored
Lowercase drive letter for non-empty path mappings (#6228)
* Lowercase drive letter for non-empty path mappings * Simplify path expansion loop * Update unit test * Fix path * Hardcode lowercase strings
1 parent cdbf229 commit 43fde7c

2 files changed

Lines changed: 47 additions & 13 deletions

File tree

src/client/debugger/extension/configuration/resolvers/attach.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { CancellationToken, Uri, WorkspaceFolder } from 'vscode';
88
import { IDocumentManager, IWorkspaceService } from '../../../../common/application/types';
99
import { IPlatformService } from '../../../../common/platform/types';
1010
import { IConfigurationService } from '../../../../common/types';
11+
import { SystemVariables } from '../../../../common/variables/systemVariables';
1112
import { AttachRequestArguments, DebugOptions } from '../../../types';
1213
import { BaseConfigurationResolver } from './base';
1314

@@ -94,18 +95,33 @@ export class AttachConfigurationResolver extends BaseConfigurationResolver<Attac
9495
}
9596
// If attaching to local host, then always map local root and remote roots.
9697
if (workspaceFolder && debugConfiguration.host &&
97-
debugConfiguration.pathMappings!.length === 0 &&
9898
['LOCALHOST', '127.0.0.1', '::1'].indexOf(debugConfiguration.host.toUpperCase()) >= 0) {
99-
// If on Windows, lowercase the drive letter for path mappings.
100-
let localRoot = workspaceFolder.fsPath;
101-
if (this.platformService.isWindows && localRoot.match(/^[A-Z]:/)) {
102-
localRoot = `${localRoot[0].toLowerCase()}${localRoot.substr(1)}`;
103-
}
104-
105-
debugConfiguration.pathMappings!.push({
106-
localRoot,
107-
remoteRoot: workspaceFolder.fsPath
108-
});
99+
let configPathMappings;
100+
if (debugConfiguration.pathMappings!.length === 0) {
101+
configPathMappings = [{
102+
localRoot: workspaceFolder.fsPath,
103+
remoteRoot: workspaceFolder.fsPath
104+
}];
105+
} else {
106+
// Expand ${workspaceFolder} variable first if necessary.
107+
const systemVariables = new SystemVariables(workspaceFolder.fsPath);
108+
configPathMappings = debugConfiguration.pathMappings.map(({ localRoot: mappedLocalRoot, remoteRoot }) => ({
109+
localRoot: systemVariables.resolveAny(mappedLocalRoot),
110+
remoteRoot
111+
}));
112+
}
113+
// If on Windows, lowercase the drive letter for path mappings.
114+
let pathMappings = configPathMappings;
115+
if (this.platformService.isWindows) {
116+
pathMappings = configPathMappings.map(({ localRoot: windowsLocalRoot, remoteRoot }) => {
117+
let localRoot = windowsLocalRoot;
118+
if (windowsLocalRoot.match(/^[A-Z]:/)) {
119+
localRoot = `${windowsLocalRoot[0].toLowerCase()}${windowsLocalRoot.substr(1)}`;
120+
}
121+
return { localRoot, remoteRoot };
122+
});
123+
}
124+
debugConfiguration.pathMappings = pathMappings;
109125
}
110126
this.sendTelemetry('attach', debugConfiguration);
111127
}

src/test/debugger/extension/configuration/resolvers/attach.unit.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ getNamesAndValues(OSType).forEach(os => {
168168
expect(pathMappings![0].localRoot).to.be.equal(workspaceFolder.uri.fsPath);
169169
expect(pathMappings![0].remoteRoot).to.be.equal(workspaceFolder.uri.fsPath);
170170
});
171-
172171
test(`Ensure drive letter is lower cased for local path mappings on Windows when host is '${host}'`, async function () {
173172
if (os.name !== 'Windows') {
174173
return this.skip();
@@ -183,7 +182,26 @@ getNamesAndValues(OSType).forEach(os => {
183182
const localRoot = `Debug_PythonPath_${new Date().toString()}`;
184183
const debugConfig = await debugProvider.resolveDebugConfiguration!(workspaceFolder, { localRoot, host, request: 'attach' } as any as DebugConfiguration);
185184
const pathMappings = (debugConfig as AttachRequestArguments).pathMappings;
186-
const lowercasedLocalRoot = workspaceFolder.uri.fsPath.charAt(0).toLowerCase() + workspaceFolder.uri.fsPath.substr(1);
185+
const lowercasedLocalRoot = path.join('c:', 'Debug', 'Python_Path');
186+
187+
expect(pathMappings![0].localRoot).to.be.equal(lowercasedLocalRoot);
188+
});
189+
test(`Ensure drive letter is lower cased for local path mappings on Windows when host is '${host}' and with existing path mappings`, async function () {
190+
if (os.name !== 'Windows') {
191+
return this.skip();
192+
}
193+
194+
const activeFile = 'xyz.py';
195+
const workspaceFolder = createMoqWorkspaceFolder(path.join('C:', 'Debug', 'Python_Path'));
196+
setupActiveEditor(activeFile, PYTHON_LANGUAGE);
197+
const defaultWorkspace = path.join('usr', 'desktop');
198+
setupWorkspaces([defaultWorkspace]);
199+
200+
const localRoot = `Debug_PythonPath_${new Date().toString()}`;
201+
const debugPathMappings = [ { localRoot: path.join('${workspaceFolder}', localRoot), remoteRoot: '/app/' }];
202+
const debugConfig = await debugProvider.resolveDebugConfiguration!(workspaceFolder, { localRoot, pathMappings: debugPathMappings, host, request: 'attach' } as any as DebugConfiguration);
203+
const pathMappings = (debugConfig as AttachRequestArguments).pathMappings;
204+
const lowercasedLocalRoot = path.join('c:', 'Debug', 'Python_Path', localRoot);
187205

188206
expect(pathMappings![0].localRoot).to.be.equal(lowercasedLocalRoot);
189207
});

0 commit comments

Comments
 (0)