From 40c63228b99bb19e0ebf0be4027c14445af7374f Mon Sep 17 00:00:00 2001 From: "Cornelius A. Ludmann" Date: Tue, 21 Apr 2020 10:13:13 +0000 Subject: [PATCH] Normalize paths of workspace roots This avoids having workspace roots like /workspace/../../workspace when having relative paths in the workspace roots configuration. Fixes #7597 Signed-off-by: Cornelius A. Ludmann --- .../src/browser/workspace-service.spec.ts | 32 +++++++++++++++++++ .../src/browser/workspace-service.ts | 3 +- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/workspace/src/browser/workspace-service.spec.ts b/packages/workspace/src/browser/workspace-service.spec.ts index bd823250b09a4..f0e73c1bbefbd 100644 --- a/packages/workspace/src/browser/workspace-service.spec.ts +++ b/packages/workspace/src/browser/workspace-service.spec.ts @@ -229,6 +229,38 @@ describe('WorkspaceService', () => { expect((>wsService['rootWatchers']).has(rootB)).to.be.true; }); + it( + 'should resolve a relative workspace root path to a normalized root path', + async () => { + const workspaceFilePath = '/home/workspaceFile'; + const workspaceFileUri = 'file://' + workspaceFilePath; + const workspaceFileStat = { + uri: workspaceFileUri, + lastModification: 0, + isDirectory: false + }; + const rootRelative = '../workspace'; + const rootActual = 'file:///workspace'; + (mockWorkspaceServer.getMostRecentlyUsedWorkspace).resolves(workspaceFileStat.uri); + const stubGetFileStat = (mockFilesystem.getFileStat); + stubGetFileStat.withArgs(workspaceFileUri).resolves(workspaceFileStat); + (mockFilesystem.exists).resolves(true); + (mockFilesystem.resolveContent).resolves({ + stat: workspaceFileStat, + content: `{"folders":[{"path":"${rootRelative}"}],"settings":{}}` + }); + stubGetFileStat.withArgs(rootActual).resolves({ + uri: rootActual, lastModification: 0, isDirectory: true + }); + (mockFileSystemWatcher.watchFileChanges).resolves(new DisposableCollection()); + + await wsService['init'](); + expect(wsService.workspace).to.eq(workspaceFileStat); + expect((await wsService.roots).length).to.eq(1); + expect(wsService.tryGetRoots().length).to.eq(1); + expect(wsService.tryGetRoots()[0].uri).to.eq(rootActual); + }); + it('should set the exposed roots an empty array if the workspace file stores invalid workspace data', async () => { const workspaceFileUri = 'file:///home/workspaceFile'; const workspaceFileStat = { diff --git a/packages/workspace/src/browser/workspace-service.ts b/packages/workspace/src/browser/workspace-service.ts index 64a2b86da72da..ace89e95c4f78 100644 --- a/packages/workspace/src/browser/workspace-service.ts +++ b/packages/workspace/src/browser/workspace-service.ts @@ -427,7 +427,8 @@ export class WorkspaceService implements FrontendApplicationContribution { if (uriStr.endsWith('/')) { uriStr = uriStr.slice(0, -1); } - const fileStat = await this.fileSystem.getFileStat(uriStr); + const normalizedUriStr = new URI(uriStr).normalizePath().toString(); + const fileStat = await this.fileSystem.getFileStat(normalizedUriStr); if (!fileStat) { return undefined; }