Skip to content

Commit

Permalink
fix #8230: ensure that schema resource exists before updating it
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Aug 11, 2020
1 parent 0a3846a commit 614120b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
8 changes: 6 additions & 2 deletions packages/debug/src/browser/debug-schema-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable, inject } from 'inversify';
import { injectable, inject, postConstruct } from 'inversify';
import { JsonSchemaRegisterContext, JsonSchemaContribution } from '@theia/core/lib/browser/json-schema-store';
import { InMemoryResources, deepClone } from '@theia/core/lib/common';
import { IJSONSchema } from '@theia/core/lib/common/json-schema';
Expand All @@ -31,8 +31,12 @@ export class DebugSchemaUpdater implements JsonSchemaContribution {
@inject(InMemoryResources) protected readonly inmemoryResources: InMemoryResources;
@inject(DebugService) protected readonly debug: DebugService;

registerSchemas(context: JsonSchemaRegisterContext): void {
@postConstruct()
protected init(): void {
this.inmemoryResources.add(this.uri, '');
}

registerSchemas(context: JsonSchemaRegisterContext): void {
context.registerSchema({
fileMatch: ['launch.json'],
url: this.uri.toString()
Expand Down
26 changes: 14 additions & 12 deletions packages/task/src/browser/task-schema-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import * as Ajv from 'ajv';
import debounce = require('p-debounce');
import { injectable, inject } from 'inversify';
import { postConstruct, injectable, inject } from 'inversify';
import { JsonSchemaContribution, JsonSchemaRegisterContext } from '@theia/core/lib/browser/json-schema-store';
import { InMemoryResources, deepClone, Emitter } from '@theia/core/lib/common';
import { IJSONSchema } from '@theia/core/lib/common/json-schema';
Expand Down Expand Up @@ -53,19 +53,16 @@ export class TaskSchemaUpdater implements JsonSchemaContribution {
protected readonly onDidChangeTaskSchemaEmitter = new Emitter<void>();
readonly onDidChangeTaskSchema = this.onDidChangeTaskSchemaEmitter.event;

registerSchemas(context: JsonSchemaRegisterContext): void {
const taskSchemaUri = new URI(taskSchemaId);
const resource = this.inmemoryResources.add(taskSchemaUri, '');
protected readonly uri = new URI(taskSchemaId);

@postConstruct()
protected init(): void {
const resource = this.inmemoryResources.add(this.uri, '');
if (resource.onDidChangeContents) {
resource.onDidChangeContents(() => {
this.onDidChangeTaskSchemaEmitter.fire(undefined);
});
}
context.registerSchema({
fileMatch: ['tasks.json', UserStorageUri.resolve('tasks.json').toString()],
url: taskSchemaUri.toString()
});

this.updateProblemMatcherNames();
this.updateSupportedTaskTypes();
// update problem matcher names in the task schema every time a problem matcher is added or disposed
Expand All @@ -75,16 +72,21 @@ export class TaskSchemaUpdater implements JsonSchemaContribution {
this.taskDefinitionRegistry.onDidUnregisterTaskDefinition(() => this.updateSupportedTaskTypes());
}

registerSchemas(context: JsonSchemaRegisterContext): void {
context.registerSchema({
fileMatch: ['tasks.json', UserStorageUri.resolve('tasks.json').toString()],
url: this.uri.toString()
});
}

readonly update = debounce(() => this.doUpdate(), 0);
protected doUpdate(): void {
const taskSchemaUri = new URI(taskSchemaId);

taskConfigurationSchema.anyOf = [processTaskConfigurationSchema, ...customizedDetectedTasks, ...customSchemas];

const schema = this.getTaskSchema();
this.doValidate = new Ajv().compile(schema);
const schemaContent = JSON.stringify(schema);
this.inmemoryResources.update(taskSchemaUri, schemaContent);
this.inmemoryResources.update(this.uri, schemaContent);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down

0 comments on commit 614120b

Please sign in to comment.