From 2fe7f7f38150a844ff95f9d36ac7556309e6c08c Mon Sep 17 00:00:00 2001 From: hnioche Date: Wed, 8 May 2019 18:05:23 +0100 Subject: [PATCH 1/5] Path manipulation using correct OS path class Allow case insensitive path comparison on Windows. Fixes path generation when local is Windows and remote is Posix. --- src/debugger/main.ts | 51 ++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/debugger/main.ts b/src/debugger/main.ts index 03e82a6d9..ea6534986 100644 --- a/src/debugger/main.ts +++ b/src/debugger/main.ts @@ -293,42 +293,61 @@ class RubyDebugSession extends DebugSession { return localPath.replace(/\\/g, '/'); } + private isSubPath(subPath: string): boolean { + return subPath && !subPath.startsWith('..') && !path.isAbsolute(subPath); + } + + private getPathImplementation(absolutePath: string): any { + return path && path.posix.isAbsolute(absolutePath) ? path.posix : path.win32; + } + protected convertClientPathToDebugger(localPath: string): string { - if (this.debugMode == Mode.launch) { + if (this.debugMode === Mode.launch) { return localPath; } - if (!localPath.startsWith(this.requestArguments.cwd)) { + let relativeLocalPath = path.relative(this.requestArguments.cwd, localPath) + + if (!this.isSubPath(relativeLocalPath)) { return localPath; } - var relativePath = path.join( - this.requestArguments.remoteWorkspaceRoot, localPath.substring(this.requestArguments.cwd.length) - ); - - var sepIndex = this.requestArguments.remoteWorkspaceRoot.lastIndexOf('/'); + let remotePathImplementation = this.getPathImplementation(this.requestArguments.remoteWorkspaceRoot); + let localPathImplementation = this.getPathImplementation(this.requestArguments.cwd); - if (sepIndex !== -1) { - // *inx or darwin - relativePath = relativePath.replace(/\\/g, '/'); - } + let relativePath = remotePathImplementation.join.apply( + null, + [this.requestArguments.remoteWorkspaceRoot].concat( + relativeLocalPath.split(localPathImplementation.sep) + ) + ); return relativePath; } - protected convertDebuggerPathToClient(serverPath: string):string{ - if (this.debugMode == Mode.launch) { + protected convertDebuggerPathToClient(serverPath: string): string { + if (this.debugMode === Mode.launch) { return serverPath; } - if (!serverPath.startsWith(this.requestArguments.remoteWorkspaceRoot)) { + let relativeRemotePath = path.relative(this.requestArguments.remoteWorkspaceRoot, serverPath) + + if (!this.isSubPath(relativeRemotePath)) { return serverPath; } + let remotePathImplementation = this.getPathImplementation(this.requestArguments.remoteWorkspaceRoot); + let localPathImplementation = this.getPathImplementation(this.requestArguments.cwd); + + // Path.join will convert the path using local OS preferred separator - var relativePath = path.join( - this.requestArguments.cwd, serverPath.substring(this.requestArguments.remoteWorkspaceRoot.length) + let relativePath = localPathImplementation.join.apply( + null, + [this.requestArguments.cwd].concat( + relativeRemotePath.split(remotePathImplementation.sep) + ) ); + return relativePath; } From 499594f4b241ad7ccb34d7a20385e9b1c117d3aa Mon Sep 17 00:00:00 2001 From: hnioche Date: Wed, 8 May 2019 20:40:23 +0100 Subject: [PATCH 2/5] Use remote path implementation to compare paths This fixes attaching vscode from a Posix environment to a debugger runing in Windows --- src/debugger/main.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/debugger/main.ts b/src/debugger/main.ts index ea6534986..5857372a3 100644 --- a/src/debugger/main.ts +++ b/src/debugger/main.ts @@ -330,17 +330,15 @@ class RubyDebugSession extends DebugSession { return serverPath; } - let relativeRemotePath = path.relative(this.requestArguments.remoteWorkspaceRoot, serverPath) + let remotePathImplementation = this.getPathImplementation(this.requestArguments.remoteWorkspaceRoot); + let localPathImplementation = this.getPathImplementation(this.requestArguments.cwd); + + let relativeRemotePath = remotePathImplementation.relative(this.requestArguments.remoteWorkspaceRoot, serverPath) if (!this.isSubPath(relativeRemotePath)) { return serverPath; } - let remotePathImplementation = this.getPathImplementation(this.requestArguments.remoteWorkspaceRoot); - let localPathImplementation = this.getPathImplementation(this.requestArguments.cwd); - - - // Path.join will convert the path using local OS preferred separator let relativePath = localPathImplementation.join.apply( null, [this.requestArguments.cwd].concat( From 6568d75b6ac61b447b38e1662a7d57ba09185ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9?= Date: Wed, 8 May 2019 20:50:07 +0100 Subject: [PATCH 3/5] Convert tabs to 4 spaces --- src/debugger/main.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/debugger/main.ts b/src/debugger/main.ts index 5857372a3..24b9fe6a9 100644 --- a/src/debugger/main.ts +++ b/src/debugger/main.ts @@ -330,10 +330,10 @@ class RubyDebugSession extends DebugSession { return serverPath; } - let remotePathImplementation = this.getPathImplementation(this.requestArguments.remoteWorkspaceRoot); - let localPathImplementation = this.getPathImplementation(this.requestArguments.cwd); + let remotePathImplementation = this.getPathImplementation(this.requestArguments.remoteWorkspaceRoot); + let localPathImplementation = this.getPathImplementation(this.requestArguments.cwd); - let relativeRemotePath = remotePathImplementation.relative(this.requestArguments.remoteWorkspaceRoot, serverPath) + let relativeRemotePath = remotePathImplementation.relative(this.requestArguments.remoteWorkspaceRoot, serverPath) if (!this.isSubPath(relativeRemotePath)) { return serverPath; From 3fc67cae21fcf132a4698b3478bcd778530bad29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9?= Date: Thu, 9 May 2019 09:43:25 +0100 Subject: [PATCH 4/5] Develop (#1) * Use a more accurate way to guess the path OS * Add missing semicolon * Default remote workspace to local one * Run prettier on changes --- src/debugger/main.ts | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/debugger/main.ts b/src/debugger/main.ts index 24b9fe6a9..9c12ea5bc 100644 --- a/src/debugger/main.ts +++ b/src/debugger/main.ts @@ -297,8 +297,16 @@ class RubyDebugSession extends DebugSession { return subPath && !subPath.startsWith('..') && !path.isAbsolute(subPath); } - private getPathImplementation(absolutePath: string): any { - return path && path.posix.isAbsolute(absolutePath) ? path.posix : path.win32; + private getPathImplementation(pathToCheck: string): any { + if (pathToCheck) { + if (pathToCheck.indexOf(path.posix.sep) >= 0) { + return path.posix; + } else if (pathToCheck.indexOf(path.win32.sep) >= 0) { + return path.win32; + } + } + + return path; } protected convertClientPathToDebugger(localPath: string): string { @@ -306,20 +314,21 @@ class RubyDebugSession extends DebugSession { return localPath; } - let relativeLocalPath = path.relative(this.requestArguments.cwd, localPath) + let relativeLocalPath = path.relative(this.requestArguments.cwd, localPath); if (!this.isSubPath(relativeLocalPath)) { return localPath; } - let remotePathImplementation = this.getPathImplementation(this.requestArguments.remoteWorkspaceRoot); + let remoteWorkspaceRoot = + this.requestArguments.remoteWorkspaceRoot || this.requestArguments.cwd; + + let remotePathImplementation = this.getPathImplementation(remoteWorkspaceRoot); let localPathImplementation = this.getPathImplementation(this.requestArguments.cwd); let relativePath = remotePathImplementation.join.apply( null, - [this.requestArguments.remoteWorkspaceRoot].concat( - relativeLocalPath.split(localPathImplementation.sep) - ) + [remoteWorkspaceRoot].concat(relativeLocalPath.split(localPathImplementation.sep)) ); return relativePath; @@ -330,10 +339,14 @@ class RubyDebugSession extends DebugSession { return serverPath; } - let remotePathImplementation = this.getPathImplementation(this.requestArguments.remoteWorkspaceRoot); + let remoteWorkspaceRoot = + this.requestArguments.remoteWorkspaceRoot || this.requestArguments.cwd; + + let remotePathImplementation = this.getPathImplementation(remoteWorkspaceRoot); let localPathImplementation = this.getPathImplementation(this.requestArguments.cwd); - let relativeRemotePath = remotePathImplementation.relative(this.requestArguments.remoteWorkspaceRoot, serverPath) + let relativeRemotePath = remotePathImplementation.relative(remoteWorkspaceRoot, serverPath); + if (!this.isSubPath(relativeRemotePath)) { return serverPath; @@ -341,9 +354,7 @@ class RubyDebugSession extends DebugSession { let relativePath = localPathImplementation.join.apply( null, - [this.requestArguments.cwd].concat( - relativeRemotePath.split(remotePathImplementation.sep) - ) + [this.requestArguments.cwd].concat(relativeRemotePath.split(remotePathImplementation.sep)) ); return relativePath; From 893b75ce7298c72663657651f6e576ef9d2afd01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9?= Date: Thu, 9 May 2019 09:47:07 +0100 Subject: [PATCH 5/5] Use spaces for indentation --- src/debugger/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/debugger/main.ts b/src/debugger/main.ts index 9c12ea5bc..774bcda55 100644 --- a/src/debugger/main.ts +++ b/src/debugger/main.ts @@ -354,7 +354,7 @@ class RubyDebugSession extends DebugSession { let relativePath = localPathImplementation.join.apply( null, - [this.requestArguments.cwd].concat(relativeRemotePath.split(remotePathImplementation.sep)) + [this.requestArguments.cwd].concat(relativeRemotePath.split(remotePathImplementation.sep)) ); return relativePath;