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

Use dotenv for reading env files #3437

Open
ekulabuhov opened this issue Jun 27, 2024 · 1 comment
Open

Use dotenv for reading env files #3437

ekulabuhov opened this issue Jun 27, 2024 · 1 comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@ekulabuhov
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Current implementation does not support multiline env variables, example below won't read past the first line:

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
...
Kh9NV...
...
-----END RSA PRIVATE KEY-----"

Describe the solution you'd like
It looks like several other adapters switched to using dotenv:

Describe alternatives you've considered

  • Manually fixing regex is also an option, I guess

Additional context
Here's the current implementation: https://github.com/golang/vscode-go/blob/master/extension/src/utils/envUtils.ts#L32-L44

The change is fairly simple as env parsing code is identical in all extensions:
image

@gopherbot gopherbot added this to the Untriaged milestone Jun 27, 2024
@ansaba ansaba added the HelpWanted Issues that are not prioritized by the maintainers. Help is requested from community contributors. label Jun 27, 2024
@hyangah hyangah added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed HelpWanted Issues that are not prioritized by the maintainers. Help is requested from community contributors. labels Jun 29, 2024
@hyangah
Copy link
Contributor

hyangah commented Jun 29, 2024

The current code is not only parsing, but also does variable substitution.

		const buffer = stripBOM(fs.readFileSync(envFilePath, 'utf8'));
		buffer.split('\n').forEach((line) => {
			const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
			if (r !== null) {
				let value = r[2] || '';
				if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
					value = value.replace(/\\n/gm, '\n');
				}
				const v = value.replace(/(^['"]|['"]$)/g, '');
				env[r[1]] = substituteEnvVars(v, env, globalVars!);
			}
		});
		return env;

microsoft/vscode-python extension does env var substitution similarly, and they do not use dotenv because it loses ordering (1).

dotenv's parse itself is pretty straightforward (except the complex regexp) and not too many lines of code. How about borrowing the code but modifying it to preserve ordering?

@hyangah hyangah modified the milestones: Untriaged, vscode-go/unplanned Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants