Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package net.ashald.envfile.products.goland;

import com.goide.execution.GoRunConfigurationBase;
import com.goide.execution.GoRunningState;
import com.goide.execution.extension.GoRunConfigurationExtension;
import com.goide.util.GoExecutor;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.RunnerSettings;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.options.SettingsEditor;
import net.ashald.envfile.platform.EnvFileEnvironmentVariables;
import net.ashald.envfile.platform.ui.EnvFileConfigurationEditor;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

public class GolandRunConfigurationExtension extends GoRunConfigurationExtension {

private static final Logger LOG = Logger.getInstance(GolandRunConfigurationExtension.class);

@Nullable
@Override
protected String getEditorTitle() {
return EnvFileConfigurationEditor.getEditorTitle();
}

// Local runs: GoLand builds a GeneralCommandLine and invokes this legacy hook.
@Override
protected void patchCommandLine(
@NotNull GoRunConfigurationBase<?> goRunConfigurationBase,
Expand All @@ -48,6 +55,55 @@ protected void patchCommandLine(
generalCommandLine.getEnvironment().putAll(newEnv);
}

// Target runs (WSL / remote): GoLand bypasses the GeneralCommandLine hook above and goes through the
// Targets API. The executor-level hook below fires for those runs (confirmed for WSL), so we inject the
// EnvFile variables into the GoExecutor here. We add only the delta over the (Windows) parent environment
// to avoid leaking host-only variables (PATH, ProgramFiles, ...) into the target (e.g. Linux under WSL).
@Override
protected void patchExecutor(
@NotNull GoRunConfigurationBase<?> configuration,
@Nullable RunnerSettings runnerSettings,
@NotNull GoExecutor executor,
@NotNull String runnerId,
@NotNull GoRunningState<? extends GoRunConfigurationBase<?>> state,
@NotNull GoRunningState.CommandLineType commandLineType
)
throws ExecutionException
{
if (commandLineType != GoRunningState.CommandLineType.RUN) {
return;
}

Map<String, String> newEnv = new EnvFileEnvironmentVariables(
EnvFileConfigurationEditor.getEnvFileSetting(configuration)
)
.render(
configuration.getProject(),
configuration.getCustomEnvironment(),
configuration.isPassParentEnvironment()
);

if (newEnv == null) {
return;
}

Map<String, String> parentEnv = new GeneralCommandLine()
.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE)
.getParentEnvironment();

Map<String, String> delta = new HashMap<>();
for (Map.Entry<String, String> entry : newEnv.entrySet()) {
if (!entry.getValue().equals(parentEnv.get(entry.getKey()))) {
delta.put(entry.getKey(), entry.getValue());
}
}

executor.withUserDefinedEnvironment(delta);

LOG.debug("EnvFile: injected " + delta.size()
+ " variable(s) into GoExecutor for target run: " + delta.keySet());
}

@Override
protected void validateConfiguration(@NotNull GoRunConfigurationBase configuration, boolean isExecution) throws Exception {
EnvFileConfigurationEditor.validateConfiguration(configuration, isExecution);
Expand Down