vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Support variables when resolving values in settings

Open OlsonDev opened this issue 10 years ago • 209 comments

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.path
  • markdown.styles
  • json.schemas
  • typescript.tsdk
  • php.validate.executablePath

OlsonDev avatar Feb 09 '16 01:02 OlsonDev

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 avatar Mar 04 '16 19:03 mjbvz

@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:

OlsonDev avatar Mar 05 '16 02:03 OlsonDev

Related: https://github.com/Microsoft/vscode/issues/3759

bpasero avatar Mar 07 '16 10:03 bpasero

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.

mjbvz avatar Mar 07 '16 20:03 mjbvz

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?

OlsonDev avatar Mar 08 '16 18:03 OlsonDev

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.

mjbvz avatar Mar 08 '16 19:03 mjbvz

@DonJayamanne is starting to look into this from https://github.com/Microsoft/vscode/pull/8555

bpasero avatar Jul 04 '16 06:07 bpasero

@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 avatar Aug 30 '16 21:08 pcgeek86

@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 avatar Aug 30 '16 22:08 DonJayamanne

@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 avatar Aug 31 '16 04:08 bpasero

@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.

DonJayamanne avatar Aug 31 '16 06:08 DonJayamanne

@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.

DonJayamanne avatar Aug 31 '16 07:08 DonJayamanne

@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).

DonJayamanne avatar Aug 31 '16 07:08 DonJayamanne

Great thanks 👍

@DonJayamanne did you put the right issue number in your comment?

bpasero avatar Aug 31 '16 08:08 bpasero

@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?

ramya-rao-a avatar Jan 31 '17 01:01 ramya-rao-a

Looking at this feature request, new configuration changes have nothing to do with this.

sandy081 avatar Jan 31 '17 11:01 sandy081

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?

ramya-rao-a avatar Jan 31 '17 19:01 ramya-rao-a

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?

ramya-rao-a avatar Feb 01 '17 06:02 ramya-rao-a

I am not aware of those refactorings. @bpasero ?

sandy081 avatar Feb 01 '17 06:02 sandy081

@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.

bpasero avatar Feb 01 '17 09:02 bpasero