Skip to content

Commit 9254b98

Browse files
committed
feat: add util function to load dev environment variables from .env
- Added `addDevEnvVariables` util function to allow the extension to load the local `.env` file from the project root if it exists, using Node's `loadEnvFile` API, and load the environment variables into `process.env`. The `.env` file will only exist on the development machine and is crucial to properly test the extension in vscode's extension testing environment. This fixes the ability to mistakenly commit the dev testing path to git. As seen in commit be9d972 and removed in commit 9ff0857 of PR #20. With the .env file, this will never happen again. - Added the `addDevEnvVariables` function call to the top of the `extension.ts` file so it is the first thing it does and the env variables will be available in all files in development. - Added the usage of the `DEV_USER_EXTENSIONS_PATH` env variable to `ExtensionData::setExtensionDiscoveryPaths` method. So that when its available to use, it will override the `userExtensionsPath` variable with the path set in the env variable.
1 parent 0a79426 commit 9254b98

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import * as vscode from "vscode";
55
import {Configuration} from "./configuration";
66
import {logger} from "./logger";
77
import {ExtensionData} from "./extensionData";
8+
import {addDevEnvVariables} from "./utils";
9+
10+
addDevEnvVariables();
811

912
const extensionData = new ExtensionData();
1013
logger.setupOutputChannel();

src/extensionData.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,20 @@ export class ExtensionData {
9494
* Set the extension discovery paths into the extensionDiscoveryPaths Map.
9595
*/
9696
private setExtensionDiscoveryPaths() {
97+
// Get the DEV_USER_EXTENSIONS_PATH env variable if it exists.
98+
const devUserExtensionsPath = process.env.DEV_USER_EXTENSIONS_PATH;
99+
97100
// The path to the user extensions.
98101
//
99102
// On Windows/Linux/Mac: ~/.vscode[-server|remote]/extensions
100103
// On WSL: ~/.vscode-[server|remote]/extensions
101-
const userExtensionsPath = isWsl ? path.join(vscode.env.appRoot, "../../", "extensions") : path.join(this.extensionPath, "../");
104+
//
105+
// Because the extensionPath is created from __dirname and retrieves where this extension
106+
// is located, in extension testing/development mode, this path will point to the local
107+
// development path, not the actual user extensions path. So we use the custom
108+
// DEV_USER_EXTENSIONS_PATH env variable to override it.
109+
const userExtensionsPath =
110+
devUserExtensionsPath || (isWsl ? path.join(vscode.env.appRoot, "../../", "extensions") : path.join(this.extensionPath, "../"));
102111

103112
this.extensionDiscoveryPaths.set("userExtensionsPath", userExtensionsPath);
104113
// The path to the built-in extensions.

src/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from "node:fs";
2+
import * as path from "node:path";
23
import * as jsonc from "jsonc-parser";
34
import {logger} from "./logger";
45
import {window} from "vscode";
@@ -237,3 +238,15 @@ export function mergeArraysBy<T>(primaryArray: T[], secondaryArray: T[], key: ke
237238

238239
return merged;
239240
}
241+
242+
/**
243+
* Add development environment variables from a local .env file located in the project root.
244+
*/
245+
export function addDevEnvVariables() {
246+
// Try to load the local .env file from the project root.
247+
try {
248+
process.loadEnvFile(path.join(__dirname, "../../.env"));
249+
} catch (error) {
250+
// Ignore errors if the .env file doesn't exist
251+
}
252+
}

0 commit comments

Comments
 (0)