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

410 LOC · 302 covered · 108 uncovered · 61 ranges · 585 concepts · 14 introducers · 348 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- abstractSynchronizer.ts ×49
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { distinct } from '../../../base/common/arrays.js';
7 > import { VSBuffer } from '../../../base/common/buffer.js';
8 > import { CancellationToken } from '../../../base/common/cancellation.js';
9 > import { Event } from '../../../base/common/event.js';
10 > import { URI } from '../../../base/common/uri.js';
11 > import { localize } from '../../../nls.js';
12 > import { ConfigurationTarget, IConfigurationService } from '../../configuration/common/configuration.js';
13 > import { ConfigurationModelParser } from '../../configuration/common/configurationModels.js';
14 > import { IEnvironmentService } from '../../environment/common/environment.js';
15 > import { IExtensionManagementService } from '../../extensionManagement/common/extensionManagement.js';
16 > import { ExtensionType } from '../../extensions/common/extensions.js';
17 > import { FileOperationError, FileOperationResult, IFileService } from '../../files/common/files.js';
18 > import { IStorageService } from '../../storage/common/storage.js';
19 > import { ITelemetryService } from '../../telemetry/common/telemetry.js';
20 > import { IUriIdentityService } from '../../uriIdentity/common/uriIdentity.js';
21 > import { IUserDataProfile, IUserDataProfilesService } from '../../userDataProfile/common/userDataProfile.js';
22 > import { AbstractInitializer, AbstractJsonFileSynchroniser, IAcceptResult, IFileResourcePreview, IMergeResult } from './abstractSynchronizer.js';
23 > import { getIgnoredSettings, isEmpty, merge, updateIgnoredSettings } from './settingsMerge.js';
24 > import { Change, IRemoteUserData, IUserDataSyncLocalStoreService, IUserDataSyncConfiguration, IUserDataSynchroniser, IUserDataSyncLogService, IUserDataSyncEnablementService, IUserDataSyncStoreService, IUserDataSyncUtilService, SyncResource, UserDataSyncError, UserDataSyncErrorCode, USER_DATA_SYNC_CONFIGURATION_SCOPE, USER_DATA_SYNC_SCHEME, getIgnoredSettingsForExtension, IUserData } from './userDataSync.js';
25 >
26 > interface ISettingsResourcePreview extends IFileResourcePreview {
27 > previewResult: IMergeResult;
28 > }
29 >
30 > export interface ISettingsSyncContent {
31 > settings: string;
32 > }
33 >
34 > function isSettingsSyncContent(thing: any): thing is ISettingsSyncContent { settingsSync.ts ×2
35 > return thing
36 > && (thing.settings && typeof thing.settings === 'string')
37 > && Object.keys(thing).length === 1;
38 > }
40 > export function parseSettingsSyncContent(syncContent: string): ISettingsSyncContent {
41 > const parsed = <ISettingsSyncContent>JSON.parse(syncContent); settingsSync.ts ×2
42 > return isSettingsSyncContent(parsed) ? parsed : /* migrate */ { settings: syncContent };
43 > }
45 > export class SettingsSynchroniser extends AbstractJsonFileSynchroniser implements IUserDataSynchroniser {
46 >
47 > /* Version 2: Change settings from `sync.${setting}` to `settingsSync.{setting}` */
48 > protected readonly version: number = 2;
49 > readonly previewResource: URI = this.extUri.joinPath(this.syncPreviewFolder, 'settings.json');
50 > readonly baseResource: URI = this.previewResource.with({ scheme: USER_DATA_SYNC_SCHEME, authority: 'base' });
51 > readonly localResource: URI = this.previewResource.with({ scheme: USER_DATA_SYNC_SCHEME, authority: 'local' });
52 > readonly remoteResource: URI = this.previewResource.with({ scheme: USER_DATA_SYNC_SCHEME, authority: 'remote' });
53 > readonly acceptedResource: URI = this.previewResource.with({ scheme: USER_DATA_SYNC_SCHEME, authority: 'accepted' });
54 >
55 > constructor(
56 > private readonly profile: IUserDataProfile, settingsSync.ts ×13
57 > collection: string | undefined,
58 > @IFileService fileService: IFileService,
59 > @IEnvironmentService environmentService: IEnvironmentService,
60 > @IStorageService storageService: IStorageService,
61 > @IUserDataSyncStoreService userDataSyncStoreService: IUserDataSyncStoreService,
62 > @IUserDataSyncLocalStoreService userDataSyncLocalStoreService: IUserDataSyncLocalStoreService,
63 > @IUserDataSyncLogService logService: IUserDataSyncLogService,
64 > @IUserDataSyncUtilService userDataSyncUtilService: IUserDataSyncUtilService,
65 > @IConfigurationService configurationService: IConfigurationService,
66 > @IUserDataSyncEnablementService userDataSyncEnablementService: IUserDataSyncEnablementService,
67 > @ITelemetryService telemetryService: ITelemetryService,
68 > @IExtensionManagementService private readonly extensionManagementService: IExtensionManagementService,
69 > @IUriIdentityService uriIdentityService: IUriIdentityService,
70 > ) {
71 > super(profile.settingsResource, { syncResource: SyncResource.Settings, profile }, collection, fileService, environmentService, storageService, userDataSyncStoreService, userDataSyncLocalStoreService, userDataSyncEnablementService, telemetryService, logService, userDataSyncUtilService, configurationService, uriIdentityService);
72 > }
73 >
74 > async getRemoteUserDataSyncConfiguration(refOrLatestData: string | IUserData | null): Promise<IUserDataSyncConfiguration> {
75 const lastSyncUserData = await this.getLastSyncUserData();
76 const remoteUserData = await this.getLatestRemoteUserData(refOrLatestData, lastSyncUserData);
77 const remoteSettingsSyncContent = this.getSettingsSyncContent(remoteUserData);
78 const parser = new ConfigurationModelParser(USER_DATA_SYNC_CONFIGURATION_SCOPE, this.logService);
79 if (remoteSettingsSyncContent?.settings) {
80 parser.parse(remoteSettingsSyncContent.settings);
81 }
82 return parser.configurationModel.getValue(USER_DATA_SYNC_CONFIGURATION_SCOPE) || {};
83 }
85 > protected async generateSyncPreview(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, isRemoteDataFromCurrentMachine: boolean): Promise<ISettingsResourcePreview[]> {
86 > const fileContent = await this.getLocalFileContent(); settingsSync.ts ×10
87 > const formattingOptions = await this.getFormattingOptions();
88 > const remoteSettingsSyncContent = this.getSettingsSyncContent(remoteUserData);
89 >
90 > // Use remote data as last sync data if last sync data does not exist and remote data is from same machine
91 > lastSyncUserData = lastSyncUserData === null && isRemoteDataFromCurrentMachine ? remoteUserData : lastSyncUserData;
92 > const lastSettingsSyncContent: ISettingsSyncContent | null = lastSyncUserData ? this.getSettingsSyncContent(lastSyncUserData) : null;
93 > const ignoredSettings = await this.getIgnoredSettings();
94 >
95 > let mergedContent: string | null = null;
96 > let hasLocalChanged: boolean = false;
97 > let hasRemoteChanged: boolean = false;
98 > let hasConflicts: boolean = false;
99 >
100 > if (remoteSettingsSyncContent) {
101 > let localContent: string = fileContent ? fileContent.value.toString().trim() : '{}'; settingsSync.ts ×1
102 > localContent = localContent || '{}';
103 > this.validateContent(localContent);
104 > this.logService.trace(`${this.syncResourceLogLabel}: Merging remote settings with local settings...`);
105 > const result = merge(localContent, remoteSettingsSyncContent.settings, lastSettingsSyncContent ? lastSettingsSyncContent.settings : null, ignoredSettings, [], formattingOptions);
106 > mergedContent = result.localContent || result.remoteContent;
107 > hasLocalChanged = result.localContent !== null;
108 > hasRemoteChanged = result.remoteContent !== null;
109 > hasConflicts = result.hasConflicts;
110 > }
112 > // First time syncing to remote
113 > else if (fileContent) {
114 > this.logService.trace(`${this.syncResourceLogLabel}: Remote settings does not exist. Synchronizing settings for the first time.`); settingsSync.ts ×1
115 > mergedContent = fileContent.value.toString().trim() || '{}';
116 > this.validateContent(mergedContent);
117 > hasRemoteChanged = true;
118 > }
120 > const localContent = fileContent ? fileContent.value.toString() : null; settingsSync.ts ×10
121 > const baseContent = lastSettingsSyncContent?.settings ?? null;
122 >
123 > const previewResult = {
124 > content: hasConflicts ? baseContent : mergedContent,
125 > localChange: hasLocalChanged ? Change.Modified : Change.None,
126 > remoteChange: hasRemoteChanged ? Change.Modified : Change.None,
127 > hasConflicts
128 > };
129 >
130 > return [{
131 > fileContent,
132 >
133 > baseResource: this.baseResource,
134 > baseContent,
135 >
136 > localResource: this.localResource,
137 > localContent,
138 > localChange: previewResult.localChange,
139 >
140 > remoteResource: this.remoteResource,
141 > remoteContent: remoteSettingsSyncContent ? remoteSettingsSyncContent.settings : null,
142 > remoteChange: previewResult.remoteChange,
143 >
144 > previewResource: this.previewResource,
145 > previewResult,
146 > acceptedResource: this.acceptedResource,
147 > }];
148 > }
150 > protected async hasRemoteChanged(lastSyncUserData: IRemoteUserData): Promise<boolean> {
151 > const lastSettingsSyncContent: ISettingsSyncContent | null = this.getSettingsSyncContent(lastSyncUserData); settingsSync.ts ×2
152 > if (lastSettingsSyncContent === null) {
153 return true;
154 }
156 > const fileContent = await this.getLocalFileContent();
157 > const localContent: string = fileContent ? fileContent.value.toString().trim() : '';
158 > const ignoredSettings = await this.getIgnoredSettings();
159 > const formattingOptions = await this.getFormattingOptions();
160 > const result = merge(localContent || '{}', lastSettingsSyncContent.settings, lastSettingsSyncContent.settings, ignoredSettings, [], formattingOptions);
161 > return result.remoteContent !== null;
162 > }
164 > protected async getMergeResult(resourcePreview: ISettingsResourcePreview, token: CancellationToken): Promise<IMergeResult> {
165 > const formatUtils = await this.getFormattingOptions(); settingsSync.ts ×8
166 > const ignoredSettings = await this.getIgnoredSettings();
167 > return {
168 > ...resourcePreview.previewResult,
169 >
170 > // remove ignored settings from the preview content
171 > content: resourcePreview.previewResult.content ? updateIgnoredSettings(resourcePreview.previewResult.content, '{}', ignoredSettings, formatUtils) : null
172 > };
173 > }
175 > protected async getAcceptResult(resourcePreview: ISettingsResourcePreview, resource: URI, content: string | null | undefined, token: CancellationToken): Promise<IAcceptResult> {
177 > const formattingOptions = await this.getFormattingOptions();
178 > const ignoredSettings = await this.getIgnoredSettings();
179 >
180 > /* Accept local resource */
181 > if (this.extUri.isEqual(resource, this.localResource)) {
182 return {
183 /* Remove ignored settings */
184 content: resourcePreview.fileContent ? updateIgnoredSettings(resourcePreview.fileContent.value.toString(), '{}', ignoredSettings, formattingOptions) : null,
185 localChange: Change.None,
186 remoteChange: Change.Modified,
187 };
188 }
190 > /* Accept remote resource */
191 > if (this.extUri.isEqual(resource, this.remoteResource)) {
192 return {
193 /* Update ignored settings from local file content */
194 content: resourcePreview.remoteContent !== null ? updateIgnoredSettings(resourcePreview.remoteContent, resourcePreview.fileContent ? resourcePreview.fileContent.value.toString() : '{}', ignoredSettings, formattingOptions) : null,
195 localChange: Change.Modified,
196 remoteChange: Change.None,
197 };
198 }
200 > /* Accept preview resource */
201 > if (this.extUri.isEqual(resource, this.previewResource)) {
202 > if (content === undefined) {
203 > return {
204 > content: resourcePreview.previewResult.content,
205 > localChange: resourcePreview.previewResult.localChange,
206 > remoteChange: resourcePreview.previewResult.remoteChange,
207 > };
208 > } else {
209 return {
210 /* Add ignored settings from local file content */
211 content: content !== null ? updateIgnoredSettings(content, resourcePreview.fileContent ? resourcePreview.fileContent.value.toString() : '{}', ignoredSettings, formattingOptions) : null,
212 localChange: Change.Modified,
213 remoteChange: Change.Modified,
214 };
215 }
217
218 throw new Error(`Invalid Resource: ${resource.toString()}`);
221 > protected async applyResult(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, resourcePreviews: [ISettingsResourcePreview, IAcceptResult][], force: boolean): Promise<void> {
222 > const { fileContent } = resourcePreviews[0][0]; settingsSync.ts ×5
223 > let { content, localChange, remoteChange } = resourcePreviews[0][1];
224 >
225 > if (localChange === Change.None && remoteChange === Change.None) {
226 > this.logService.info(`${this.syncResourceLogLabel}: No changes found during synchronizing settings.`); settingsSync.ts ×1
227 > }
229 > content = content ? content.trim() : '{}';
230 > content = content || '{}';
231 > this.validateContent(content);
232 >
233 > if (localChange !== Change.None) {
234 > this.logService.trace(`${this.syncResourceLogLabel}: Updating local settings...`); settingsSync.ts ×2
235 > if (fileContent) {
236 > await this.backupLocal(JSON.stringify(this.toSettingsSyncContent(fileContent.value.toString()))); settingsSync.ts ×1
237 > }
238 > await this.updateLocalFileContent(content, fileContent, force); settingsSync.ts ×2
239 > await this.configurationService.reloadConfiguration(ConfigurationTarget.USER_LOCAL);
240 > this.logService.info(`${this.syncResourceLogLabel}: Updated local settings`);
241 > }
243 > if (remoteChange !== Change.None) {
244 > const formatUtils = await this.getFormattingOptions(); settingsSync.ts ×8
245 > // Update ignored settings from remote
246 > const remoteSettingsSyncContent = this.getSettingsSyncContent(remoteUserData);
247 > const ignoredSettings = await this.getIgnoredSettings(content);
248 > content = updateIgnoredSettings(content, remoteSettingsSyncContent ? remoteSettingsSyncContent.settings : '{}', ignoredSettings, formatUtils);
249 > this.logService.trace(`${this.syncResourceLogLabel}: Updating remote settings...`);
250 > remoteUserData = await this.updateRemoteUserData(JSON.stringify(this.toSettingsSyncContent(content)), force ? null : remoteUserData.ref);
251 > this.logService.info(`${this.syncResourceLogLabel}: Updated remote settings`);
252 > }
254 > // Delete the preview
255 > try {
256 > await this.fileService.del(this.previewResource);
257 > } catch (e) { /* ignore */ }
258 >
259 > if (lastSyncUserData?.ref !== remoteUserData.ref) {
260 > this.logService.trace(`${this.syncResourceLogLabel}: Updating last synchronized settings...`);
261 > await this.updateLastSyncUserData(remoteUserData);
262 > this.logService.info(`${this.syncResourceLogLabel}: Updated last synchronized settings`);
263 > }
264 >
265 > }
267 > async hasLocalData(): Promise<boolean> {
268 try {
269 const localFileContent = await this.getLocalFileContent();
270 if (localFileContent) {
271 return !isEmpty(localFileContent.value.toString());
272 }
273 } catch (error) {
274 if ((<FileOperationError>error).fileOperationResult !== FileOperationResult.FILE_NOT_FOUND) {
275 return true;
276 }
277 }
278 return false;
279 }
281 > async resolveContent(uri: URI): Promise<string | null> {
282 if (this.extUri.isEqual(this.remoteResource, uri)
283 || this.extUri.isEqual(this.localResource, uri)
284 || this.extUri.isEqual(this.acceptedResource, uri)
285 || this.extUri.isEqual(this.baseResource, uri)
286 ) {
287 return this.resolvePreviewContent(uri);
288 }
289 return null;
290 }
292 > protected override async resolvePreviewContent(resource: URI): Promise<string | null> {
293 let content = await super.resolvePreviewContent(resource);
294 if (content) {
295 const formatUtils = await this.getFormattingOptions();
296 // remove ignored settings from the preview content
297 const ignoredSettings = await this.getIgnoredSettings();
298 content = updateIgnoredSettings(content, '{}', ignoredSettings, formatUtils);
299 }
300 return content;
301 }
303 > private getSettingsSyncContent(remoteUserData: IRemoteUserData): ISettingsSyncContent | null {
304 > return remoteUserData.syncData ? this.parseSettingsSyncContent(remoteUserData.syncData.content) : null; settingsSync.ts ×10
305 > }
307 > private parseSettingsSyncContent(syncContent: string): ISettingsSyncContent | null {
308 > try { settingsSync.ts ×2
309 > return parseSettingsSyncContent(syncContent);
310 > } catch (e) {
311 this.logService.error(e);
312 }
313 return null;
316 > private toSettingsSyncContent(settings: string): ISettingsSyncContent {
317 > return { settings }; settingsSync.ts ×8
318 > }
320 > private coreIgnoredSettings: Promise<string[]> | undefined = undefined;
321 > private systemExtensionsIgnoredSettings: Promise<string[]> | undefined = undefined;
322 > private userExtensionsIgnoredSettings: Promise<string[]> | undefined = undefined;
323 > private async getIgnoredSettings(content?: string): Promise<string[]> { abstractSynchronizer.ts ×49
324 > if (!this.coreIgnoredSettings) { settingsSync.ts ×10
325 > this.coreIgnoredSettings = this.userDataSyncUtilService.resolveDefaultCoreIgnoredSettings();
326 > }
327 > if (!this.systemExtensionsIgnoredSettings) {
328 > this.systemExtensionsIgnoredSettings = this.getIgnoredSettingForSystemExtensions();
329 > }
330 > if (!this.userExtensionsIgnoredSettings) {
331 > this.userExtensionsIgnoredSettings = this.getIgnoredSettingForUserExtensions();
332 > const disposable = this._register(Event.any<any>(
333 > Event.filter(this.extensionManagementService.onDidInstallExtensions, (e => e.some(({ local }) => !!local))),
334 > Event.filter(this.extensionManagementService.onDidUninstallExtension, (e => !e.error)))(() => {
335 disposable.dispose();
336 this.userExtensionsIgnoredSettings = undefined;
338 > }
339 > const defaultIgnoredSettings = (await Promise.all([this.coreIgnoredSettings, this.systemExtensionsIgnoredSettings, this.userExtensionsIgnoredSettings])).flat();
340 > return getIgnoredSettings(defaultIgnoredSettings, this.configurationService, content);
341 > }
343 > private async getIgnoredSettingForSystemExtensions(): Promise<string[]> {
344 > const systemExtensions = await this.extensionManagementService.getInstalled(ExtensionType.System); settingsSync.ts ×10
345 > return distinct(systemExtensions.map(e => getIgnoredSettingsForExtension(e.manifest)).flat());
346 > }
348 > private async getIgnoredSettingForUserExtensions(): Promise<string[]> {
349 > const userExtensions = await this.extensionManagementService.getInstalled(ExtensionType.User, this.profile.extensionsResource); settingsSync.ts ×10
350 > return distinct(userExtensions.map(e => getIgnoredSettingsForExtension(e.manifest)).flat());
351 > }
353 > private validateContent(content: string): void {
354 > if (this.hasErrors(content, false)) { settingsSync.ts ×10
355 > throw new UserDataSyncError(localize('errorInvalidSettings', "Unable to sync settings as there are errors/warning in settings file."), UserDataSyncErrorCode.LocalInvalidContent, this.resource); settingsSync.ts ×1
356 > }
359 > }
360 >
361 > export class SettingsInitializer extends AbstractInitializer {
362 >
363 > constructor(
364 @IFileService fileService: IFileService,
365 @IUserDataProfilesService userDataProfilesService: IUserDataProfilesService,
366 @IEnvironmentService environmentService: IEnvironmentService,
367 @IUserDataSyncLogService logService: IUserDataSyncLogService,
368 @IStorageService storageService: IStorageService,
369 @IUriIdentityService uriIdentityService: IUriIdentityService,
370 ) {
371 super(SyncResource.Settings, userDataProfilesService, environmentService, logService, fileService, storageService, uriIdentityService);
372 }
374 > protected async doInitialize(remoteUserData: IRemoteUserData): Promise<void> {
375 const settingsSyncContent = remoteUserData.syncData ? this.parseSettingsSyncContent(remoteUserData.syncData.content) : null;
376 if (!settingsSyncContent) {
377 this.logService.info('Skipping initializing settings because remote settings does not exist.');
378 return;
379 }
380
381 const isEmpty = await this.isEmpty();
382 if (!isEmpty) {
383 this.logService.info('Skipping initializing settings because local settings exist.');
384 return;
385 }
386
387 await this.fileService.writeFile(this.userDataProfilesService.defaultProfile.settingsResource, VSBuffer.fromString(settingsSyncContent.settings));
388
389 await this.updateLastSyncUserData(remoteUserData);
390 }
392 > private async isEmpty(): Promise<boolean> {
393 try {
394 const fileContent = await this.fileService.readFile(this.userDataProfilesService.defaultProfile.settingsResource);
395 return isEmpty(fileContent.value.toString().trim());
396 } catch (error) {
397 return (<FileOperationError>error).fileOperationResult === FileOperationResult.FILE_NOT_FOUND;
398 }
399 }
401 > private parseSettingsSyncContent(syncContent: string): ISettingsSyncContent | null {
402 try {
403 return parseSettingsSyncContent(syncContent);
404 } catch (e) {
405 this.logService.error(e);
406 }
407 return null;
408 }
410 > }