Skip to content
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

Open
karrtikr opened this issue Jan 18, 2022 · 4 comments
Open

Standardize how to do environment variable parsing & substitution #18307

karrtikr opened this issue Jan 18, 2022 · 4 comments
Labels
area-debugging area-environments Features relating to handling interpreter environments bug Issue identified by VS Code Team member as probable bug meta Issue that is tracking an overall project needs PR Ready to be worked on

Comments

@karrtikr
Copy link

karrtikr commented Jan 18, 2022

There're two parts to this issue:

Parsing

We should probably always follow dotenv syntax:

Substitution

There are currently two ways of substituting variables in a .env file:

VAR2="value;${env:VAR1}"

and

VAR2="value;${VAR1}"

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.

@karrtikr karrtikr added bug Issue identified by VS Code Team member as probable bug needs PR area-debugging meta Issue that is tracking an overall project triage-needed Needs assignment to the proper sub-team area-environments Features relating to handling interpreter environments labels Jan 18, 2022
@karrtikr karrtikr changed the title Clarify how to do environment variable substitution Standardize how to do environment variable substitution Jan 18, 2022
@karthiknadig karthiknadig removed the triage-needed Needs assignment to the proper sub-team label Jan 18, 2022
@github-actions github-actions bot removed the needs PR label Aug 9, 2022
@karrtikr karrtikr added the needs PR Ready to be worked on label Aug 9, 2022
@karrtikr karrtikr changed the title Standardize how to do environment variable substitution Standardize how to do environment variable parsing & substitution Nov 2, 2023
thekaveman added a commit to cal-itp/benefits that referenced this issue Feb 13, 2024
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
thekaveman added a commit to cal-itp/benefits that referenced this issue Feb 13, 2024
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
thekaveman added a commit to cal-itp/benefits that referenced this issue Feb 13, 2024
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
thekaveman added a commit to cal-itp/benefits that referenced this issue Feb 13, 2024
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
thekaveman added a commit to cal-itp/benefits that referenced this issue Feb 13, 2024
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
@starball5
Copy link

@ababak
Copy link

ababak commented Nov 8, 2024

Any updates on this? The lack of support for newlines is problematic.

@eleanorjboyd
Copy link
Member

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

@karthiknadig
Copy link
Member

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:
API: https://github.com/microsoft/vscode-python-environments/blob/0519050ee0d85baf0487bdb4a7519fbc06db60bc/src/api.ts#L1197-L1222

Implementation: https://github.com/microsoft/vscode-python-environments/blob/main/src/features/execution/envVariableManager.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-debugging area-environments Features relating to handling interpreter environments bug Issue identified by VS Code Team member as probable bug meta Issue that is tracking an overall project needs PR Ready to be worked on
Projects
None yet
Development

No branches or pull requests

5 participants