From 7adff5b2e2a97db6e1a7af9dbf6755de02e4750f Mon Sep 17 00:00:00 2001 From: Rich Chiodo Date: Tue, 9 Jun 2020 16:53:52 -0700 Subject: [PATCH 1/2] Fix the package json not being read on second run --- .../common/application/customEditorService.ts | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/client/common/application/customEditorService.ts b/src/client/common/application/customEditorService.ts index f6bcffca0f92..1ca0ed0f239b 100644 --- a/src/client/common/application/customEditorService.ts +++ b/src/client/common/application/customEditorService.ts @@ -20,10 +20,7 @@ export class CustomEditorService implements ICustomEditorService { @inject(IApplicationEnvironment) private readonly appEnvironment: IApplicationEnvironment, @inject(IFileSystem) private readonly fileSystem: IFileSystem ) { - // Double check the package json has the necessary entries for contributing a custom editor - if (this.useCustomEditorApi && !appEnvironment.packageJson.contributes?.customEditors) { - this.rewritePackageJson().catch((e) => traceError(`Error rewriting package json: `, e)); - } + this.verifyPackageJson().catch((e) => traceError(`Error rewriting package json: `, e)); } public registerCustomEditorProvider( @@ -48,12 +45,23 @@ export class CustomEditorService implements ICustomEditorService { } } - private async rewritePackageJson() { + private async verifyPackageJson(): Promise { + // Double check the package json has the necessary entries for contributing a custom editor. Note + // we have to actually read it because appEnvironment.packageJson is the webpacked version + const packageJson = JSON.parse(await this.fileSystem.readFile(path.join(EXTENSION_ROOT_DIR, 'package.json'))); + if (this.useCustomEditorApi && !packageJson.contributes?.customEditors) { + return this.addCustomEditors(packageJson); + } else if (!this.useCustomEditorApi && packageJson.contributes.customEditors) { + return this.removeCustomEditors(); + } + } + + // tslint:disable-next-line: no-any + private async addCustomEditors(currentPackageJson: any) { // tslint:disable-next-line:no-require-imports no-var-requires const _mergeWith = require('lodash/mergeWith') as typeof import('lodash/mergeWith'); - const current = this.appEnvironment.packageJson; const improvedContents = await this.fileSystem.readFile(path.join(EXTENSION_ROOT_DIR, 'customEditor.json')); - const improved = _mergeWith({ ...current }, JSON.parse(improvedContents), (l, r) => { + const improved = _mergeWith({ ...currentPackageJson }, JSON.parse(improvedContents), (l, r) => { if (Array.isArray(l) && Array.isArray(r)) { return [...l, ...r]; } @@ -64,4 +72,14 @@ export class CustomEditorService implements ICustomEditorService { ); this.commandManager.executeCommand('python.reloadVSCode', DataScience.reloadCustomEditor()); } + private async removeCustomEditors() { + // Note, to put it back, use the shipped version. This packageJson is required into the product + // so it's packed by webpack into the source. + const original = { ...this.appEnvironment.packageJson }; + await this.fileSystem.writeFile( + path.join(EXTENSION_ROOT_DIR, 'package.json'), + JSON.stringify(original, null, 4) + ); + this.commandManager.executeCommand('python.reloadVSCode', DataScience.reloadCustomEditor()); + } } From a755654478779bda330bafe8aec75496db8a3114 Mon Sep 17 00:00:00 2001 From: Rich Chiodo Date: Tue, 9 Jun 2020 16:55:57 -0700 Subject: [PATCH 2/2] Add news entry --- news/2 Fixes/12231.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/2 Fixes/12231.md diff --git a/news/2 Fixes/12231.md b/news/2 Fixes/12231.md new file mode 100644 index 000000000000..292d47e10a90 --- /dev/null +++ b/news/2 Fixes/12231.md @@ -0,0 +1 @@ +Infinite loop of asking to reload the extension when enabling custom editor. \ No newline at end of file