Skip to content

Commit

Permalink
fix: support relative paths and path mappings (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
zobo authored Feb 14, 2023
1 parent 453ebc3 commit 75c3e1d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.31.1]

- Fix relative paths and path mappings support.

## [1.31.0]

- Allow more flexible path mappings in url format.
Expand Down
21 changes: 18 additions & 3 deletions src/paths.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fileUrl from 'file-url'
import * as url from 'url'
import * as path from 'path'
import * as Path from 'path'
import minimatch from 'minimatch'

/** converts a server-side Xdebug file URI to a local path for VS Code with respect to source root settings */
Expand Down Expand Up @@ -45,7 +45,7 @@ export function convertDebuggerPathToClient(fileUri: string, pathMapping?: { [in
let pathname = u.pathname
if (isWindowsUri(fileUri)) {
// From Node.js lib/internal/url.js pathToFileURL
pathname = pathname.replace(/\//g, path.win32.sep)
pathname = pathname.replace(/\//g, Path.win32.sep)
pathname = decodeURIComponent(pathname)
if (u.hostname !== '') {
localPath = `\\\\${url.domainToUnicode(u.hostname)}${pathname}`
Expand Down Expand Up @@ -123,14 +123,16 @@ function pathOrUrlToUrl(path: string): string {
try {
// try to parse, but do not modify
new URL(path).toString()
return path
// super simple relative path resolver
return simpleResolveUrl(path)
} catch (ex) {
// should be a path
}
}
// Not a URL, do some windows path mangling before it is converted to URL
if (path.startsWith('\\\\')) {
// UNC
path = Path.win32.resolve(path)
const hostEndIndex = path.indexOf('\\', 2)
const host = path.substring(2, hostEndIndex)
const outURL = new URL('file://')
Expand All @@ -147,6 +149,7 @@ function pathOrUrlToUrl(path: string): string {
// // Xdebug always lowercases Windows drive letters in file URIs
// //path = path.replace(/^[A-Z]:/, match => match.toLowerCase())
// }
path = isWindowsPath(path) ? Path.win32.resolve(path) : Path.posix.resolve(path)
return fileUrl(path, { resolve: false })
}

Expand All @@ -163,3 +166,15 @@ export function isPositiveMatchInGlobs(path: string, globs: string[]): boolean {
const f = globs.find(glob => minimatch(path, glob.charAt(0) == '!' ? glob.substring(1) : glob))
return f !== undefined && f.charAt(0) !== '!'
}

function simpleResolveUrl(path: string): string {
if (path.indexOf('/../') != -1) {
const pp = path.split('/')
let i
while ((i = pp.findIndex(v => v == '..')) > 0) {
pp.splice(i - 1, 2)
}
path = pp.join('/')
}
return path
}
48 changes: 48 additions & 0 deletions src/test/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,38 @@ describe('paths', () => {
)
})
})
describe('relative paths', () => {
it('should resolve relative path posix', () => {
assert.equal(convertClientPathToDebugger('/var/www/foo/../bar'), 'file:///var/www/bar')
})
it('should resolve relative path maps posix', () => {
assert.equal(
convertClientPathToDebugger('/work/foo/test.php', {
'/var/www/html/bar': '/work/project/folder/../../foo',
}),
'file:///var/www/html/bar/test.php'
)
})
it('should resolve relative path win32', () => {
assert.equal(convertClientPathToDebugger('C:\\var\\www\\foo\\..\\bar'), 'file:///C:/var/www/bar')
})
it('should resolve relative path maps win32 to posix', () => {
assert.equal(
convertClientPathToDebugger('C:\\work\\foo\\test.php', {
'/var/www/html/bar': 'C:\\work\\project\\folder\\..\\..\\foo',
}),
'file:///var/www/html/bar/test.php'
)
})
it('should resolve relative path maps win32 to win32', () => {
assert.equal(
convertClientPathToDebugger('C:\\work\\foo\\test.php', {
'C:\\var\\www\\html\\bar': 'C:\\work\\project\\folder\\..\\..\\foo',
}),
'file:///C:/var/www/html/bar/test.php'
)
})
})
})
describe('convertDebuggerPathToClient', () => {
describe('without source mapping', () => {
Expand Down Expand Up @@ -366,6 +398,22 @@ describe('paths', () => {
'ssh://host/path/file.php'
)
})
it('should map sshfs to remote unix relative', () => {
assert.equal(
convertClientPathToDebugger('ssh://host/path/file.php', {
'/root/path': 'ssh://host/test/../path/',
}),
'file:///root/path/file.php'
)
})
it('should map remote unix to sshfs relative', () => {
assert.equal(
convertDebuggerPathToClient('file:///root/path/file.php', {
'/root/path': 'ssh://host/test/../path/',
}),
'ssh://host/path/file.php'
)
})
})
describe('UNC', () => {
it('should convert UNC to url', () => {
Expand Down

0 comments on commit 75c3e1d

Please sign in to comment.