Skip to content

Commit 942cc4b

Browse files
authored
* Fix microsoft#162439 * better fix and also add tests
1 parent 002db8f commit 942cc4b

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/vs/platform/userDataSync/common/abstractSynchronizer.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,16 @@ export abstract class AbstractFileSynchroniser extends AbstractSynchroniser {
733733
}
734734
}
735735

736+
protected async deleteLocalFile(): Promise<void> {
737+
try {
738+
await this.fileService.del(this.file);
739+
} catch (e) {
740+
if (!(e instanceof FileOperationError && e.fileOperationResult === FileOperationResult.FILE_NOT_FOUND)) {
741+
throw e;
742+
}
743+
}
744+
}
745+
736746
private onFileChanges(e: FileChangesEvent): void {
737747
if (!e.contains(this.file)) {
738748
return;

src/vs/platform/userDataSync/common/tasksSync.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { AbstractFileSynchroniser, AbstractInitializer, IAcceptResult, IFileReso
1818
import { Change, IRemoteUserData, IUserDataSyncBackupStoreService, IUserDataSyncConfiguration, IUserDataSynchroniser, IUserDataSyncLogService, IUserDataSyncEnablementService, IUserDataSyncStoreService, SyncResource, USER_DATA_SYNC_SCHEME } from 'vs/platform/userDataSync/common/userDataSync';
1919

2020
interface ITasksSyncContent {
21-
tasks: string;
21+
tasks?: string;
2222
}
2323

2424
interface ITasksResourcePreview extends IFileResourcePreview {
@@ -28,7 +28,7 @@ interface ITasksResourcePreview extends IFileResourcePreview {
2828
export function getTasksContentFromSyncContent(syncContent: string, logService: ILogService): string | null {
2929
try {
3030
const parsed = <ITasksSyncContent>JSON.parse(syncContent);
31-
return parsed.tasks;
31+
return parsed.tasks ?? null;
3232
} catch (e) {
3333
logService.error(e);
3434
return null;
@@ -76,7 +76,7 @@ export class TasksSynchroniser extends AbstractFileSynchroniser implements IUser
7676
let hasRemoteChanged: boolean = false;
7777
let hasConflicts: boolean = false;
7878

79-
if (remoteContent) {
79+
if (remoteUserData.syncData) {
8080
const localContent = fileContent ? fileContent.value.toString() : null;
8181
if (!lastSyncContent // First time sync
8282
|| lastSyncContent !== localContent // Local has forwarded
@@ -196,13 +196,17 @@ export class TasksSynchroniser extends AbstractFileSynchroniser implements IUser
196196
if (fileContent) {
197197
await this.backupLocal(JSON.stringify(this.toTasksSyncContent(fileContent.value.toString())));
198198
}
199-
await this.updateLocalFileContent(content || '{}', fileContent, force);
199+
if (content) {
200+
await this.updateLocalFileContent(content, fileContent, force);
201+
} else {
202+
await this.deleteLocalFile();
203+
}
200204
this.logService.info(`${this.syncResourceLogLabel}: Updated local tasks`);
201205
}
202206

203207
if (remoteChange !== Change.None) {
204208
this.logService.trace(`${this.syncResourceLogLabel}: Updating remote tasks...`);
205-
const remoteContents = JSON.stringify(this.toTasksSyncContent(content || '{}'));
209+
const remoteContents = JSON.stringify(this.toTasksSyncContent(content));
206210
remoteUserData = await this.updateRemoteUserData(remoteContents, force ? null : remoteUserData.ref);
207211
this.logService.info(`${this.syncResourceLogLabel}: Updated remote tasks`);
208212
}
@@ -235,8 +239,8 @@ export class TasksSynchroniser extends AbstractFileSynchroniser implements IUser
235239
return null;
236240
}
237241

238-
private toTasksSyncContent(tasks: string): ITasksSyncContent {
239-
return { tasks };
242+
private toTasksSyncContent(tasks: string | null): ITasksSyncContent {
243+
return tasks ? { tasks } : {};
240244
}
241245

242246
}

src/vs/platform/userDataSync/test/common/tasksSync.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,34 @@ suite('TasksSync', () => {
444444
assert.strictEqual((await fileService.readFile(tasksResource)).value.toString(), content);
445445
});
446446

447+
test('when tasks file was removed in one client', async () => {
448+
const fileService = client.instantiationService.get(IFileService);
449+
const tasksResource = client.instantiationService.get(IUserDataProfilesService).defaultProfile.tasksResource;
450+
await fileService.writeFile(tasksResource, VSBuffer.fromString(JSON.stringify({
451+
'version': '2.0.0',
452+
'tasks': []
453+
})));
454+
await testObject.sync(await client.getResourceManifest());
455+
456+
const client2 = disposableStore.add(new UserDataSyncClient(server));
457+
await client2.setUp(true);
458+
await client2.sync();
459+
460+
const tasksResource2 = client2.instantiationService.get(IUserDataProfilesService).defaultProfile.tasksResource;
461+
const fileService2 = client2.instantiationService.get(IFileService);
462+
fileService2.del(tasksResource2);
463+
await client2.sync();
464+
465+
await testObject.sync(await client.getResourceManifest());
466+
467+
assert.deepStrictEqual(testObject.status, SyncStatus.Idle);
468+
const lastSyncUserData = await testObject.getLastSyncUserData();
469+
const remoteUserData = await testObject.getRemoteUserData(null);
470+
assert.strictEqual(getTasksContentFromSyncContent(lastSyncUserData!.syncData!.content!, client.instantiationService.get(ILogService)), null);
471+
assert.strictEqual(getTasksContentFromSyncContent(remoteUserData!.syncData!.content!, client.instantiationService.get(ILogService)), null);
472+
assert.strictEqual(await fileService.exists(tasksResource), false);
473+
});
474+
447475
test('when tasks file is created after first sync', async () => {
448476
const fileService = client.instantiationService.get(IFileService);
449477
const tasksResource = client.instantiationService.get(IUserDataProfilesService).defaultProfile.tasksResource;
@@ -491,7 +519,7 @@ suite('TasksSync', () => {
491519
assert.deepStrictEqual(server.requests, []);
492520
});
493521

494-
test('sync profile snippets', async () => {
522+
test('sync profile tasks', async () => {
495523
const client2 = disposableStore.add(new UserDataSyncClient(server));
496524
await client2.setUp(true);
497525
const profile = await client2.instantiationService.get(IUserDataProfilesService).createNamedProfile('profile1');

0 commit comments

Comments
 (0)