11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4+ import * as dotenv from 'dotenv' ;
45import * as fsapi from 'fs-extra' ;
56import { Disposable , env , l10n , LanguageStatusSeverity , LogOutputChannel , Uri } from 'vscode' ;
67import { State } from 'vscode-languageclient' ;
@@ -20,33 +21,6 @@ import { getConfiguration } from './vscodeapi';
2021
2122export 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 ( / ^ e x p o r t \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-
5024async 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