golang / vscode-go

Go extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=golang.Go
Other
3.78k stars 728 forks source link

Use dotenv for reading env files #3437

Open ekulabuhov opened 5 days ago

ekulabuhov commented 5 days ago

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

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
hyangah commented 4 days ago

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?