-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix: Add env object syntax to launch.json schema for cppdbg and cppvsdbg
#14691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
aebf397
deb60ac
13f598c
66f6d95
793ac13
146e959
7130385
84a0467
e2b17cf
737ecd1
d961d42
034ed40
30f2f9d
c933aee
af30f6c
875e702
51fe13f
ce9cbd9
dc91405
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -270,6 +270,10 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv | |
| // Add environment variables from .env file | ||
| this.resolveEnvFile(config, folder); | ||
|
|
||
| // Debug adapters consume the legacy `environment` array, not `env`. | ||
| // Convert here so both syntaxes work while preserving `env` precedence. | ||
| this.resolveEnvObject(config); | ||
|
|
||
| await this.expand(config, folder); | ||
|
|
||
| this.resolveSourceFileMapVariables(config); | ||
|
|
@@ -700,6 +704,35 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv | |
| } | ||
| } | ||
|
|
||
| private resolveEnvObject(config: CppDebugConfiguration): void { | ||
| if ((config.type !== DebuggerType.cppdbg && config.type !== DebuggerType.cppvsdbg) || config.request !== 'launch') { | ||
| return; | ||
| } | ||
|
|
||
| const envObject = config.env; | ||
| if (!util.isObject(envObject)) { | ||
| return; | ||
| } | ||
|
|
||
| const environment: Environment[] = util.isArray(config.environment) ? config.environment : []; | ||
| const mergedEnvironment = new Map<string, string | null>(); | ||
|
|
||
| for (const entry of environment) { | ||
| if (util.isString(entry?.name) && (util.isString(entry?.value) || entry?.value === null)) { | ||
| mergedEnvironment.set(entry.name, entry.value); | ||
| } | ||
| } | ||
|
|
||
| for (const [name, value] of Object.entries(envObject)) { | ||
| if (util.isString(value) || value === null) { | ||
| mergedEnvironment.set(name, value); | ||
| } | ||
|
Comment on lines
+726
to
+729
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨Copilot (agent117): Confirmed on |
||
| } | ||
|
|
||
| config.environment = Array.from(mergedEnvironment.entries()).map(([name, value]) => ({ name, value })); | ||
| delete config.env; | ||
| } | ||
|
|
||
| private resolveSourceFileMapVariables(config: CppDebugConfiguration): void { | ||
| const messages: string[] = []; | ||
| if (config.sourceFileMap) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,21 @@ import { isWindows } from '../constants'; | |
|
|
||
| nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); | ||
| const localize = nls.loadMessageBundle(); | ||
| type TerminalEnvironment = NonNullable<vscode.TerminalOptions['env']>; | ||
| const managedTerminals = new Map<string, vscode.Terminal>(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨Copilot (agent117): [Minor] Closed terminals remain strongly referenced here until the same executable is launched again. Since each distinct program adds an entry and the per-session close listener is disposed when the session terminates, manually closing retained terminals can accumulate stale |
||
| const terminalEnvironments = new WeakMap<vscode.Terminal, TerminalEnvironment>(); | ||
|
|
||
| type LaunchEnvironmentEntry = { name: string; value: string | null; }; | ||
|
|
||
| type LaunchConfiguration = { | ||
| program?: string; | ||
| args?: string[]; | ||
| cwd?: string; | ||
| environment?: LaunchEnvironmentEntry[]; | ||
| env?: Record<string, string | null>; | ||
| console?: string; | ||
| externalConsole?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * A minimal inline Debug Adapter that runs the target program directly without a debug adapter | ||
|
|
@@ -59,31 +74,30 @@ export class RunWithoutDebuggingAdapter implements vscode.DebugAdapter { | |
| } | ||
|
|
||
| private async launch(request: { command: string; seq: number; arguments?: any; }): Promise<void> { | ||
| const config = request.arguments as { | ||
| program?: string; | ||
| args?: string[]; | ||
| cwd?: string; | ||
| environment?: { name: string; value: string; }[]; | ||
| console?: string; | ||
| externalConsole?: boolean; | ||
| }; | ||
| const config = request.arguments as LaunchConfiguration; | ||
|
|
||
| const program: string = config.program ?? ''; | ||
| const args: string[] = config.args ?? []; | ||
| const cwd: string | undefined = config.cwd; | ||
| const environment: { name: string; value: string; }[] = config.environment ?? []; | ||
| const environment: LaunchEnvironmentEntry[] = config.environment ?? []; | ||
| const envObject: Record<string, string | null> = config.env ?? {}; | ||
| const consoleMode: string = config.console ?? (config.externalConsole ? 'externalTerminal' : 'integratedTerminal'); | ||
|
|
||
| // Merge the launch config's environment variables on top of the inherited process environment. | ||
| // Merge environment values in this order: inherited process environment, legacy | ||
| // `environment` entries, then shorthand `env` values (higher precedence). | ||
| const env: NodeJS.ProcessEnv = { ...process.env }; | ||
| const terminalEnv: TerminalEnvironment = {}; | ||
| for (const e of environment) { | ||
| env[e.name] = e.value; | ||
| this.applyEnvironmentValue(env, terminalEnv, e.name, e.value); | ||
| } | ||
| for (const [key, value] of Object.entries(envObject)) { | ||
| this.applyEnvironmentValue(env, terminalEnv, key, value); | ||
| } | ||
|
|
||
| this.sendResponse(request, {}); | ||
|
|
||
| if (consoleMode === 'integratedTerminal' || consoleMode === 'internalConsole') { | ||
| await this.launchIntegratedTerminal(program, args, cwd, env); | ||
| await this.launchIntegratedTerminal(program, args, cwd, terminalEnv); | ||
| } else if (consoleMode === 'externalTerminal') { | ||
| this.launchExternalTerminal(program, args, cwd, env); | ||
| } | ||
|
|
@@ -93,14 +107,25 @@ export class RunWithoutDebuggingAdapter implements vscode.DebugAdapter { | |
| * Launch the program in a VS Code integrated terminal. | ||
| * The terminal will remain open after the program exits and be reused for the next session, if applicable. | ||
| */ | ||
| private async launchIntegratedTerminal(program: string, args: string[], cwd: string | undefined, env: NodeJS.ProcessEnv): Promise<void> { | ||
| private async launchIntegratedTerminal(program: string, args: string[], cwd: string | undefined, env: TerminalEnvironment): Promise<void> { | ||
| const terminalName = path.normalize(program); | ||
| const existingTerminal = vscode.window.terminals.find(t => t.name === terminalName); | ||
| const managedTerminal = managedTerminals.get(terminalName); | ||
| let existingTerminal = managedTerminal && vscode.window.terminals.includes(managedTerminal) ? managedTerminal : undefined; | ||
| if (!existingTerminal) { | ||
| managedTerminals.delete(terminalName); | ||
| } | ||
| if (existingTerminal && !this.environmentsEqual(terminalEnvironments.get(existingTerminal), env)) { | ||
| existingTerminal.dispose(); | ||
|
8prashant marked this conversation as resolved.
|
||
| existingTerminal = undefined; | ||
| managedTerminals.delete(terminalName); | ||
| } | ||
| this.terminal = existingTerminal ?? vscode.window.createTerminal({ | ||
| name: terminalName, | ||
| cwd, | ||
| env: env as Record<string, string> | ||
| env | ||
| }); | ||
| managedTerminals.set(terminalName, this.terminal); | ||
| terminalEnvironments.set(this.terminal, env); | ||
| this.terminal.show(true); | ||
|
|
||
| const shellIntegration: vscode.TerminalShellIntegration | undefined = | ||
|
|
@@ -212,6 +237,40 @@ export class RunWithoutDebuggingAdapter implements vscode.DebugAdapter { | |
| return arg.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); | ||
| } | ||
|
|
||
| private applyEnvironmentValue(processEnv: NodeJS.ProcessEnv, terminalEnv: TerminalEnvironment, name: string, value: string | null): void { | ||
| const matchingKeys = isWindows | ||
| ? new Set([...Object.keys(processEnv), ...Object.keys(terminalEnv)].filter(key => key.toLowerCase() === name.toLowerCase())) | ||
| : new Set([name]); | ||
|
|
||
| for (const key of matchingKeys) { | ||
| delete processEnv[key]; | ||
| if (key !== name || value === null) { | ||
| terminalEnv[key] = null; | ||
| } | ||
| } | ||
|
|
||
| if (value === null) { | ||
| terminalEnv[name] = null; | ||
| } else { | ||
| processEnv[name] = value; | ||
| terminalEnv[name] = value; | ||
| } | ||
| } | ||
|
|
||
| private environmentsEqual(first: TerminalEnvironment | undefined, second: TerminalEnvironment): boolean { | ||
| if (!first) { | ||
| return false; | ||
| } | ||
|
|
||
| const firstKeys = Object.keys(first); | ||
| const secondKeys = Object.keys(second); | ||
| if (firstKeys.length !== secondKeys.length) { | ||
| return false; | ||
| } | ||
|
|
||
| return firstKeys.every(key => first[key] === second[key]); | ||
| } | ||
|
|
||
| private waitForShellIntegration(terminal: vscode.Terminal, timeoutMs: number): Promise<vscode.TerminalShellIntegration | undefined> { | ||
| return new Promise(resolve => { | ||
| let resolved: boolean = false; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #include <cstdlib> | ||
| #include <fstream> | ||
|
|
||
| int main(int argc, char *argv[]) { | ||
| if (argc < 3) { | ||
| return 1; | ||
| } | ||
|
|
||
| const char *value = std::getenv(argv[1]); | ||
|
|
||
| std::ofstream resultFile(argv[2]); | ||
| if (!resultFile) { | ||
| return 2; | ||
| } | ||
|
|
||
| if (value) { | ||
| resultFile << value; | ||
| } | ||
|
|
||
| return 0; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.