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

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

Open
shellvon opened this issue Jul 12, 2024 · 6 comments
Open

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

shellvon opened this issue Jul 12, 2024 · 6 comments
Assignees
Labels
area-environments Features relating to handling interpreter environments area-terminal triage-needed Needs assignment to the proper sub-team

Comments

@shellvon
Copy link

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
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Jul 12, 2024
@anthonykim1
Copy link

Hello @shellvon. Thank you for filing this issue. I appreciate the code reference and suggestion here.
If I am understanding correctly, is the comment part of your lines in .env file also being "activated" when the terminal activates and loads your environment variables in the .env file?

Would be curious to see how your environment variables look after the Python extension activates your terminal.
I assume #your comment would show up as part of printing environment variables??

@anthonykim1 anthonykim1 added area-environments Features relating to handling interpreter environments area-terminal and removed triage-needed Needs assignment to the proper sub-team labels Jul 23, 2024
@github-actions github-actions bot added the info-needed Issue requires more information from poster label Jul 23, 2024
@shellvon
Copy link
Author

Yes, When using this extension, my code read SOME_VAR as "900" #inline comments (with inline comment) instead of "900". because this extension don't parse any inline comment

@brettcannon
Copy link
Member

Ah, the joys of .env files not being a standard. 🙃

Copy link

Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on, then we will be happy to re-open this issue to pick up where we left off.

Happy Coding!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 25, 2024
@shellvon
Copy link
Author

shellvon commented Aug 26, 2024

I think I've given enough information, it would be weird to just close it like this... @anthonykim1

@anthonykim1
Copy link

Hi @shellvon, yes I am reopening this. Please excuse the bot

@anthonykim1 anthonykim1 reopened this Aug 26, 2024
@anthonykim1 anthonykim1 removed the info-needed Issue requires more information from poster label Aug 26, 2024
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-environments Features relating to handling interpreter environments area-terminal triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

4 participants
@brettcannon @shellvon @anthonykim1 and others