Skip to content

Use parent environment to allow variable substitution#165

Merged
ashald merged 2 commits into
ashald:developfrom
jansorg:FIX-env-substitution
Oct 2, 2022
Merged

Use parent environment to allow variable substitution#165
ashald merged 2 commits into
ashald:developfrom
jansorg:FIX-env-substitution

Conversation

@jansorg
Copy link
Copy Markdown
Contributor

@jansorg jansorg commented Dec 11, 2021

I'm experimenting to support EnvFile with [BashSupport Pro}(https://www.bashsupport.com).
I noticed that the variable substitution isn't working as it should.

Currently EnvFile only provides the variables configured directly in the run configuration to the var env providers.
Now, if a variable substitution references a variable, which is inherited from the parent environment, then it has an empty value. That's happening because eneralCommandLine.getEnvironment() is not including the variables from the parent environment.

The SDK's process setup adds the parent environment variables to the new process, if the setting for this is enabled: https://github.com/JetBrains/intellij-community/blob/8e89a51b18059a34081c84439c6c19483f64ad10/platform/platform-util-io/src/com/intellij/execution/configurations/GeneralCommandLine.java#L469

EnvFile must follow the same logic to provide the same set of variables to the patched command line.
I successfully tested this with Python, the other modified providers follow the same logic.

I don't know much about EnvFile, but hope that this is helpful.

Copy link
Copy Markdown
Owner

@ashald ashald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!
I left few comments, could you take a look please?

Map<String, String> currentEnv = generalCommandLine.getEnvironment();
Map<String, String> newEnv = EnvFileConfigurationEditor.collectEnv(goRunConfigurationBase, new HashMap<>(currentEnv));
Map<String, String> newEnv = EnvFileConfigurationEditor.collectEnv(goRunConfigurationBase, EnvUtil.getInitialEnv(generalCommandLine));
Map<String, String> currentEnv = generalCommandLine.getEffectiveEnvironment();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currentEnv seems unused?..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling getEffectiveEnvironment() was wrong (unintentional change, missed in the code cleanup).
cmdline.getEnvironment() returns the reference to the environment, not a copy. As with the old code, clear and putAll on it modifies the data used by the command line. I added comments to clarify, because it's confusing at first sight.

protected void patchCommandLine(@NotNull AbstractPythonRunConfiguration configuration, @Nullable RunnerSettings runnerSettings, @NotNull GeneralCommandLine cmdLine, @NotNull String runnerId) throws ExecutionException {
Map<String, String> newEnv = EnvFileConfigurationEditor.collectEnv(configuration, EnvUtil.getInitialEnv(cmdLine));
Map<String, String> currentEnv = cmdLine.getEnvironment();
Map<String, String> newEnv = EnvFileConfigurationEditor.collectEnv(configuration, new HashMap<>(currentEnv));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change, currentEnv defined above becomes unused - should we drop it altogether?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above. Keeping the references to currentEnv together seemed clearer to me.

protected void patchCommandLine(@NotNull AbstractRubyRunConfiguration<?> configuration, @Nullable RunnerSettings runnerSettings, @NotNull GeneralCommandLine cmdLine, @NotNull String runnerId) throws ExecutionException {
Map<String, String> newEnv = EnvFileConfigurationEditor.collectEnv(configuration, EnvUtil.getInitialEnv(cmdLine));
Map<String, String> currentEnv = cmdLine.getEnvironment();
Map<String, String> newEnv = EnvFileConfigurationEditor.collectEnv(configuration, new HashMap<>(currentEnv));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change, currentEnv defined above becomes unused - should we drop it altogether?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

@jansorg jansorg requested a review from ashald December 15, 2021 05:14
@jansorg
Copy link
Copy Markdown
Contributor Author

jansorg commented Feb 15, 2022

Can I help with anything to get this merged?

@ashald ashald merged commit 0d383e6 into ashald:develop Oct 2, 2022
ashald added a commit that referenced this pull request Oct 2, 2022
This reverts commit 0d383e6, reversing
changes made to c988be9.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants