Support variables when resolving values in settings
Hi,
I was just reading the latest updates and it says one can install typescript@next globally and then set typescript.tsdk so VS Code can use the appropriate version/installation. In a team environment, I'd like to put that setting in our project, something like:
.vscode/settings.json:
{
"typescript.tsdk": "%APPDATA%/npm/node_modules/typescript/lib"
}
The problem is restarting VS Code results in an error:
The path c:\Projects\Derp\%APPDATA%\npm\node_modules\typescript\lib doesn't point to a valid tsserver install. TypeScript language features will be disabled.
Now the setting needs to be per-person because I highly doubt my teammates have tsc installed in C:\Users\Olson\AppData\Roaming\npm\node_modules\typescript\lib :wink:
Could we get environment variables evaluated on that and all other settings that involve paths?
I haven't tested other paths, but I see these in the Default Settings:
git.pathmarkdown.stylesjson.schemastypescript.tsdkphp.validate.executablePath
I was looking into this a little, but it'd be helpful to understand what the behavior should look like on non windows systems. Mainly, should we use the same environment variable %SYNTAX% or try to emulate the host platform?
I'd argue that having VS Code be consistent across multiple systems is more important than constancy with the host platform, but I'm curious to see if anyone has any objections to that. We could use $SYNTAX or something else, across all platforms too.
@mjbvz I thought about this when I first opened the issue but neglected to mention it because I'm indifferent. However, I do agree consistency within VS Code is likely more beneficial than with the OS.
Visual Studio seems to use $(PATH) syntax. I think that would make the implementation easier because, ya know, having an end delimiter tells you exactly what to look up instead of having to filter down Object.keys(process.env) one character at a time.
I guess my vote goes toward $(PATH) syntax for consistency within Microsoft IDEs. :+1:
Related: https://github.com/Microsoft/vscode/issues/3759
Just looking through the code, this best approach seems to be using the existing SystemVariables (or extracting its functionality) class since SystemVariables::resolve takes a string and substitutes in environment variables for any ${VAR} occurrences (see AbstractSystemVariables::__resolveString).
Another approach may be to update newConfigFile() from platform/configuration/model.ts to resolve the environment variables when setting the config values, but this would not pick up changes to environment variables after the config is saved.
Another approach may be to update newConfigFile from platform/configuration/common.ts to resolve the environment variables when setting the config values.
You mean platform/configuration/common/model.ts, I assume?
Yes, thanks. I've updated the comment with the correct file name.
Resolving the environment variables only when they are actually needed would be best, so that always pick up the latest values.
@DonJayamanne is starting to look into this from https://github.com/Microsoft/vscode/pull/8555
@bpasero Given the latest comment in #8555, what should the expectation be for having the ability to specify environment variables within user and workspace JSON configurations?
@pcgeek86, this capability hasn't been added yet. The PR you mentioned was closed without merging the changes due to some other ongoing changes mentioned by @bpasero in his last comment
@DonJayamanne for your case (Python I think?), did you find a workaround to support this?
If we add variables support to settings, we need to think about validation. If a setting only allows for certain string values, putting in a variable would always result in warnings. I guess this problem already exists today for launch.json and task.json.
@bpasero, yes for Python extension i do have a work around, with support for $workspaceRoot and $env.. (env variables) in settings.json. Here's an example:
"python.pythonPath":"${workspaceRoot}/env/bin/python"
However, there's one problem.
The debugger can now reference settings defined in settings.json (via the following PR using the syntax ${config.python.pythonPath}).
Unfortunately the variable ${workspaceRoot} doesn't get resolved in the debugger. I cannot implement a work around either as the workspace Root path isn't available in the debugger either.
@bpasero , upon further analysis, I believe the bug is in configVariables. The problem is the debugger is getting the resolved value from settings.json, however the resolved value needs to be resolved once again to get the tokens (such as ${workspaceRoot}) replaced. I'll raise a separate issue for that.
@bpasero , I have created a new issue #11284 for the problem I have come across. Just so there's no confusion, I'm not waiting on this particular issue to be completed (as I have a workaround).
Great thanks 👍
@DonJayamanne did you put the right issue number in your comment?
@bpasero @sandy081 With all the refactoring in the configuration service in the past few months, any updates on where we stand on this feature ask of resolving env vars and/or variables like workspaceroot in the settings?
Looking at this feature request, new configuration changes have nothing to do with this.
I was referring to https://github.com/Microsoft/vscode/pull/8555#issuecomment-241667965 That was back in July/August. So based on the changes since then, any suggestions?
For example, we now have the configurationResolverService which can be used to resolve variables like ${workspaceRoot}, ${file} etc.
Why not use this service to resolve the settings as well?
I am not aware of those refactorings. @bpasero ?
@sandy081 this was added by @isidorn so I let him comment
the rationale was that the configuration service should always return the truth as it is on disk and not some resolved values. that is why we introduced a new service for this.