-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Standardize how to do environment variable parsing & substitution #18307
Comments
Docker, bash, etc. support multiline environment variables, by simply wrapping the value in single quotes with newlines, e.g. in an .env file: multi_line_value='first line second line third line' Resulting in the expected: $ echo "$multi_line_value" first line second line third line Due to a quirk in VS Code's Python extension, multiline values are not parsed, see https://code.visualstudio.com/docs/python/environments#_environment-variables > ... Multiline values aren't supported ... And more ongoing discussion at microsoft/vscode-python#18307 When running locally in e.g. Debug mode, and secrets are read dynamically from the environment, Python loses the multiline value and we end up with: >> value = os.environ.get("multi_line_value") >> print(value) first line This changes the samples so literal newlines are added to the value of the environment variable in an .env file: multi_line_value='first line\nsecond line\nthird line' So when Python reads it we get the actual newline: first line\nsecond line\nthird line
Docker, bash, etc. support multiline environment variables, by simply wrapping the value in single quotes with newlines, e.g. in an .env file: multi_line_value='first line second line third line' Resulting in the expected: $ echo "$multi_line_value" first line second line third line Due to a quirk in VS Code's Python extension, multiline values are not parsed, see https://code.visualstudio.com/docs/python/environments#_environment-variables > ... Multiline values aren't supported ... And more ongoing discussion at microsoft/vscode-python#18307 When running locally in e.g. Debug mode, and secrets are read dynamically from the environment, Python loses the multiline value and we end up with: >> value = os.environ.get("multi_line_value") >> print(value) first line This changes the samples so literal newlines are added to the value of the environment variable in an .env file: multi_line_value='first line\nsecond line\nthird line' So when Python reads it we get the actual newline: first line\nsecond line\nthird line
Docker, bash, etc. support multiline environment variables, by simply wrapping the value in single quotes with newlines, e.g. in an .env file: multi_line_value='first line second line third line' Resulting in the expected: $ echo "$multi_line_value" first line second line third line Due to a quirk in VS Code's Python extension, multiline values are not parsed, see https://code.visualstudio.com/docs/python/environments#_environment-variables > ... Multiline values aren't supported ... And more ongoing discussion at microsoft/vscode-python#18307 When running locally in e.g. Debug mode, and secrets are read dynamically from the environment, Python loses the multiline value and we end up with: >> value = os.environ.get("multi_line_value") >> print(value) first line This changes the samples so literal newlines are added to the value of the environment variable in an .env file: multi_line_value='first line\nsecond line\nthird line' So when Python reads it we get the actual newline: first line\nsecond line\nthird line
Docker, bash, etc. support multiline environment variables, by simply wrapping the value in single quotes with newlines, e.g. in an .env file: multi_line_value='first line second line third line' Resulting in the expected: $ echo "$multi_line_value" first line second line third line Due to a quirk in VS Code's Python extension, multiline values are not parsed, see https://code.visualstudio.com/docs/python/environments#_environment-variables > ... Multiline values aren't supported ... And more ongoing discussion at microsoft/vscode-python#18307 When running locally in e.g. Debug mode, and secrets are read dynamically from the environment, Python loses the multiline value and we end up with: >> value = os.environ.get("multi_line_value") >> print(value) first line This changes the samples so literal newlines are added to the value of the environment variable in an .env file: multi_line_value='first line\nsecond line\nthird line' So when Python reads it we get the actual newline: first line\nsecond line\nthird line
Docker, bash, etc. support multiline environment variables, by simply wrapping the value in single quotes with newlines, e.g. in an .env file: multi_line_value='first line second line third line' Resulting in the expected: $ echo "$multi_line_value" first line second line third line Due to a quirk in VS Code's Python extension, multiline values are not parsed, see https://code.visualstudio.com/docs/python/environments#_environment-variables > ... Multiline values aren't supported ... And more ongoing discussion at microsoft/vscode-python#18307 When running locally in e.g. Debug mode, and secrets are read dynamically from the environment, Python loses the multiline value and we end up with: >> value = os.environ.get("multi_line_value") >> print(value) first line This changes the samples and docs so literal newlines are added to the value of the environment variable in an .env file: multi_line_value='first line\nsecond line\nthird line' But the initial value read by Python contains _escaped_ newline characters: first line\\nsecond line\\nthird line Hence unescaping so that local secrets contain the actual newline character: first line\nsecond line\nthird line
Related on Stack Overflow: Why does VSCode show inline comments as part of environment variables from .env file? |
Any updates on this? The lack of support for newlines is problematic. |
@karthiknadig and @anthonykim1 - wondering if something along this lines should be a larger discussion across different features. I feel like I have seen a few of these regarding variable substitution and might be worthwhile to tackle? |
We can do this in the new environments extension. The plan is to have an implementation there that can handle broad set of cases. The is still in ideation phase, and is a work in progress: Implementation: https://github.com/microsoft/vscode-python-environments/blob/main/src/features/execution/envVariableManager.ts |
There're two parts to this issue:
Parsing
We should probably always follow dotenv syntax:
Circuit Python: https://docs.circuitpython.org/en/latest/shared-bindings/dotenv/index.html#
It's almost very similar to what we do; except for the multiline support and comments on the same line, so good news is it won't break us if we decide to align.
Python dot-env: https://pypi.org/project/python-dotenv/
Match with how it does variable substitution: Standardize how to do environment variable parsing & substitution #18307
Substitution
There are currently two ways of substituting variables in a
.env
file:and
and a combination of both has to used to resolve all variables in different situations. However, this is not standard syntax. Again we should probably rely on the dotenv syntax always.
${env:...}
only works for process env variables (for eg.PATH
) when debugging or when resolving env variables in settings.${env:...}
in.env
files when debugging a Python file: Environment variable definitions file substitution does not work when debugging a Python file vscode-python-debugger#159. Make sure Environment variable definitions file substitution does not work when debugging a Python file vscode-python-debugger#159 is taken care of with this issue.${env:...}
. It works only with${...}
. Need to correct the docs: Test envVarsService newline substitution behavior #17747 (review)..env
file is currently not supported when running code in terminal: Use environment variables defined in.env
file when running code in a terminal #944${...}
can not be used for process env variables when debugging: https://github.com/microsoft/debugpy/issues/821. Solution:vscode-python/src/client/common/variables/environmentVariablesProvider.ts
Line 56 in 456ac06
${...}
in addition to${env:...}
.The text was updated successfully, but these errors were encountered: