From 2980b60439bcd91a43fd52cdad5897b5db5c1da2 Mon Sep 17 00:00:00 2001 From: Jonah Graham Date: Mon, 24 Feb 2025 20:52:18 -0500 Subject: [PATCH] Include system environment when debugging core build When doing a local launch, the only environment passed to the debuggee was the modified variables, typically just PATH. This meant all the other environment was lost. This change does the same thing as all the other calls to setBuildEnvironment and passes in the initial system environment. I added a small note to the API of setBuildEnvironment to try to avoid this error in the future. --- .../cdt/core/build/ICBuildConfiguration.java | 3 +++ .../CoreBuildLocalDebugLaunchDelegate.java | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/build/ICBuildConfiguration.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/build/ICBuildConfiguration.java index fc1cc39c679..f8617102862 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/build/ICBuildConfiguration.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/build/ICBuildConfiguration.java @@ -151,6 +151,9 @@ public interface ICBuildConfiguration extends IAdaptable, IScannerInfoProvider { * @param env * build environment * @since 6.1 + * @implNote Ensure you pass a new non-empty map containing the base environment + * as this method modifies the passed in map adding the ICBuildConfiguration specific + * overrides, such as applying the project's build environment. */ default void setBuildEnvironment(Map env) { } diff --git a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/internal/launching/CoreBuildLocalDebugLaunchDelegate.java b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/internal/launching/CoreBuildLocalDebugLaunchDelegate.java index 37ca5ab01b7..d7ee8191f9f 100644 --- a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/internal/launching/CoreBuildLocalDebugLaunchDelegate.java +++ b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/internal/launching/CoreBuildLocalDebugLaunchDelegate.java @@ -34,6 +34,7 @@ import org.eclipse.cdt.dsf.gdb.launching.ServicesLaunchSequence; import org.eclipse.cdt.dsf.gdb.service.GdbDebugServicesFactory; import org.eclipse.cdt.dsf.gdb.service.command.IGDBControl; import org.eclipse.cdt.dsf.service.DsfServicesTracker; +import org.eclipse.cdt.utils.spawner.EnvironmentReader; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; @@ -76,10 +77,20 @@ public class CoreBuildLocalDebugLaunchDelegate extends CoreBuildLaunchConfigDele ILaunchTarget target = ((ITargetedLaunch) launch).getLaunchTarget(); ICBuildConfiguration buildConfig = getBuildConfiguration(configuration, mode, target, monitor); - Map buildEnv = new HashMap<>(); - buildConfig.setBuildEnvironment(buildEnv); + Map systemEnv = new HashMap<>(); + Properties environmentVariables = EnvironmentReader.getEnvVars(); + for (String key : environmentVariables.stringPropertyNames()) { + String value = environmentVariables.getProperty(key); + systemEnv.put(key, value); + } + + var before = new HashMap<>(systemEnv); + buildConfig.setBuildEnvironment(systemEnv); + + before.forEach((k, v) -> systemEnv.remove(k, v)); + Properties envProps = new Properties(); - envProps.putAll(buildEnv); + envProps.putAll(systemEnv); gdbLaunch.setInitialEnvironment(envProps); String debugger = configuration.getAttribute(IGDBLaunchConfigurationConstants.ATTR_DEBUG_NAME,