File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -43,6 +43,10 @@ describe("workspaceRelativeFilePath", () => {
4343 [ "/Repo/Project/src/main.ts" , "/repo/project" , null ] ,
4444 [ "C:/USERS/mike/t3code/main.ts" , "c:/users/MIKE/t3code" , "main.ts" ] ,
4545 [ "/repo/project-two/a.ts" , "/repo/project" , null ] ,
46+ // regression: #16354 a `..` that leaves the root is a host path, not a workspace path
47+ [ "/repo/project/../other/notes.md" , "/repo/project" , null ] ,
48+ [ "C:\\repo\\project\\src\\..\\..\\notes.md" , "C:\\repo\\project" , null ] ,
49+ [ "/repo/project/docs/a/../b.md" , "/repo/project" , "docs/a/../b.md" ] ,
4650 ] ) ( "relates %s to %s" , ( path , workspaceRoot , relativePath ) => {
4751 expect ( workspaceRelativeFilePath ( path , workspaceRoot ) ) . toBe ( relativePath ) ;
4852 } ) ;
Original file line number Diff line number Diff line change @@ -98,7 +98,15 @@ export function workspaceRelativeFilePath(
9898 const rootForCompare = caseInsensitive ? normalizedRoot . toLowerCase ( ) : normalizedRoot ;
9999 if ( pathForCompare . replace ( / \/ + $ / , "" ) === rootForCompare ) return "." ;
100100 if ( ! pathForCompare . startsWith ( `${ rootForCompare } /` ) ) return null ;
101- return normalizedPath . slice ( normalizedRoot . length + 1 ) ;
101+ const relativePath = normalizedPath . slice ( normalizedRoot . length + 1 ) ;
102+ // `/repo/../x` starts with the root but escapes it; the server rejects that as a workspace path.
103+ let depth = 0 ;
104+ for ( const segment of relativePath . split ( "/" ) ) {
105+ if ( segment === ".." ) depth -= 1 ;
106+ else if ( segment . length > 0 && segment !== "." ) depth += 1 ;
107+ if ( depth < 0 ) return null ;
108+ }
109+ return relativePath ;
102110}
103111
104112export function isAbsolutePath ( value : string ) : boolean {
You can’t perform that action at this time.
0 commit comments