Skip to content

Commit f34de12

Browse files
author
Benjamin Pasero
committed
Cannot Open my old workspace (fixes microsoft#33239)
1 parent e9cf1bb commit f34de12

4 files changed

Lines changed: 20 additions & 52 deletions

File tree

src/vs/platform/workspace/common/workspace.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,26 +150,16 @@ export class Workspace implements IWorkspace {
150150
this.roots = roots;
151151
}
152152

153-
private ensureAbsoluteAndUnique(roots: URI[]): URI[] {
154-
if (!this.configuration) {
155-
return roots;
156-
}
157-
158-
return distinct(roots.map(root => {
159-
if (paths.isAbsolute(root.fsPath)) {
160-
return URI.file(root.fsPath);
161-
}
162-
163-
return URI.file(paths.join(paths.dirname(this.configuration.fsPath), root.fsPath));
164-
}), root => isLinux ? root.fsPath : root.fsPath.toLowerCase());
153+
private ensureUnique(roots: URI[]): URI[] {
154+
return distinct(roots, root => isLinux ? root.fsPath : root.fsPath.toLowerCase());
165155
}
166156

167157
public get roots(): URI[] {
168158
return this._roots;
169159
}
170160

171161
public set roots(roots: URI[]) {
172-
this._roots = this.ensureAbsoluteAndUnique(roots);
162+
this._roots = this.ensureUnique(roots);
173163
this.updateRootsMap();
174164
}
175165

src/vs/platform/workspace/test/common/workspace.test.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,15 @@
88
import * as assert from 'assert';
99
import { Workspace } from 'vs/platform/workspace/common/workspace';
1010
import URI from 'vs/base/common/uri';
11-
import { join, dirname } from 'vs/base/common/paths';
1211

1312
suite('Workspace', () => {
1413

15-
test('Workspace ensures absolute and unique roots', () => {
14+
test('Workspace ensures unique roots', () => {
1615

1716
// Unique
1817
let roots = [URI.file('/some/path'), URI.file('/some/path')];
1918
let ws = new Workspace('id', 'name', roots, URI.file('/config'));
2019

2120
assert.equal(ws.roots.length, 1);
22-
23-
// Absolute
24-
let config = URI.file('/someFolder/workspace.code-workspace');
25-
roots = [URI.parse('./some/path'), URI.parse('some/other/path')];
26-
ws = new Workspace('id', 'name', roots, config);
27-
28-
assert.equal(ws.roots.length, 2);
29-
assert.equal(ws.roots[0].fsPath, URI.file(join(dirname(config.fsPath), roots[0].fsPath)).fsPath);
30-
assert.equal(ws.roots[1].fsPath, URI.file(join(dirname(config.fsPath), roots[1].fsPath)).fsPath);
31-
32-
// Absolute (from root)
33-
config = URI.file('/workspace.code-workspace');
34-
roots = [URI.parse('./some/path'), URI.parse('some/other/path')];
35-
ws = new Workspace('id', 'name', roots, config);
36-
37-
assert.equal(ws.roots.length, 2);
38-
assert.equal(ws.roots[0].fsPath, URI.file(join(dirname(config.fsPath), roots[0].fsPath)).fsPath);
39-
assert.equal(ws.roots[1].fsPath, URI.file(join(dirname(config.fsPath), roots[1].fsPath)).fsPath);
4021
});
4122
});

src/vs/workbench/services/configuration/common/configurationModels.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
'use strict';
66

77
import { clone } from 'vs/base/common/objects';
8-
import URI from 'vs/base/common/uri';
98
import { CustomConfigurationModel, toValuesTree } from 'vs/platform/configuration/common/model';
109
import { ConfigurationModel } from 'vs/platform/configuration/common/configuration';
1110
import { Registry } from 'vs/platform/registry/common/platform';
@@ -16,7 +15,7 @@ import { IStoredWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces
1615
export class WorkspaceConfigurationModel<T> extends CustomConfigurationModel<T> {
1716

1817
private _raw: T;
19-
private _folders: URI[];
18+
private _folders: string[];
2019
private _worksapaceSettings: ConfigurationModel<T>;
2120
private _tasksConfiguration: ConfigurationModel<T>;
2221
private _launchConfiguration: ConfigurationModel<T>;
@@ -28,7 +27,7 @@ export class WorkspaceConfigurationModel<T> extends CustomConfigurationModel<T>
2827
this._workspaceConfiguration = this.consolidate();
2928
}
3029

31-
get folders(): URI[] {
30+
get folders(): string[] {
3231
return this._folders;
3332
}
3433

@@ -39,26 +38,14 @@ export class WorkspaceConfigurationModel<T> extends CustomConfigurationModel<T>
3938
protected processRaw(raw: T): void {
4039
this._raw = raw;
4140

42-
this._folders = this.parseFolders();
41+
this._folders = (this._raw['folders'] as IStoredWorkspaceFolder[]).map(folder => folder.path);
4342
this._worksapaceSettings = this.parseConfigurationModel('settings');
4443
this._tasksConfiguration = this.parseConfigurationModel('tasks');
4544
this._launchConfiguration = this.parseConfigurationModel('launch');
4645

4746
super.processRaw(raw);
4847
}
4948

50-
private parseFolders(): URI[] {
51-
const folders: IStoredWorkspaceFolder[] = this._raw['folders'] || [];
52-
53-
return folders.map(folder => {
54-
try {
55-
return URI.parse(folder.path);
56-
} catch (error) {
57-
return null; // parsing a URI can fail for invalid characters
58-
}
59-
}).filter(f => !!f);
60-
}
61-
6249
private parseConfigurationModel(section: string): ConfigurationModel<T> {
6350
const rawSection = this._raw[section] || {};
6451
const contents = toValuesTree(rawSection, message => console.error(`Conflict in section '${section}' of workspace configuration file ${message}`));

src/vs/workbench/services/configuration/node/configuration.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,23 @@ export class WorkspaceServiceImpl extends WorkspaceService {
441441
}
442442
const workspaceId = (this.workspaceIdentifier as IWorkspaceIdentifier).id;
443443
const workspaceName = getWorkspaceLabel({ id: workspaceId, configPath: this.workspaceConfigPath.fsPath }, this.environmentService);
444-
this.workspace = new Workspace(workspaceId, workspaceName, workspaceConfigurationModel.folders, this.workspaceConfigPath);
444+
this.workspace = new Workspace(workspaceId, workspaceName, this.parseWorkspaceFolders(workspaceConfigurationModel.folders), this.workspaceConfigPath);
445445
this.legacyWorkspace = new LegacyWorkspace(this.workspace.roots[0]);
446446
this._register(this.workspaceConfiguration.onDidUpdateConfiguration(() => this.onWorkspaceConfigurationChanged()));
447447
return null;
448448
});
449449
}
450450

451+
private parseWorkspaceFolders(configuredFolders: string[]): URI[] {
452+
return configuredFolders.map(configuredFolder => {
453+
if (paths.isAbsolute(configuredFolder)) {
454+
return URI.file(configuredFolder);
455+
}
456+
457+
return URI.file(paths.join(paths.dirname(this.workspaceConfigPath.fsPath), configuredFolder));
458+
});
459+
}
460+
451461
private registerWorkspaceConfigSchema(): void {
452462
const contributionRegistry = Registry.as<IJSONContributionRegistry>(JSONExtensions.JSONContribution);
453463
if (!contributionRegistry.getSchemaContributions().schemas['vscode://schemas/workspaceConfig']) {
@@ -525,8 +535,8 @@ export class WorkspaceServiceImpl extends WorkspaceService {
525535
}
526536

527537
private onWorkspaceConfigurationChanged(): void {
528-
let configuredFolders = this.workspaceConfiguration.workspaceConfigurationModel.folders;
529-
const foldersChanged = !equals(this.workspace.roots, configuredFolders, (r1, r2) => r1.toString() === r2.toString());
538+
let configuredFolders = this.parseWorkspaceFolders(this.workspaceConfiguration.workspaceConfigurationModel.folders);
539+
const foldersChanged = !equals(this.workspace.roots, configuredFolders, (r1, r2) => r1.fsPath === r2.fsPath);
530540
if (foldersChanged) { // TODO@Sandeep be smarter here about detecting changes
531541
this.workspace.roots = configuredFolders;
532542
this.onFoldersChanged()

0 commit comments

Comments
 (0)