diff --git a/src/vs/workbench/contrib/sessionSync/browser/sessionSync.contribution.ts b/src/vs/workbench/contrib/sessionSync/browser/sessionSync.contribution.ts index 3c102115db848..d14c6922b0d80 100644 --- a/src/vs/workbench/contrib/sessionSync/browser/sessionSync.contribution.ts +++ b/src/vs/workbench/contrib/sessionSync/browser/sessionSync.contribution.ts @@ -157,7 +157,7 @@ export class SessionSyncContribution extends Disposable implements IWorkbenchCon if (uri === undefined) { return; } // Run the store action to get back a ref - const ref = await that.storeEditSession(); + const ref = await that.storeEditSession(false); // Append the ref to the URI if (ref !== undefined) { @@ -215,7 +215,7 @@ export class SessionSyncContribution extends Disposable implements IWorkbenchCon await that.progressService.withProgress({ location: ProgressLocation.Notification, title: localize('storing edit session', 'Storing edit session...') - }, async () => await that.storeEditSession()); + }, async () => await that.storeEditSession(true)); } })); } @@ -293,8 +293,9 @@ export class SessionSyncContribution extends Disposable implements IWorkbenchCon } } - async storeEditSession(): Promise { + async storeEditSession(fromStoreCommand: boolean): Promise { const folders: Folder[] = []; + let hasEdits = false; for (const repository of this.scmService.repositories) { // Look through all resource groups and compute which files were added/modified/deleted @@ -321,6 +322,8 @@ export class SessionSyncContribution extends Disposable implements IWorkbenchCon } } catch { } + hasEdits = true; + if (await this.fileService.exists(uri)) { workingChanges.push({ type: ChangeType.Addition, fileType: FileType.File, contents: (await this.fileService.readFile(uri)).value.toString(), relativeFilePath: relativeFilePath }); } else { @@ -332,6 +335,14 @@ export class SessionSyncContribution extends Disposable implements IWorkbenchCon folders.push({ workingChanges, name: name ?? '' }); } + if (!hasEdits) { + this.logService.info('Edit Sessions: Skipping storing edit session as there are no edits to store.'); + if (fromStoreCommand) { + this.notificationService.info(localize('no edits to store', 'Skipped storing edit session as there are no edits to store.')); + } + return undefined; + } + const data: EditSession = { folders, version: 1 }; try { diff --git a/src/vs/workbench/contrib/sessionSync/test/browser/sessionSync.test.ts b/src/vs/workbench/contrib/sessionSync/test/browser/sessionSync.test.ts index cdb412c50607c..5c29394116b6c 100644 --- a/src/vs/workbench/contrib/sessionSync/test/browser/sessionSync.test.ts +++ b/src/vs/workbench/contrib/sessionSync/test/browser/sessionSync.test.ts @@ -36,10 +36,13 @@ suite('Edit session sync', () => { let instantiationService: TestInstantiationService; let sessionSyncContribution: SessionSyncContribution; let fileService: FileService; + let sandbox: sinon.SinonSandbox; const disposables = new DisposableStore(); - setup(() => { + suiteSetup(() => { + sandbox = sinon.createSandbox(); + instantiationService = new TestInstantiationService(); // Set up filesystem @@ -71,6 +74,9 @@ suite('Edit session sync', () => { } }); + // Stub repositories + instantiationService.stub(ISCMService, '_repositories', new Map()); + sessionSyncContribution = instantiationService.createInstance(SessionSyncContribution); }); @@ -100,13 +106,9 @@ suite('Edit session sync', () => { }; // Stub sync service to return edit session data - const sandbox = sinon.createSandbox(); const readStub = sandbox.stub().returns({ editSession, ref: '0' }); instantiationService.stub(ISessionSyncWorkbenchService, 'read', readStub); - // Stub repositories - instantiationService.stub(ISCMService, '_repositories', new Map()); - // Create root folder await fileService.createFolder(folderUri); @@ -116,4 +118,17 @@ suite('Edit session sync', () => { // Verify edit session was correctly applied assert.equal((await fileService.readFile(fileUri)).value.toString(), fileContents); }); + + test('Edit session not stored if there are no edits', async function () { + const writeStub = sandbox.stub(); + instantiationService.stub(ISessionSyncWorkbenchService, 'write', writeStub); + + // Create root folder + await fileService.createFolder(folderUri); + + await sessionSyncContribution.storeEditSession(true); + + // Verify that we did not attempt to write the edit session + assert.equal(writeStub.called, false); + }); });