Skip to content

Commit

Permalink
feat: "deno.env" and "deno.envFile" settings (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Jul 3, 2024
1 parent 17fd0b2 commit 57bd420
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 31 deletions.
18 changes: 18 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"vscode": "^1.60.0"
},
"dependencies": {
"dotenv": "^16.4.5",
"jsonc-parser": "^3.2.0",
"semver": "7.5.2",
"vscode-languageclient": "^8.0.1"
Expand Down
56 changes: 49 additions & 7 deletions client/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ import { registryState } from "./lsp_extensions";
import { createRegistryStateHandler } from "./notification_handlers";
import { DenoServerInfo } from "./server_info";

import * as dotenv from "dotenv";
import * as semver from "semver";
import * as vscode from "vscode";
import { LanguageClient, ServerOptions } from "vscode-languageclient/node";
import type { Location, Position } from "vscode-languageclient/node";
import { getWorkspacesEnabledInfo, setupCheckConfig } from "./enable";
import { denoUpgradePromptAndExecute } from "./upgrade";
import { join } from "path";
import { readFileSync } from "fs";

// deno-lint-ignore no-explicit-any
export type Callback = (...args: any[]) => unknown;
Expand Down Expand Up @@ -118,14 +121,34 @@ export function startLanguageServer(

const config = vscode.workspace.getConfiguration(EXTENSION_NS);

const env: Record<string, string> = {
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
const env: Record<string, string | undefined> = {
...process.env,
"DENO_V8_FLAGS": getV8Flags(),
"NO_COLOR": "1",
};
const denoEnvFile = config.get<string>("envFile");
if (denoEnvFile) {
if (workspaceFolder) {
const denoEnvPath = join(workspaceFolder.uri.fsPath, denoEnvFile);
try {
const content = readFileSync(denoEnvPath, { encoding: "utf8" });
const parsed = dotenv.parse(content);
Object.assign(env, parsed);
} catch (error) {
vscode.window.showErrorMessage(
`Could not read env file "${denoEnvPath}": ${error}`,
);
}
}
}
const denoEnv = config.get<Record<string, string>>("env");
if (denoEnv) {
Object.assign(env, denoEnv);
}
if (config.get<boolean>("future")) {
env["DENO_FUTURE"] = "1";
}
env["NO_COLOR"] = "1";
env["DENO_V8_FLAGS"] = getV8Flags();

const serverOptions: ServerOptions = {
run: {
Expand Down Expand Up @@ -349,7 +372,27 @@ export function test(
testArgs.push("--import-map", importMap.trim());
}
}
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
const env = {} as Record<string, string>;
const denoEnvFile = config.get<string>("envFile");
if (denoEnvFile) {
if (workspaceFolder) {
const denoEnvPath = join(workspaceFolder.uri.fsPath, denoEnvFile);
try {
const content = readFileSync(denoEnvPath, { encoding: "utf8" });
const parsed = dotenv.parse(content);
Object.assign(env, parsed);
} catch (error) {
vscode.window.showErrorMessage(
`Could not read env file "${denoEnvPath}": ${error}`,
);
}
}
}
const denoEnv = config.get<Record<string, string>>("env");
if (denoEnv) {
Object.assign(env, denoEnv);
}
const cacheDir: string | undefined | null = config.get("cache");
if (cacheDir?.trim()) {
env["DENO_DIR"] = cacheDir.trim();
Expand All @@ -367,11 +410,10 @@ export function test(
env,
};

assert(vscode.workspace.workspaceFolders);
const target = vscode.workspace.workspaceFolders[0];
assert(workspaceFolder);
const denoCommand = await getDenoCommandName();
const task = tasks.buildDenoTask(
target,
workspaceFolder,
denoCommand,
definition,
`test "${name}"`,
Expand All @@ -389,7 +431,7 @@ export function test(
const createdTask = await vscode.tasks.executeTask(task);

if (options?.inspect) {
await vscode.debug.startDebugging(target, {
await vscode.debug.startDebugging(workspaceFolder, {
name,
request: "attach",
type: "node",
Expand Down
58 changes: 37 additions & 21 deletions client/src/debug_config_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as vscode from "vscode";
import type { DenoExtensionContext } from "./types";
import { getDenoCommandName, getInspectArg } from "./util";
import { EXTENSION_NS } from "./constants";

export class DenoDebugConfigurationProvider
implements vscode.DebugConfigurationProvider {
Expand All @@ -11,7 +12,12 @@ export class DenoDebugConfigurationProvider
#getEnv() {
const settings = this.#extensionContext.clientOptions
.initializationOptions();
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
const env: Record<string, string> = {};
const denoEnv = config.get<Record<string, string>>("env");
if (denoEnv) {
Object.assign(env, denoEnv);
}
if (settings.cache) {
env["DENO_DIR"] = settings.cache;
}
Expand Down Expand Up @@ -48,24 +54,28 @@ export class DenoDebugConfigurationProvider
}

async provideDebugConfigurations(): Promise<vscode.DebugConfiguration[]> {
return [
{
request: "launch",
name: "Launch Program",
type: "node",
program: "${workspaceFolder}/main.ts",
cwd: "${workspaceFolder}",
env: this.#getEnv(),
runtimeExecutable: await getDenoCommandName(),
runtimeArgs: [
"run",
...this.#getAdditionalRuntimeArgs(),
this.#getInspectArg(),
"--allow-all",
],
attachSimplePort: 9229,
},
];
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
const debugConfig: vscode.DebugConfiguration = {
request: "launch",
name: "Launch Program",
type: "node",
program: "${workspaceFolder}/main.ts",
cwd: "${workspaceFolder}",
env: this.#getEnv(),
runtimeExecutable: await getDenoCommandName(),
runtimeArgs: [
"run",
...this.#getAdditionalRuntimeArgs(),
this.#getInspectArg(),
"--allow-all",
],
attachSimplePort: 9229,
};
const denoEnvFile = config.get<string>("envFile");
if (denoEnvFile) {
debugConfig.envFile = denoEnvFile;
}
return [debugConfig];
}

async resolveDebugConfiguration(
Expand All @@ -81,9 +91,9 @@ export class DenoDebugConfigurationProvider
(langId === "typescript" || langId === "javascript" ||
langId === "typescriptreact" || langId === "javascriptreact")
) {
// https://github.com/microsoft/vscode/issues/106703#issuecomment-694595773
// Bypass the bug of the vscode 1.49.0
vscode.debug.startDebugging(workspace, {
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
const debugConfig: vscode.DebugConfiguration = {
request: "launch",
name: "Launch Program",
type: "node",
Expand All @@ -97,7 +107,13 @@ export class DenoDebugConfigurationProvider
"--allow-all",
],
attachSimplePort: 9229,
});
};
const denoEnvFile = config.get<string>("envFile");
if (denoEnvFile) {
debugConfig.envFile = denoEnvFile;
}
// https://github.com/microsoft/vscode/issues/106703#issuecomment-694595773
vscode.debug.startDebugging(workspace, debugConfig);
return undefined;
}
return null;
Expand Down
2 changes: 2 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ function handleConfigurationChange(event: vscode.ConfigurationChangeEvent) {
event.affectsConfiguration("deno.enable") ||
event.affectsConfiguration("deno.disablePaths") ||
event.affectsConfiguration("deno.enablePaths") ||
event.affectsConfiguration("deno.env") ||
event.affectsConfiguration("deno.envFile") ||
event.affectsConfiguration("deno.future") ||
event.affectsConfiguration("deno.internalInspect") ||
event.affectsConfiguration("deno.logFile") ||
Expand Down
28 changes: 25 additions & 3 deletions client/src/upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import { readFileSync } from "fs";
import { EXTENSION_NS } from "./constants";
import * as tasks from "./tasks";
import { UpgradeAvailable } from "./types";
import { assert, getDenoCommandName } from "./util";
import * as dotenv from "dotenv";
import * as vscode from "vscode";
import { join } from "path";

export async function denoUpgradePromptAndExecute(
{ latestVersion, isCanary }: UpgradeAvailable,
Expand All @@ -31,7 +34,27 @@ export async function denoUpgradePromptAndExecute(
}
args.push("--version");
args.push(latestVersion);
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
const env = {} as Record<string, string>;
const denoEnvFile = config.get<string>("envFile");
if (denoEnvFile) {
if (workspaceFolder) {
const denoEnvPath = join(workspaceFolder.uri.fsPath, denoEnvFile);
try {
const content = readFileSync(denoEnvPath, { encoding: "utf8" });
const parsed = dotenv.parse(content);
Object.assign(env, parsed);
} catch (error) {
vscode.window.showErrorMessage(
`Could not read env file "${denoEnvPath}": ${error}`,
);
}
}
}
const denoEnv = config.get<Record<string, string>>("env");
if (denoEnv) {
Object.assign(env, denoEnv);
}
const cacheDir: string | undefined | null = config.get("cache");
if (cacheDir?.trim()) {
env["DENO_DIR"] = cacheDir.trim();
Expand All @@ -45,11 +68,10 @@ export async function denoUpgradePromptAndExecute(
args,
env,
};
assert(vscode.workspace.workspaceFolders);
const target = vscode.workspace.workspaceFolders[0];
assert(workspaceFolder);
const denoCommand = await getDenoCommandName();
const task = tasks.buildDenoTask(
target,
workspaceFolder,
denoCommand,
definition,
"upgrade",
Expand Down
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,29 @@
"C:\\Program Files\\deno\\deno.exe"
]
},
"deno.env": {
"type": "object",
"default": {},
"patternProperties": {
".+": {
"type": "string"
}
},
"markdownDescription": "Additional environment variables to pass to Deno processes. Overrides the user's env and `deno.envFile`. These will be overridden by more specific settings such as `deno.future` for `DENO_FUTURE`, and invariables like `NO_COLOR=1`.",
"scope": "window",
"examples": [
{ "HTTP_PROXY": "http://localhost:8080" }
]
},
"deno.envFile": {
"type": "string",
"default": null,
"markdownDescription": "Env file containing additional environment variables to pass to Deno processes. Overrides the user's env. These will be overridden by `deno.env`, more specific settings such as `deno.future` for `DENO_FUTURE`, and invariables like `NO_COLOR=1`.",
"scope": "window",
"examples": [
".env"
]
},
"deno.cache": {
"type": "string",
"default": null,
Expand Down

0 comments on commit 57bd420

Please sign in to comment.