Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support relative paths for JSON schema associations (Fix #7140) #7225

Closed
wants to merge 3 commits into from

Conversation

ndoschek
Copy link
Contributor

@ndoschek ndoschek commented Feb 26, 2020

What it does

JSON preferences now also accept relative paths inside a theia workspace to associate JSON schemas to JSON files.
Absolute paths and URLs still work as expected.

Remark

As mentioned in #7140, I came across a minor problem, namely the jsonSchemaService (vscode-json-languageservice) returns the following warning:
Problems loading reference 'file:///test/my-schema-definition.json': Unable to load schema from '/test/my-schema-definition.json': ENOENT: no such file or directory, open '/test/my-schema-definition.json'.

The JSON schema support works as expected now also for relative paths, but this warning occurs in files that are associated with JSON schemas via relative paths.

How to test

  1. Set up a theia-workspace and include a plain JSON file and a JSON schema file,
    e.g. /testing.json and /.schemas/json-schema.json
  2. Define a schema association for those JSON files in your settings,
    e.g.
"json.schemas": [
  {
    "fileMatch": [
      "/testing.json"
    ],
    "url": "/.schemas/json-schema.json"
  }
]
  1. JSON schema support for the matched file is supported for associations via relative workspace paths.

Review checklist

Reminder for reviewers

Signed-off-by: Nina Doschek ndoschek@eclipsesource.com

@ndoschek
Copy link
Contributor Author

@akosyakov
As mentioned in #7140 I opened this PR with my proposed changes.

@vince-fugnitto vince-fugnitto added the json issues related to the json language label Feb 26, 2020
@akosyakov
Copy link
Member

/.schemas/json-schema.json that's an absolute path, have you tried with ./.schemas/json-schema.json or .schemas/json-schema.json?

@ndoschek
Copy link
Contributor Author

/.schemas/json-schema.json that's an absolute path, have you tried with ./.schemas/json-schema.json or .schemas/json-schema.json?

Yes, those cases are also covered.

const fileUri = new URI(schemaConfig.url);
if (fileUri.scheme === 'file') {
const filePath = fileUri.path.toString();
const workspaceRootPath = this.workspace.rootPath;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not handle multi root case. We should fetch json.schemas scoped by resource URI from each root and then apply such schemas only in the scope of a corresponding root.

@ndoschek
Copy link
Contributor Author

ndoschek commented Apr 8, 2020

Thank you for your review!
I adjusted the behavior accordingly and tested with different workspaces, as well as User, Workspace and Folder settings.

Still, I have the problem (as described earlier in the first comment, section 'Remark') with loading the reference for a JSON schema that was defined with an absolute path. But, this problem only seems to exist in a single-root-workspace.
Do you have any idea on this?

@ndoschek ndoschek requested a review from akosyakov April 8, 2020 17:44
@akosyakov
Copy link
Member

An error seems to come from here: https://github.com/microsoft/vscode-json-languageservice/blob/2ea5ad3d2ffbbe40dea11cfe764a502becf113ce/src/services/jsonSchemaService.ts#L437-L452

Maybe you can debug it somehow or read through the code assuming your path.

@ndoschek ndoschek force-pushed the fix7140 branch 2 times, most recently from f5547ef to f134faf Compare April 29, 2020 10:59
@ndoschek
Copy link
Contributor Author

I have tracked down the problem by debugging the vscode json language server. It listens additionally to changes of the workspace settings, and hence there the relative paths were not resolved properly.
I revised my earlier code and tested different scenarios (e.g. single and multi root workspaces absolute, relative paths, URLs and so on) and am glad to hear your opinion on this.

 Signed-off-by: Nina Doschek <ndoschek@eclipsesource.com>
Signed-off-by: Nina Doschek <ndoschek@eclipsesource.com>
Signed-off-by: Nina Doschek <ndoschek@eclipsesource.com>
Rebase on master
@ndoschek
Copy link
Contributor Author

Hi @akosyakov !
I rebased my change to master and tested the functionality also with the new preference editor.
Do you have time to take a look at it?
Thanks in advance!

@akosyakov
Copy link
Member

@ndoschek sorry that it takes so long, I'm looking at it this week.

Copy link
Member

@akosyakov akosyakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is the right direction but resolutions should happen via Language Client middleware as in VS Code json extension: https://github.com/microsoft/vscode/blob/45aa0bf525b08d7e23c9b10ca65f1de55949e601/extensions/json-language-features/client/src/jsonMain.ts#L137-L140 You basically can copy code from there.

Before you continue working on it: we are going to delete this extension by mid of July and use VS Code extension instead which will automatically resolve the issue.


resolve<T>(preferenceName: string, resourceUri?: string): PreferenceResolveResult<T> {
// resolve relative paths for json schema settings in workspace scope
if (preferenceName === 'json.schemas' && this.getScope() === PreferenceScope.Workspace) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be done by clients? It does not look good to introduce handling of individual preferences into the preference service logic.

@@ -119,6 +149,24 @@ export class JsonClientContribution extends BaseLanguageClientContribution {
}
}

protected initializeJsonPreferencesAssociations(): void {
const userSettings = this.preferenceService.inspect<JsonSchemaConfiguration[]>('json.schemas');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit confused why we need to worry about json.schemas. We send complete json configuration to JSON language server whenever it is changed. I don't think we need to pass it additionally as part of json/schemaAssociations.

@ndoschek
Copy link
Contributor Author

ndoschek commented Jun 5, 2020

Thank you for your answer! Do I understand you correctly, that the Theia json extension is being replaced by the vscode json extension in July?
So I will close this PR for now and see if the issues with resolving relative schema paths in single root workspaces are resolved then.
Thank you anyway for your support! :)

@akosyakov
Copy link
Member

Thank you for your answer! Do I understand you correctly, that the Theia json extension is being replaced by the vscode json extension in July?

Yes, we plan to deliver it latest mid July.

@ndoschek ndoschek closed this Jun 5, 2020
@ndoschek ndoschek deleted the fix7140 branch July 13, 2020 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
json issues related to the json language
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants