Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debt - clean up some todos #168158

Merged
merged 1 commit into from Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 2 additions & 32 deletions src/vs/platform/backup/electron-main/backupMainService.ts
Expand Up @@ -11,7 +11,7 @@ import { isLinux } from 'vs/base/common/platform';
import { extUriBiasedIgnorePathCase } from 'vs/base/common/resources';
import { Promises, RimRafMode } from 'vs/base/node/pfs';
import { IBackupMainService } from 'vs/platform/backup/electron-main/backup';
import { ISerializedBackupWorkspaces, IEmptyWindowBackupInfo, isEmptyWindowBackupInfo, deserializeWorkspaceInfos, deserializeFolderInfos, ISerializedWorkspaceBackupInfo, ISerializedFolderBackupInfo, ISerializedEmptyWindowBackupInfo, ILegacySerializedBackupWorkspaces } from 'vs/platform/backup/node/backup';
import { ISerializedBackupWorkspaces, IEmptyWindowBackupInfo, isEmptyWindowBackupInfo, deserializeWorkspaceInfos, deserializeFolderInfos, ISerializedWorkspaceBackupInfo, ISerializedFolderBackupInfo, ISerializedEmptyWindowBackupInfo } from 'vs/platform/backup/node/backup';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IEnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService';
import { IStateMainService } from 'vs/platform/state/electron-main/state';
Expand Down Expand Up @@ -50,7 +50,7 @@ export class BackupMainService implements IBackupMainService {
async initialize(): Promise<void> {

// read backup workspaces
const serializedBackupWorkspaces = await this.initializeAndMigrateBackupWorkspacesMetadata();
const serializedBackupWorkspaces = this.stateMainService.getItem<ISerializedBackupWorkspaces>(BackupMainService.backupWorkspacesMetadataStorageKey) ?? { workspaces: [], folders: [], emptyWindows: [] };

// validate empty workspaces backups first
this.emptyWindows = await this.validateEmptyWorkspaces(serializedBackupWorkspaces.emptyWindows);
Expand All @@ -65,36 +65,6 @@ export class BackupMainService implements IBackupMainService {
this.storeWorkspacesMetadata();
}

private async initializeAndMigrateBackupWorkspacesMetadata(): Promise<ISerializedBackupWorkspaces> {
let serializedBackupWorkspaces = this.stateMainService.getItem<ISerializedBackupWorkspaces>(BackupMainService.backupWorkspacesMetadataStorageKey);
if (!serializedBackupWorkspaces) {
try {
//TODO@bpasero remove after a while
const legacyBackupWorkspacesPath = join(this.backupHome, 'workspaces.json');
const legacyBackupWorkspaces = await Promises.readFile(legacyBackupWorkspacesPath, 'utf8');

try {
await Promises.unlink(legacyBackupWorkspacesPath);
} catch (error) {
// ignore
}

const legacySerializedBackupWorkspaces = JSON.parse(legacyBackupWorkspaces) as ILegacySerializedBackupWorkspaces;
serializedBackupWorkspaces = {
workspaces: Array.isArray(legacySerializedBackupWorkspaces.rootURIWorkspaces) ? legacySerializedBackupWorkspaces.rootURIWorkspaces : [],
folders: Array.isArray(legacySerializedBackupWorkspaces.folderWorkspaceInfos) ? legacySerializedBackupWorkspaces.folderWorkspaceInfos : [],
emptyWindows: Array.isArray(legacySerializedBackupWorkspaces.emptyWorkspaceInfos) ? legacySerializedBackupWorkspaces.emptyWorkspaceInfos : [],
};
} catch (error) {
if (error.code !== 'ENOENT') {
this.logService.error(`Backup: Could not migrate legacy backup workspaces metadata: ${error.toString()}`);
}
}
}

return serializedBackupWorkspaces ?? { workspaces: [], folders: [], emptyWindows: [] };
}

protected getWorkspaceBackups(): IWorkspaceBackupInfo[] {
if (this.isHotExitOnExitAndWindowClose()) {
// Only non-folder windows are restored on main process launch when
Expand Down
Expand Up @@ -81,11 +81,6 @@ flakySuite('BackupMainService', () => {
return workspace;
}

function ensureEmptyWindowExists(id: string): Promise<void> {
const backupFolder = service.toBackupPath(id);
return createBackupFolder(backupFolder);
}

async function createBackupFolder(backupFolder: string): Promise<void> {
if (!fs.existsSync(backupFolder)) {
fs.mkdirSync(backupFolder);
Expand Down Expand Up @@ -171,73 +166,6 @@ flakySuite('BackupMainService', () => {
return Promises.rm(testDir);
});

test('service migrates from old workspace.json', async function () {
const legacyWorkspaceJsonPath = path.join(backupHome, 'workspaces.json');

// malformed file
stateMainService.removeItem('backupWorkspaces');
await Promises.writeFile(legacyWorkspaceJsonPath, '{ hello world');
await service.initialize();
assert.ok(!await Promises.exists(legacyWorkspaceJsonPath));
assert.equal(service.getEmptyWindowBackups().length, 0);
assert.equal(service.testGetFolderBackups().length, 0);
assert.equal(service.testGetWorkspaceBackups().length, 0);

// file with empty contents (1)
stateMainService.removeItem('backupWorkspaces');
await Promises.writeFile(legacyWorkspaceJsonPath, '{}');
await service.initialize();
assert.ok(!await Promises.exists(legacyWorkspaceJsonPath));
assert.equal(service.getEmptyWindowBackups().length, 0);
assert.equal(service.testGetFolderBackups().length, 0);
assert.equal(service.testGetWorkspaceBackups().length, 0);

// file with empty contents (2)
stateMainService.removeItem('backupWorkspaces');
await Promises.writeFile(legacyWorkspaceJsonPath, JSON.stringify({
rootURIWorkspaces: [],
folderWorkspaceInfos: [],
emptyWorkspaceInfos: []
}));
await service.initialize();
assert.ok(!await Promises.exists(legacyWorkspaceJsonPath));
assert.equal(service.getEmptyWindowBackups().length, 0);
assert.equal(service.testGetFolderBackups().length, 0);
assert.equal(service.testGetWorkspaceBackups().length, 0);

// file with contents

const workspacePath = path.join(testDir, 'Foo.code-workspace');
const workspace1 = await ensureWorkspaceExists(toWorkspace(workspacePath));
await ensureFolderExists(existingTestFolder1);
const emptyWindowWorkspaceId = '1662626964151';
await ensureEmptyWindowExists(emptyWindowWorkspaceId);

stateMainService.removeItem('backupWorkspaces');
await Promises.writeFile(legacyWorkspaceJsonPath, JSON.stringify({
rootURIWorkspaces: [workspace1].map(toSerializedWorkspace),
folderWorkspaceInfos: [{ folderUri: existingTestFolder1.toString() }],
emptyWorkspaceInfos: [{ backupFolder: emptyWindowWorkspaceId }]
}));
await service.initialize();
assert.ok(!await Promises.exists(legacyWorkspaceJsonPath));
assert.equal(service.getEmptyWindowBackups().length, 1);
assert.equal(service.testGetFolderBackups().length, 1);
assert.equal(service.testGetWorkspaceBackups().length, 1);

// subsequent initialize ignores file

await Promises.writeFile(legacyWorkspaceJsonPath, JSON.stringify({
rootURIWorkspaces: [],
folderWorkspaceInfos: [],
emptyWorkspaceInfos: []
}));
await service.initialize();
assert.equal(service.getEmptyWindowBackups().length, 1);
assert.equal(service.testGetFolderBackups().length, 1);
assert.equal(service.testGetWorkspaceBackups().length, 1);
});

test('service validates backup workspaces on startup and cleans up (folder workspaces)', async function () {

// 1) backup workspace path does not exist
Expand Down
16 changes: 2 additions & 14 deletions src/vs/workbench/browser/layout.ts
Expand Up @@ -31,7 +31,7 @@ import { IStatusbarService } from 'vs/workbench/services/statusbar/browser/statu
import { IFileService } from 'vs/platform/files/common/files';
import { isCodeEditor } from 'vs/editor/browser/editorBrowser';
import { coalesce } from 'vs/base/common/arrays';
import { assertIsDefined, isNumber } from 'vs/base/common/types';
import { assertIsDefined } from 'vs/base/common/types';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { WINDOW_ACTIVE_BORDER, WINDOW_INACTIVE_BORDER } from 'vs/workbench/common/theme';
Expand Down Expand Up @@ -683,23 +683,11 @@ export abstract class Layout extends Disposable implements IWorkbenchLayoutServi
return {
layout: defaultLayout.layout?.editors,
filesToOpenOrCreate: defaultLayout?.editors?.map(editor => {
// TODO@bpasero remove me eventually
const editor2 = editor as any;
const legacySelection = editor2.selection && editor2.selection.start && isNumber(editor2.selection.start.line) ? {
startLineNumber: editor2.selection.start.line,
startColumn: isNumber(editor2.selection.start.column) ? editor2.selection.start.column : 1,
endLineNumber: isNumber(editor2.selection.end.line) ? editor2.selection.end.line : undefined,
endColumn: isNumber(editor2.selection.end.line) ? (isNumber(editor2.selection.end.column) ? editor2.selection.end.column : 1) : undefined,
} : undefined;

return {
viewColumn: editor.viewColumn,
fileUri: URI.revive(editor.uri),
openOnlyIfExists: editor.openOnlyIfExists,
options: {
selection: legacySelection,
...editor.options // keep at the end to override legacy selection/override that may be `undefined`
}
options: editor.options
};
})
};
Expand Down