Skip to content

Commit

Permalink
Normalize paths of workspace roots
Browse files Browse the repository at this point in the history
This avoids having workspace roots like /workspace/../../workspace when having relative paths in the workspace roots configuration.

Fixes eclipse-theia#7597

Signed-off-by: Cornelius A. Ludmann <cornelius.ludmann@typefox.io>
  • Loading branch information
corneliusludmann committed Apr 21, 2020
1 parent ebf7b78 commit 40c6322
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
32 changes: 32 additions & 0 deletions packages/workspace/src/browser/workspace-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,38 @@ describe('WorkspaceService', () => {
expect((<Map<string, Disposable>>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 = <FileStat>{
uri: workspaceFileUri,
lastModification: 0,
isDirectory: false
};
const rootRelative = '../workspace';
const rootActual = 'file:///workspace';
(<sinon.SinonStub>mockWorkspaceServer.getMostRecentlyUsedWorkspace).resolves(workspaceFileStat.uri);
const stubGetFileStat = (<sinon.SinonStub>mockFilesystem.getFileStat);
stubGetFileStat.withArgs(workspaceFileUri).resolves(workspaceFileStat);
(<sinon.SinonStub>mockFilesystem.exists).resolves(true);
(<sinon.SinonStub>mockFilesystem.resolveContent).resolves({
stat: workspaceFileStat,
content: `{"folders":[{"path":"${rootRelative}"}],"settings":{}}`
});
stubGetFileStat.withArgs(rootActual).resolves(<FileStat>{
uri: rootActual, lastModification: 0, isDirectory: true
});
(<sinon.SinonStub>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 = <FileStat>{
Expand Down
3 changes: 2 additions & 1 deletion packages/workspace/src/browser/workspace-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 40c6322

Please sign in to comment.