Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Add support for multiple environment files #2395

Merged
merged 11 commits into from
Jan 28, 2020
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,14 @@
"description": "Various levels of logging shown in the debug console. When set to 'log' or 'verbose', the logs will also be written to a file."
},
"envFile": {
"type": "string",
"description": "Absolute path to a file containing environment variable definitions.",
"type": [
"string",
"array"
],
"items": {
"type": "string"
},
"description": "Absolute path to a file containing environment variable definitions. Multiple files can be specified by provided an array of absolute paths",
"default": "${workspaceFolder}/.env"
},
"backend": {
Expand Down
19 changes: 14 additions & 5 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { existsSync, lstatSync } from 'fs';
import { basename, dirname, extname } from 'path';
import { spawn, ChildProcess, execSync, spawnSync, execFile } from 'child_process';
import { Client, RPCConnection } from 'json-rpc2';
import { parseEnvFile, getBinPathWithPreferredGopath, getInferredGopath, getCurrentGoWorkspaceFromGOPATH, envPath, fixDriveCasingInWindows } from '../goPath';
import { parseEnvFile, parseEnvFiles, getBinPathWithPreferredGopath, getInferredGopath, getCurrentGoWorkspaceFromGOPATH, envPath, fixDriveCasingInWindows } from '../goPath';

const fsAccess = util.promisify(fs.access);
const fsUnlink = util.promisify(fs.unlink);
Expand Down Expand Up @@ -217,7 +217,7 @@ interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArguments {
init?: string;
trace?: 'verbose' | 'log' | 'error';
/** Optional path to .env file. */
envFile?: string;
envFile?: string | string[];
backend?: string;
output?: string;
/** Delve LoadConfig parameters **/
Expand Down Expand Up @@ -315,14 +315,23 @@ class Delve {
}

// read env from disk and merge into env variables
let fileEnv = {};
const envs = [process.env];

try {
fileEnv = parseEnvFile(launchArgs.envFile);
if (typeof launchArgs.envFile === 'string') {
envs.push(parseEnvFile(launchArgs.envFile));
}

if (Array.isArray(launchArgs.envFile)) {
envs.push(...parseEnvFiles(launchArgs.envFile));
}
} catch (e) {
return reject(e);
}

const env = Object.assign({}, process.env, fileEnv, launchArgsEnv);
envs.push(launchArgsEnv);

const env = Object.assign({}, ...envs);

const dirname = isProgramDirectory ? program : path.dirname(program);
if (!env['GOPATH'] && (mode === 'debug' || mode === 'test')) {
Expand Down
4 changes: 4 additions & 0 deletions src/goPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ export function parseEnvFile(path: string): { [key: string]: string } {
}
}

export function parseEnvFiles(paths: string[]): { [key: string]: string }[] {
return paths.map(path => parseEnvFile(path));
}

// Walks up given folder path to return the closest ancestor that has `src` as a child
export function getInferredGopath(folderPath: string): string {
if (!folderPath) {
Expand Down