Description
Recently, I discovered that projects opened via VSCode automatically load the .env
file. Upon further investigation, I confirmed that the issue aligns with what was previously noted by #22982 .
In this context, I observed a particular detail: within VSCode, the .env
file supports inline commenting capabilities.
Some other tools that handle env files also support inline comments:
- dotenv https://github.com/motdotla/dotenv/pull/568/files
- python-dotenv https://github.com/theskumar/python-dotenv?tab=readme-ov-file#file-format
When the .env
file is processed by ms-python
, such inline comment functionality is not recognized. Instead, ms-python
leaves the commented parts of the file intact, treating them as part of the active configuration rather than ignoring them as comments.
vscode-python/src/client/common/variables/environment.ts
Lines 141 to 165 in 5fd5098
I believe this behavior can mislead many users because the UI suggests that it is a comment, yet the comments remain when loaded by this plugin. Without additional handling by the program, this can lead to peculiar results.
Perhaps we could maintain consistency with the regular expression found in the dotenv source code:
source from https://github.com/motdotla/dotenv/blob/8ab33066f90a20445d3c41e4fafba6c929c5e1a5/lib/main.js#L9
const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg