11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4- import * as dotenv from 'dotenv' ;
54import * as fsapi from 'fs-extra' ;
65import * as path from 'path' ;
76import { Disposable , env , l10n , LanguageStatusSeverity , LogOutputChannel , Uri } from 'vscode' ;
@@ -13,36 +12,16 @@ import {
1312 ServerOptions ,
1413} from 'vscode-languageclient/node' ;
1514import { DEBUG_SERVER_SCRIPT_PATH , SERVER_SCRIPT_PATH } from './constants' ;
15+ import { getEnvFileVars } from './envFile' ;
1616import { traceError , traceInfo , traceVerbose } from './logging' ;
1717import { getDebuggerPath } from './python' ;
1818import { getExtensionSettings , getGlobalSettings , ISettings } from './settings' ;
1919import { getLSClientTraceLevel , getDocumentSelector } from './utilities' ;
2020import { updateStatus } from './status' ;
21- import { getConfiguration } from './vscodeapi' ;
21+ import { getWorkspaceFolder } from './vscodeapi' ;
2222
2323export type IInitOptions = { settings : ISettings [ ] ; globalSettings : ISettings } ;
2424
25- async function loadEnvFile ( workspacePath : string ) : Promise < Record < string , string > > {
26- try {
27- const pythonConfig = getConfiguration ( 'python' , Uri . file ( workspacePath ) ) ;
28- let envFilePath = pythonConfig . get < string > ( 'envFile' , '${workspaceFolder}/.env' ) ;
29- envFilePath = envFilePath . replace ( '${workspaceFolder}' , workspacePath ) ;
30-
31- if ( await fsapi . pathExists ( envFilePath ) ) {
32- const content = await fsapi . readFile ( envFilePath , 'utf-8' ) ;
33- const envVars = dotenv . parse ( content ) ;
34- const count = Object . keys ( envVars ) . length ;
35- if ( count > 0 ) {
36- traceInfo ( `Loaded ${ count } environment variable(s) from ${ envFilePath } ` ) ;
37- }
38- return envVars ;
39- }
40- } catch ( ex ) {
41- traceError ( `Failed to load envFile: ${ ex } ` ) ;
42- }
43- return { } ;
44- }
45-
4625/**
4726 * Resolves the CWD for spawning the server process.
4827 *
@@ -70,8 +49,9 @@ async function createServer(
7049 const newEnv = { ...process . env } ;
7150
7251 // Load environment variables from python.envFile (.env)
73- const workspacePath = Uri . parse ( settings . workspace ) . fsPath ;
74- const envFileVars = await loadEnvFile ( workspacePath ) ;
52+ const workspaceUri = Uri . parse ( settings . workspace ) ;
53+ const workspaceFolder = getWorkspaceFolder ( workspaceUri ) ;
54+ const envFileVars = workspaceFolder ? await getEnvFileVars ( workspaceFolder ) : { } ;
7555 for ( const [ key , val ] of Object . entries ( envFileVars ) ) {
7656 if ( ( key === 'PYTHONPATH' || key === 'PATH' ) && newEnv [ key ] ) {
7757 newEnv [ key ] = newEnv [ key ] + path . delimiter + val ;
0 commit comments