Skip to content

Commit e338cc2

Browse files
edvilmebalajishanmugam85Copilot
authored
Use dotenv for parsing .env files (#771)
Co-authored-by: Copilot <226224632+Copilot@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 28220b7 commit e338cc2

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@
230230
"dependencies": {
231231
"@vscode/python-environments": "https://pkgs.dev.azure.com/azure-public/vside/_packaging/msft_consumption/npm/registry/@vscode/python-environments/-/python-environments-1.0.0.tgz",
232232
"@vscode/python-extension": "^1.0.6",
233+
"dotenv": "^17.4.0",
233234
"fs-extra": "^11.3.4",
234235
"semver": "^7.7.4",
235236
"vscode-languageclient": "^8.1.0"

src/common/server.ts

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
import * as dotenv from 'dotenv';
45
import * as fsapi from 'fs-extra';
56
import { Disposable, env, l10n, LanguageStatusSeverity, LogOutputChannel, Uri } from 'vscode';
67
import { State } from 'vscode-languageclient';
@@ -20,33 +21,6 @@ import { getConfiguration } from './vscodeapi';
2021

2122
export type IInitOptions = { settings: ISettings[]; globalSettings: ISettings };
2223

23-
function parseEnvFile(content: string): Record<string, string> {
24-
const env: Record<string, string> = {};
25-
for (const line of content.split(/\r?\n/)) {
26-
const trimmed = line.trim();
27-
if (!trimmed || trimmed.startsWith('#')) {
28-
continue;
29-
}
30-
const eqIndex = trimmed.indexOf('=');
31-
if (eqIndex === -1) {
32-
continue;
33-
}
34-
const key = trimmed
35-
.substring(0, eqIndex)
36-
.trim()
37-
.replace(/^export\s+/, '');
38-
let value = trimmed.substring(eqIndex + 1).trim();
39-
// Strip surrounding quotes
40-
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
41-
value = value.slice(1, -1);
42-
}
43-
if (key) {
44-
env[key] = value;
45-
}
46-
}
47-
return env;
48-
}
49-
5024
async function loadEnvVarsFromFile(workspace: Uri): Promise<Record<string, string>> {
5125
const pythonConfig = getConfiguration('python', workspace);
5226
let envFileSetting = pythonConfig.get<string>('envFile', '${workspaceFolder}/.env');
@@ -59,7 +33,7 @@ async function loadEnvVarsFromFile(workspace: Uri): Promise<Record<string, strin
5933
try {
6034
const content = await fsapi.readFile(envFileSetting, 'utf-8');
6135
traceInfo(`Loaded environment variables from ${envFileSetting}`);
62-
return parseEnvFile(content);
36+
return dotenv.parse(content);
6337
} catch (ex) {
6438
traceError(`Failed to read env file ${envFileSetting}: ${ex}`);
6539
return {};
@@ -74,13 +48,13 @@ async function createServer(
7448
initializationOptions: IInitOptions,
7549
): Promise<LanguageClient> {
7650
const command = settings.interpreter[0];
77-
const cwd = settings.cwd === '${fileDirname}' ? Uri.file(settings.workspace).fsPath : settings.cwd;
51+
const cwd = settings.cwd === '${fileDirname}' ? Uri.parse(settings.workspace).fsPath : settings.cwd;
7852

7953
// Set debugger path needed for debugging Python code.
8054
const newEnv = { ...process.env };
8155

8256
// Load environment variables from the envFile (python.envFile setting)
83-
const workspaceUri = Uri.file(settings.workspace);
57+
const workspaceUri = Uri.parse(settings.workspace);
8458
const envFileVars = await loadEnvVarsFromFile(workspaceUri);
8559
Object.assign(newEnv, envFileVars);
8660

0 commit comments

Comments
 (0)