grafana / vscode-jsonnet

Full code support (formatting, highlighting, navigation, etc) for Jsonnet
Apache License 2.0
70 stars 9 forks source link

Support multi-root workspaces #24

Open davidovich opened 1 year ago

davidovich commented 1 year ago

Problem

When you use multiple folders in a VSCode workspace, the jpath is constructed with first workspace folder.

I have many jsonnet folders that respect a general hierearchy, and they are all in a named workspace.

# foreach folder of the workspace

     vendor/ # as produced by jsonnet bundler
     lib/ # local libraries
     build/ # some generated json files for import 

I set my jpath to [vendor, lib, build], but my libsonnet files are still not found, if I am not in the first folder.

Browsing through the code, I see that only the first project is used to deduce an absolute jpath. Also, this seems to happen on start of client, and on config reload. This seems like a global setting sent to the language server...

Expectation

I would expect hat the configured jpath is relative to the current folder of the current workspace and that the extension use the current context to find my files.

Notes

Maybe we could use the asRelativePath function of the workspace API, or getWorkspaceFolder to deduce the correct jpaths.

julienduchesne commented 1 year ago

Can you find imports if you enable the "resolve paths with Tanka" option? I don't have any jpaths set for any of my repositories

davidovich commented 1 year ago

Well, that improves it for folder relative lib and vendor. Nice. But build is still ignored. In the current project, I am on bare jsonnet, not tanka (unfortunately at the moment).

kflathers-chwy commented 8 months ago

@julienduchesne

Also running into this exact issue.

<my_workspace>/.vscode/settings.json

{
  "jsonnet.languageServer.jpath": [
    "/${workspaceFolder}/configs/foo/bar/"
  ],
  "jsonnet.languageServer.tankaMode": false,
}

and the resulting debug jsonnet language server output

level=info msg="configuration updated: {ResolvePathsWithTanka:true JPaths:[] ExtVars:map[] ExtCode:map[] FormattingOptions:{Indent:2 MaxBlankLines:2 StringStyle:1 CommentStyle:1 PrettyFieldNames:true PadArrays:false PadObjects:true SortImports:true UseImplicitPlus:true StripEverything:false StripComments:false StripAllButComments:false} EnableEvalDiagnostics:true EnableLintDiagnostics:false}"

ResolvePathsWithTanka and JPaths are both incorrect.

When I add these settings to my global user settings, it works, but the workspace settings completely fails.

kflathers-chwy commented 8 months ago

Adding to my about comment, appears like https://stackoverflow.com/questions/65192859/for-workspace-getconfiguration-how-do-i-get-a-setting-from-the-multi-root-works addresses how to fix this. When you access the configuration:

this:

const workspaceConfig = workspace.getConfiguration('jsonnet');

changes to:

const uri = vscode.window.activeTextEditor.document.uri;
const workspaceFolder = vscode.workspace.getWorkspaceFolder(uri);
const workspaceConfig = workspace.getConfiguration('jsonnet', workspaceFolder);

Have not tested this at all, and possibly a null check is needed or a try catch that reverts back to the global settings.