Skip to content

autoloading .env file format does not ignore inline comments. #23799

@shellvon

Description

@shellvon

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.

image

Some other tools that handle env files also support inline comments:

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.

function parseEnvLine(line: string): [string, string] {
// Most of the following is an adaptation of the dotenv code:
// https://github.com/motdotla/dotenv/blob/master/lib/main.js#L32
// We don't use dotenv here because it loses ordering, which is
// significant for substitution.
const match = line.match(/^\s*(_*[a-zA-Z]\w*)\s*=\s*(.*?)?\s*$/);
if (!match) {
return ['', ''];
}
const name = match[1];
let value = match[2];
if (value && value !== '') {
if (value[0] === "'" && value[value.length - 1] === "'") {
value = value.substring(1, value.length - 1);
value = value.replace(/\\n/gm, '\n');
} else if (value[0] === '"' && value[value.length - 1] === '"') {
value = value.substring(1, value.length - 1);
value = value.replace(/\\n/gm, '\n');
}
} else {
value = '';
}
return [name, value];

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

Metadata

Metadata

Assignees

Labels

area-environmentsFeatures relating to handling interpreter environmentsarea-terminalbugIssue identified by VS Code Team member as probable bugneeds spikeLabel for issues that need investigation before they can be worked on.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions