Skip to content

Commit 3c66f87

Browse files
bjoluczobo
authored andcommitted
fix: implement longest prefix matching for pathMappings (closes #331) (#363)
* fix: implement longest prefix matching for pathMappings (closes #331) * test: add test cases for server path longest prefix matching Based on the test cases over at robberphex/vscode-php-debug@729be1d. Thanks @robberphex!
1 parent f64d05b commit 3c66f87

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/paths.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ export function convertDebuggerPathToClient(
4646
// normalize slashes for windows-to-unix
4747
const serverRelative = (serverIsWindows ? path.win32 : path.posix).relative(mappedServerPath, serverPath)
4848
if (serverRelative.indexOf('..') !== 0) {
49-
serverSourceRoot = mappedServerPath
50-
localSourceRoot = mappedLocalSource
51-
break
49+
// If a matching mapping has previously been found, only update
50+
// it if the current server path is longer than the previous one
51+
// (longest prefix matching)
52+
if (!serverSourceRoot || mappedServerPath.length > serverSourceRoot.length) {
53+
serverSourceRoot = mappedServerPath
54+
localSourceRoot = mappedLocalSource
55+
}
5256
}
5357
}
5458
}
@@ -82,9 +86,13 @@ export function convertClientPathToDebugger(localPath: string, pathMapping?: { [
8286
const mappedLocalSource = pathMapping[mappedServerPath]
8387
const localRelative = path.relative(mappedLocalSource, localPath)
8488
if (localRelative.indexOf('..') !== 0) {
85-
serverSourceRoot = mappedServerPath
86-
localSourceRoot = mappedLocalSource
87-
break
89+
// If a matching mapping has previously been found, only update
90+
// it if the current local path is longer than the previous one
91+
// (longest prefix matching)
92+
if (!localSourceRoot || mappedLocalSource.length > localSourceRoot.length) {
93+
serverSourceRoot = mappedServerPath
94+
localSourceRoot = mappedLocalSource
95+
}
8896
}
8997
}
9098
}

src/test/paths.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ describe('paths', () => {
5555
}),
5656
'file:///app/source.php'
5757
)
58+
// longest prefix matching for server paths
59+
assert.strictEqual(
60+
convertClientPathToDebugger('/home/felix/mysource/subdir/source.php', {
61+
'/var/www': '/home/felix/mysite',
62+
'/app/subdir1': '/home/felix/mysource/subdir',
63+
'/app': '/home/felix/mysource',
64+
}),
65+
'file:///app/subdir1/source.php'
66+
)
5867
})
5968
// unix to windows
6069
it('should convert a unix path to a windows URI', () => {
@@ -165,6 +174,15 @@ describe('paths', () => {
165174
}),
166175
'/home/felix/mysource/source.php'
167176
)
177+
// longest prefix matching for local paths
178+
assert.strictEqual(
179+
convertDebuggerPathToClient('file:///app/subdir/source.php', {
180+
'/var/www': '/home/felix/mysite',
181+
'/app/subdir': '/home/felix/mysource/subdir1',
182+
'/app': '/home/felix/mysource',
183+
}),
184+
'/home/felix/mysource/subdir1/source.php'
185+
)
168186
})
169187
// unix to windows
170188
;(process.platform === 'win32' ? it : it.skip)('should map unix uris to windows paths', () => {

0 commit comments

Comments
 (0)