Skip to content

Commit 10aa709

Browse files
committed
Disable hot exit on empty workspaces, fix uncaught exceptions
1 parent c280e0c commit 10aa709

4 files changed

Lines changed: 82 additions & 17 deletions

File tree

src/vs/platform/backup/node/backupService.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,17 @@ export class BackupService implements IBackupService {
3232
@IWorkspaceContextService contextService?: IWorkspaceContextService
3333
) {
3434
// IWorkspaceContextService will not exist on the main process
35-
if (contextService) {
36-
this.workspaceResource = contextService.getWorkspace().resource;
35+
if (!contextService) {
36+
return;
3737
}
38+
39+
// Hot exit is disabled for empty workspaces
40+
const workspace = contextService.getWorkspace();
41+
if (!workspace) {
42+
return;
43+
}
44+
45+
this.workspaceResource = workspace.resource;
3846
}
3947

4048
public getWorkspaceBackupPaths(): string[] {
@@ -52,6 +60,10 @@ export class BackupService implements IBackupService {
5260
public pushWorkspaceBackupPaths(workspaces: string[]): void {
5361
this.load();
5462
workspaces.forEach(workspace => {
63+
// Hot exit is disabled for empty workspaces
64+
if (!workspace) {
65+
return;
66+
}
5567
if (!this.fileContent.folderWorkspaces[workspace]) {
5668
this.fileContent.folderWorkspaces[workspace] = [];
5769
}
@@ -74,6 +86,11 @@ export class BackupService implements IBackupService {
7486
}
7587

7688
public getWorkspaceUntitledFileBackups(workspace: string): string[] {
89+
// Hot exit is disabled for empty workspaces
90+
if (!this.workspaceResource) {
91+
return;
92+
}
93+
7794
const workspaceHash = crypto.createHash('md5').update(this.workspaceResource.fsPath).digest('hex');
7895
const untitledDir = path.join(this.environmentService.backupHome, workspaceHash, 'untitled');
7996
try {
@@ -87,6 +104,10 @@ export class BackupService implements IBackupService {
87104
}
88105

89106
public getBackupResource(resource: Uri): Uri {
107+
// Hot exit is disabled for empty workspaces
108+
if (!this.workspaceResource) {
109+
return;
110+
}
90111

91112
const workspaceHash = crypto.createHash('md5').update(this.workspaceResource.fsPath).digest('hex');
92113
const backupName = crypto.createHash('md5').update(resource.fsPath).digest('hex');
@@ -96,6 +117,11 @@ export class BackupService implements IBackupService {
96117
}
97118

98119
public registerResourceForBackup(resource: Uri): void {
120+
// Hot exit is disabled for empty workspaces
121+
if (!this.workspaceResource) {
122+
return;
123+
}
124+
99125
this.load();
100126
if (arrays.contains(this.fileContent.folderWorkspaces[this.workspaceResource.fsPath], resource.fsPath)) {
101127
return;
@@ -105,6 +131,11 @@ export class BackupService implements IBackupService {
105131
}
106132

107133
public deregisterResourceForBackup(resource: Uri): void {
134+
// Hot exit is disabled for empty workspaces
135+
if (!this.workspaceResource) {
136+
return;
137+
}
138+
108139
this.load();
109140
this.fileContent.folderWorkspaces[this.workspaceResource.fsPath] = this.fileContent.folderWorkspaces[this.workspaceResource.fsPath].filter(value => value !== resource.fsPath);
110141
this.save();

src/vs/workbench/electron-browser/workbench.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,22 @@ export class Workbench implements IPartService {
172172
serviceCollection
173173
};
174174

175-
// Restore any backups if they exist
176-
options.filesToRestore = this.backupService.getWorkspaceTextFilesWithBackups(workspace.resource.fsPath).map(filePath => {
177-
return { resource: Uri.file(filePath), options: { pinned: true } };
178-
});
179-
options.untitledFilesToRestore = this.backupService.getWorkspaceUntitledFileBackups(workspace.resource.fsPath).map(untitledFilePath => {
180-
return { resource: Uri.file(untitledFilePath), options: { pinned: true } };
181-
});
175+
// Restore any backups if they exist for this workspace (empty workspaces are not supported yet)
176+
if (workspace) {
177+
options.filesToRestore = this.backupService.getWorkspaceTextFilesWithBackups(workspace.resource.fsPath).map(filePath => {
178+
return { resource: Uri.file(filePath), options: { pinned: true } };
179+
});
180+
options.untitledFilesToRestore = this.backupService.getWorkspaceUntitledFileBackups(workspace.resource.fsPath).map(untitledFilePath => {
181+
return { resource: Uri.file(untitledFilePath), options: { pinned: true } };
182+
});
183+
}
182184

183185
this.hasFilesToCreateOpenOrDiff =
184186
(options.filesToCreate && options.filesToCreate.length > 0) ||
185187
(options.filesToOpen && options.filesToOpen.length > 0) ||
186188
(options.filesToDiff && options.filesToDiff.length > 0) ||
187-
(options.filesToRestore.length > 0) ||
188-
(options.untitledFilesToRestore.length > 0);
189+
(options.filesToRestore && options.filesToRestore.length > 0) ||
190+
(options.untitledFilesToRestore && options.untitledFilesToRestore.length > 0);
189191

190192
this.toDispose = [];
191193
this.toShutdown = [];

src/vs/workbench/services/files/node/fileService.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,17 +463,36 @@ export class FileService implements IFileService {
463463
this.backupService.registerResourceForBackup(resource);
464464
}
465465
const backupResource = this.getBackupPath(resource);
466+
467+
// Hot exit is disabled for empty workspaces
468+
if (!backupResource) {
469+
return TPromise.as(null);
470+
}
471+
466472
console.log(`Backing up to ${backupResource.fsPath}`);
467473
return this.updateContent(backupResource, content);
468474
}
469475

470476
public discardBackup(resource: uri): TPromise<void> {
471477
this.backupService.deregisterResourceForBackup(resource);
472-
return this.del(this.getBackupPath(resource));
478+
const backupResource = this.getBackupPath(resource);
479+
480+
// Hot exit is disabled for empty workspaces
481+
if (!backupResource) {
482+
return TPromise.as(null);
483+
}
484+
485+
return this.del(backupResource);
473486
}
474487

475488
public discardBackups(): TPromise<void> {
476-
return this.del(uri.file(this.getBackupRoot()));
489+
// Hot exit is disabled for empty workspaces
490+
const backupRootPath = this.getBackupRootPath();
491+
if (!backupRootPath) {
492+
return TPromise.as(void 0);
493+
}
494+
495+
return this.del(uri.file(backupRootPath));
477496
}
478497

479498
public isHotExitEnabled(): boolean {
@@ -483,13 +502,25 @@ export class FileService implements IFileService {
483502
// Helpers
484503

485504
private getBackupPath(resource: uri): uri {
505+
// Hot exit is disabled for empty workspaces
506+
const backupRootPath = this.getBackupRootPath();
507+
if (!backupRootPath) {
508+
return null;
509+
}
510+
486511
const backupName = crypto.createHash('md5').update(resource.fsPath).digest('hex');
487-
const backupPath = paths.join(this.getBackupRoot(), resource.scheme, backupName);
512+
const backupPath = paths.join(backupRootPath, resource.scheme, backupName);
488513
return uri.file(backupPath);
489514
}
490515

491-
private getBackupRoot(): string {
492-
let workspaceHash = crypto.createHash('md5').update(this.contextService.getWorkspace().resource.fsPath).digest('hex');
516+
private getBackupRootPath(): string {
517+
// Hot exit is disabled for empty workspaces
518+
const workspace = this.contextService.getWorkspace();
519+
if (!workspace) {
520+
return null;
521+
}
522+
523+
const workspaceHash = crypto.createHash('md5').update(workspace.resource.fsPath).digest('hex');
493524
return paths.join(this.environmentService.userDataPath, 'Backups', workspaceHash);
494525
}
495526

src/vs/workbench/services/textfile/browser/textFileService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ export abstract class TextFileService implements ITextFileService {
231231
this.saveAll().done(null, errors.onUnexpectedError);
232232
}
233233

234-
this.configuredHotExit = configuration && configuration.files && configuration.files.hotExit;
234+
// Hot exit is disabled for empty workspaces
235+
this.configuredHotExit = this.contextService.getWorkspace() && configuration && configuration.files && configuration.files.hotExit;
235236

236237
// Check for change in files associations
237238
const filesAssociation = configuration && configuration.files && configuration.files.associations;

0 commit comments

Comments
 (0)