11import fileUrl from 'file-url'
22import * as url from 'url'
3- import * as path from 'path'
3+ import * as Path from 'path'
44import minimatch from 'minimatch'
55
66/** converts a server-side Xdebug file URI to a local path for VS Code with respect to source root settings */
@@ -45,7 +45,7 @@ export function convertDebuggerPathToClient(fileUri: string, pathMapping?: { [in
4545 let pathname = u . pathname
4646 if ( isWindowsUri ( fileUri ) ) {
4747 // From Node.js lib/internal/url.js pathToFileURL
48- pathname = pathname . replace ( / \/ / g, path . win32 . sep )
48+ pathname = pathname . replace ( / \/ / g, Path . win32 . sep )
4949 pathname = decodeURIComponent ( pathname )
5050 if ( u . hostname !== '' ) {
5151 localPath = `\\\\${ url . domainToUnicode ( u . hostname ) } ${ pathname } `
@@ -123,14 +123,16 @@ function pathOrUrlToUrl(path: string): string {
123123 try {
124124 // try to parse, but do not modify
125125 new URL ( path ) . toString ( )
126- return path
126+ // super simple relative path resolver
127+ return simpleResolveUrl ( path )
127128 } catch ( ex ) {
128129 // should be a path
129130 }
130131 }
131132 // Not a URL, do some windows path mangling before it is converted to URL
132133 if ( path . startsWith ( '\\\\' ) ) {
133134 // UNC
135+ path = Path . win32 . resolve ( path )
134136 const hostEndIndex = path . indexOf ( '\\' , 2 )
135137 const host = path . substring ( 2 , hostEndIndex )
136138 const outURL = new URL ( 'file://' )
@@ -147,6 +149,7 @@ function pathOrUrlToUrl(path: string): string {
147149 // // Xdebug always lowercases Windows drive letters in file URIs
148150 // //path = path.replace(/^[A-Z]:/, match => match.toLowerCase())
149151 // }
152+ path = isWindowsPath ( path ) ? Path . win32 . resolve ( path ) : Path . posix . resolve ( path )
150153 return fileUrl ( path , { resolve : false } )
151154}
152155
@@ -163,3 +166,15 @@ export function isPositiveMatchInGlobs(path: string, globs: string[]): boolean {
163166 const f = globs . find ( glob => minimatch ( path , glob . charAt ( 0 ) == '!' ? glob . substring ( 1 ) : glob ) )
164167 return f !== undefined && f . charAt ( 0 ) !== '!'
165168}
169+
170+ function simpleResolveUrl ( path : string ) : string {
171+ if ( path . indexOf ( '/../' ) != - 1 ) {
172+ const pp = path . split ( '/' )
173+ let i
174+ while ( ( i = pp . findIndex ( v => v == '..' ) ) > 0 ) {
175+ pp . splice ( i - 1 , 2 )
176+ }
177+ path = pp . join ( '/' )
178+ }
179+ return path
180+ }
0 commit comments