Skip to content

Commit

Permalink
fix: implement longest prefix matching for pathMappings (closes #331) (
Browse files Browse the repository at this point in the history
…#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!
  • Loading branch information
bjoluc authored and zobo committed Feb 6, 2021
1 parent f64d05b commit 3c66f87
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ export function convertDebuggerPathToClient(
// normalize slashes for windows-to-unix
const serverRelative = (serverIsWindows ? path.win32 : path.posix).relative(mappedServerPath, serverPath)
if (serverRelative.indexOf('..') !== 0) {
serverSourceRoot = mappedServerPath
localSourceRoot = mappedLocalSource
break
// If a matching mapping has previously been found, only update
// it if the current server path is longer than the previous one
// (longest prefix matching)
if (!serverSourceRoot || mappedServerPath.length > serverSourceRoot.length) {
serverSourceRoot = mappedServerPath
localSourceRoot = mappedLocalSource
}
}
}
}
Expand Down Expand Up @@ -82,9 +86,13 @@ export function convertClientPathToDebugger(localPath: string, pathMapping?: { [
const mappedLocalSource = pathMapping[mappedServerPath]
const localRelative = path.relative(mappedLocalSource, localPath)
if (localRelative.indexOf('..') !== 0) {
serverSourceRoot = mappedServerPath
localSourceRoot = mappedLocalSource
break
// If a matching mapping has previously been found, only update
// it if the current local path is longer than the previous one
// (longest prefix matching)
if (!localSourceRoot || mappedLocalSource.length > localSourceRoot.length) {
serverSourceRoot = mappedServerPath
localSourceRoot = mappedLocalSource
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ describe('paths', () => {
}),
'file:///app/source.php'
)
// longest prefix matching for server paths
assert.strictEqual(
convertClientPathToDebugger('/home/felix/mysource/subdir/source.php', {
'/var/www': '/home/felix/mysite',
'/app/subdir1': '/home/felix/mysource/subdir',
'/app': '/home/felix/mysource',
}),
'file:///app/subdir1/source.php'
)
})
// unix to windows
it('should convert a unix path to a windows URI', () => {
Expand Down Expand Up @@ -165,6 +174,15 @@ describe('paths', () => {
}),
'/home/felix/mysource/source.php'
)
// longest prefix matching for local paths
assert.strictEqual(
convertDebuggerPathToClient('file:///app/subdir/source.php', {
'/var/www': '/home/felix/mysite',
'/app/subdir': '/home/felix/mysource/subdir1',
'/app': '/home/felix/mysource',
}),
'/home/felix/mysource/subdir1/source.php'
)
})
// unix to windows
;(process.platform === 'win32' ? it : it.skip)('should map unix uris to windows paths', () => {
Expand Down

0 comments on commit 3c66f87

Please sign in to comment.