-
Notifications
You must be signed in to change notification settings - Fork 270
[bug] plugin env PATH fix #2418
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
Changes from 6 commits
4686d25
165ffec
bffbfc6
9ee0767
b56a5ff
163ae99
8c84419
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
|
@@ -346,9 +347,18 @@ func (c *Config) NixPkgsCommitHash() string { | |
func (c *Config) Env() map[string]string { | ||
env := map[string]string{} | ||
for _, i := range c.included { | ||
maps.Copy(env, i.Env()) | ||
// Here, in new pluginPath we replace the keyword "$PATH" | ||
// with what we have for PATH so far. If so far we have /plugin1/bin:$PATH | ||
// and new plugin adds /plugin2/bin/:$PATH the result becomes | ||
// /plugin2/bin/:/plugin1/bin/:$PATH | ||
pluginEnv := mergePATHsFromTwoEnvs(env, i.Env()) | ||
maps.Copy(env, pluginEnv) | ||
} | ||
maps.Copy(env, c.Root.Env) | ||
// Here we merge the processed PATH from all plugins to PATH in config env. | ||
// So if config env has /config/env/:$PATH merging with the plugin PATH | ||
// it becomes /config/env/:/plugin2/bin/:/plugin1/bin/:$PATH | ||
rootEnv := mergePATHsFromTwoEnvs(env, c.Root.Env) | ||
maps.Copy(env, rootEnv) | ||
|
||
return env | ||
} | ||
|
||
|
@@ -406,3 +416,18 @@ func createIncludableFromPluginConfig(pluginConfig *plugin.Config) *Config { | |
} | ||
return includable | ||
} | ||
|
||
// Instead of overwriting PATH modifications we need to merge them | ||
// so merging of /plugin1/bin/:$PATH and /plugin2/bin/:$PATH | ||
// and /config/env/bin/:$PATH should result in | ||
// | ||
// /config/env/bin/:/plugin2/bin/:/plugin1/bin/:$PATH | ||
// | ||
// because config env should take priority over plugins | ||
func mergePATHsFromTwoEnvs(currentEnv, newEnv map[string]string) map[string]string { | ||
if currentEnv["PATH"] != "" && newEnv["PATH"] != "" { | ||
slog.Debug("A Plugin or Config wants to modify PATH. Processing the merge", "AddedPATH", newEnv["PATH"]) | ||
newEnv["PATH"] = strings.Replace(newEnv["PATH"], "$PATH", currentEnv["PATH"], 1) | ||
} | ||
return newEnv | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use
OSExpandEnvMap
instead ofmergePATHsFromTwoEnvs
?