Skip to content

Commit 8634e57

Browse files
committed
VSCode Insiders crashes on open with TypeError: Cannot read property 'lastIndexOf' of undefined. Fixes microsoft#54933
1 parent 2525f40 commit 8634e57

1 file changed

Lines changed: 37 additions & 17 deletions

File tree

src/vs/platform/history/electron-main/historyMainService.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ import { Schemas } from 'vs/base/common/network';
2626
import { IUriDisplayService } from 'vs/platform/uriDisplay/common/uriDisplay';
2727

2828
interface ISerializedRecentlyOpened {
29-
workspaces: (IWorkspaceIdentifier | string | UriComponents)[];
29+
workspaces2: (IWorkspaceIdentifier | string)[]; // IWorkspaceIdentifier or URI.toString()
3030
files: string[];
3131
}
3232

33+
interface ILegacySerializedRecentlyOpened {
34+
workspaces: (IWorkspaceIdentifier | string | UriComponents)[]; // legacy (UriComponents was also supported for a few insider builds)
35+
}
36+
3337
export class HistoryMainService implements IHistoryMainService {
3438

3539
private static readonly MAX_TOTAL_RECENT_ENTRIES = 100;
@@ -246,35 +250,51 @@ export class HistoryMainService implements IHistoryMainService {
246250
}
247251

248252
private getRecentlyOpenedFromStorage(): IRecentlyOpened {
249-
const storedRecents: ISerializedRecentlyOpened = this.stateService.getItem<ISerializedRecentlyOpened>(HistoryMainService.recentlyOpenedStorageKey);
253+
const storedRecents = this.stateService.getItem<ISerializedRecentlyOpened & ILegacySerializedRecentlyOpened>(HistoryMainService.recentlyOpenedStorageKey);
250254
const result: IRecentlyOpened = { workspaces: [], files: [] };
251-
if (storedRecents && Array.isArray(storedRecents.workspaces)) {
252-
for (const workspace of storedRecents.workspaces) {
253-
if (typeof workspace === 'string') {
254-
result.workspaces.push(URI.file(workspace));
255-
} else if (isWorkspaceIdentifier(workspace)) {
256-
result.workspaces.push(workspace);
257-
} else {
258-
result.workspaces.push(URI.revive(workspace));
255+
if (storedRecents) {
256+
if (Array.isArray(storedRecents.workspaces2)) {
257+
for (const workspace of storedRecents.workspaces2) {
258+
if (isWorkspaceIdentifier(workspace)) {
259+
result.workspaces.push(workspace);
260+
} else if (typeof workspace === 'string') {
261+
result.workspaces.push(URI.parse(workspace));
262+
}
263+
}
264+
} else if (Array.isArray(storedRecents.workspaces)) {
265+
// format of 1.25 and before
266+
for (const workspace of storedRecents.workspaces) {
267+
if (typeof workspace === 'string') {
268+
result.workspaces.push(URI.file(workspace));
269+
} else if (isWorkspaceIdentifier(workspace)) {
270+
result.workspaces.push(workspace);
271+
} else if (workspace && typeof workspace.path === 'string' && typeof workspace.scheme === 'string') {
272+
// added by 1.26-insiders
273+
result.workspaces.push(URI.revive(workspace));
274+
}
275+
}
276+
}
277+
if (Array.isArray(storedRecents.files)) {
278+
for (const file of storedRecents.files) {
279+
if (typeof file === 'string') {
280+
result.files.push(file);
281+
}
259282
}
260283
}
261-
}
262-
if (storedRecents && Array.isArray(storedRecents.files)) {
263-
result.files.push(...storedRecents.files);
264284
}
265285
return result;
266286
}
267287

268288
private saveRecentlyOpened(recent: IRecentlyOpened): void {
269-
const serialized: ISerializedRecentlyOpened = { workspaces: [], files: recent.files };
289+
const serialized: ISerializedRecentlyOpened = { workspaces2: [], files: recent.files };
270290
for (const workspace of recent.workspaces) {
271291
if (isSingleFolderWorkspaceIdentifier(workspace)) {
272-
serialized.workspaces.push(<UriComponents>workspace.toJSON());
292+
serialized.workspaces2.push(workspace.toString());
273293
} else {
274-
serialized.workspaces.push(workspace);
294+
serialized.workspaces2.push(workspace);
275295
}
276296
}
277-
this.stateService.setItem(HistoryMainService.recentlyOpenedStorageKey, recent);
297+
this.stateService.setItem(HistoryMainService.recentlyOpenedStorageKey, serialized);
278298
}
279299

280300
updateWindowsJumpList(): void {

0 commit comments

Comments
 (0)