vscode-go icon indicating copy to clipboard operation
vscode-go copied to clipboard

Use dotenv for reading env files

Open ekulabuhov opened this issue 1 year ago • 1 comments

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:

  • https://github.com/microsoft/vscode-js-debug/pull/1117
  • https://github.com/microsoft/vscode-java-debug/pull/1062

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

ekulabuhov avatar Jun 27 '24 16:06 ekulabuhov

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 avatar Jun 29 '24 04:06 hyangah