In general with VS Code, it's a pain that variable support is so patchy.
My expectation is that any string value in any VS Code .json file should respect any variable substitution doc'd in https://code.visualstudio.com/docs/editor/variables-reference - but actually, it's a matter for the developers of any feature whether or not they choose to implement variable expansion and, if so, how much of it.
That much is clearly an issue with VS Code, not Remote. And clearly, there's context of which I know not.
Given that Code is patchy in this regard, could Remote offer fuller support?
The behaviour I'd expect as a new user would be for variable expansion to be done on any string values but not keys. Any leaf node that's a string.
I believe from the issues that this would make sense to a lot of people.
Examples:
remoteUser does not expand ${env:USERNAME}. (workaround: "runArgs": ["--user", "${env:USERNAME}"])initializeCommand expands env but not config, so I can't use this: "initializeCommand": ["bash", "-c", ".devcontainer/initializeCommand.sh ${env:STACKNAME} ${config:docker.host}"] (logs show it's trying to run .devcontainer/initializeCommand.sh fsackur ${config:docker.host}). Therefore I have to hard-code a docker host or set it in whatever launches VS Code.Breaking cases:
if you intend to send a literal string with a colon, like '${env:STACKNAME}' or '${config:docker.host}'. I'm not aware of a reason to do that, but it does currently work without escaping.
To mitigate that, I suggest an extension setting key "greedyVariableInterpolation" that defaults to True to cater for new users.
USERNAME appears to be unset somewhere. remoteUser does expand other environment variables.
One note, most of these variables will not work in the context of devcontainer.json since they only apply once VS Code is actually up and running. In addition, many like env are ambiguous. Here's the list of variables we could add and which already exist.
| Variable | devcontainer.json | Exists |
|----------|---------------------|-------|
| workspaceFolder | localWorkspaceFolder | โ|
| workspaceFolder | containerWorkspaceFolder | โ|
| workspaceFolderBasename | localWorkspaceFolderBasename | โ|
| workspaceFolderBasename | containerWorkspaceFolderBasename | โ|
| env | localEnv | โ|
| env | containerEnv (cannot be used from containerEnv property, but can be used in remoteEnv) | โ |
| execPath | localExecPath | |
| execPath | containerExecPath | |
| config | config | |
Adding containerWorkspaceFolder, localWorkspaceFolderBasename and containerWorkspaceFolderBasename with #2473. (Updating the above table.)
As an update, specific supported properties are documented here to clarify what is expected to work: https://code.visualstudio.com/docs/remote/devcontainerjson-reference#_variables-in-devcontainerjson
Most helpful comment
One note, most of these variables will not work in the context of
devcontainer.jsonsince they only apply once VS Code is actually up and running. In addition, many likeenvare ambiguous. Here's the list of variables we could add and which already exist.| Variable | devcontainer.json | Exists |
|----------|---------------------|-------|
|
workspaceFolder|localWorkspaceFolder| โ||
workspaceFolder|containerWorkspaceFolder| โ||
workspaceFolderBasename|localWorkspaceFolderBasename| โ||
workspaceFolderBasename|containerWorkspaceFolderBasename| โ||
env|localEnv| โ||
env|containerEnv(cannot be used fromcontainerEnvproperty, but can be used inremoteEnv) | โ ||
execPath|localExecPath| ||
execPath|containerExecPath| ||
config|config| |