-
Notifications
You must be signed in to change notification settings - Fork 14
WIP fix: better env loading [IDE-1314] #407
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 4 commits
910e95c
aa51dfe
972ba72
4a692e1
2c466e3
cf823dd
e3dbb06
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 |
|---|---|---|
|
|
@@ -20,62 +20,120 @@ const ( | |
| ShellEnvVarName = "SHELL" | ||
| ) | ||
|
|
||
| // LoadConfiguredEnvironment updates the environment with local configuration. Precedence as follows: | ||
| // 1. std folder-based config files | ||
| // 2. given command-line parameter config file | ||
| // 3. std config file in home directory | ||
| // 4. global shell configuration | ||
| // LoadConfiguredEnvironment updates the environment with local configuration. | ||
| // First the user's SHELL is read, then the configuration files. | ||
| // Any new PATH is prepended to the existing PATH. | ||
| // See LoadShellEnvironment and LoadConfigFiles. | ||
| func LoadConfiguredEnvironment(customConfigFiles []string, workingDirectory string) { | ||
| LoadShellEnvironment() | ||
|
|
||
| LoadConfigFiles(customConfigFiles, workingDirectory) | ||
| } | ||
|
|
||
| // LoadShellEnvironment loads the user's shell environment and prepends | ||
| // any PATH entries to the current PATH. This ensures shell PATH takes precedence. | ||
| func LoadShellEnvironment() { | ||
| bashOutput := getEnvFromShell("bash") | ||
|
|
||
| // Prepend the Bash PATH now, as it is low priority | ||
| // We use the Bash shell env as well, since the more info scraping the better to help OSS scans | ||
| env := gotenv.Parse(strings.NewReader(bashOutput)) | ||
| if val, ok := env[PathEnvVarName]; ok { | ||
| UpdatePath(val, true) | ||
rrama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // this is applied at the end always, as it does not overwrite existing variables | ||
| // We use the Bash shell env as well, since the more info scraping the better to help OSS scans | ||
rrama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| defer func() { _ = gotenv.Apply(strings.NewReader(bashOutput)) }() //nolint:errcheck // we can't do anything with the error | ||
|
||
|
|
||
| env := gotenv.Parse(strings.NewReader(bashOutput)) | ||
| env = gotenv.Parse(strings.NewReader(bashOutput)) | ||
rrama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| specificShell, ok := env[ShellEnvVarName] | ||
| if ok { | ||
| fromSpecificShell := getEnvFromShell(specificShell) | ||
| _ = gotenv.Apply(strings.NewReader(fromSpecificShell)) //nolint:errcheck // we can't do anything with the error | ||
| } | ||
|
|
||
| // process config files | ||
| for _, file := range customConfigFiles { | ||
| if !filepath.IsAbs(file) { | ||
| file = filepath.Join(workingDirectory, file) | ||
| env = gotenv.Parse(strings.NewReader(fromSpecificShell)) | ||
| if val, ok := env[PathEnvVarName]; ok { | ||
| UpdatePath(val, true) | ||
| } | ||
| loadFile(file) | ||
| } | ||
| } | ||
|
|
||
| func loadFile(fileName string) { | ||
| // preserve path | ||
| previousPath := os.Getenv(PathEnvVarName) | ||
| // LoadConfigFiles loads environment variables from configuration files. | ||
| // Handles PATH and SDK environment variables specially. | ||
| // The resultant PATH is constructed as follows: | ||
| // 1. Config file PATH entries (highest precedence) | ||
| // 2. SDK bin directories (if SDK variables like JAVA_HOME, GOROOT are set by the config file) | ||
| // 3. Previous PATH entries (lowest precedence) | ||
| func LoadConfigFiles(customConfigFiles []string, workingDirectory string) { | ||
bastiandoetsch marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Capture current SDK environment variables and track their latest values | ||
| sdkVarNames := []string{"JAVA_HOME", "GOROOT"} | ||
| sdkValues := make(map[string]string) | ||
| for _, sdkVar := range sdkVarNames { | ||
| sdkValues[sdkVar] = os.Getenv(sdkVar) | ||
| // Unset the env var so we can capture if the config file sets it. | ||
| _ = os.Unsetenv(sdkVar) | ||
| } | ||
|
||
|
|
||
| // Process config files | ||
| for _, envFilePath := range customConfigFiles { | ||
| if !filepath.IsAbs(envFilePath) { | ||
| envFilePath = filepath.Join(workingDirectory, envFilePath) | ||
| } | ||
|
|
||
| // overwrite existing variables with file config | ||
| err := gotenv.OverLoad(fileName) | ||
| if err != nil { | ||
| return | ||
| // Preserve original PATH and unset it to ensure correct precedence order. | ||
| // Without unsetting, if the config file has no PATH, SDK bins will be appended to original PATH (wrong precedence). | ||
| previousPath := os.Getenv(PathEnvVarName) | ||
| _ = os.Unsetenv(PathEnvVarName) | ||
|
|
||
| // overwrite existing variables with file config | ||
| err := gotenv.OverLoad(envFilePath) | ||
| if err != nil { | ||
| // Restore PATH if config file loading failed. | ||
| _ = os.Setenv(PathEnvVarName, previousPath) | ||
| continue | ||
| } | ||
|
|
||
| // Check if SDK variables were set by this config file and append their bin directories | ||
| for _, sdkVar := range sdkVarNames { | ||
| currentValue := os.Getenv(sdkVar) | ||
| if currentValue != "" { | ||
| binPath := filepath.Join(currentValue, "bin") | ||
| UpdatePath(binPath, false) | ||
| // Update our tracking and unset for the next file | ||
| sdkValues[sdkVar] = currentValue | ||
| _ = os.Unsetenv(sdkVar) | ||
| } | ||
| } | ||
|
|
||
| // Add previous PATH to the end of the new | ||
| UpdatePath(previousPath, false) | ||
| } | ||
|
|
||
| // add previous path to the end of the new | ||
| UpdatePath(previousPath, false) | ||
| // Set final SDK values (latest from config files, or original if not overridden) | ||
| for _, sdkVar := range sdkVarNames { | ||
| if sdkValues[sdkVar] != "" { | ||
| _ = os.Setenv(sdkVar, sdkValues[sdkVar]) | ||
| } | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // guard against command injection | ||
| var shellWhiteList = map[string]bool{ | ||
| "bash": true, | ||
| "/bin/zsh": true, | ||
| "/bin/sh": true, | ||
| "/bin/fish": true, | ||
| "/bin/csh": true, | ||
| "/bin/ksh": true, | ||
| "/bin/bash": true, | ||
| "/usr/bin/zsh": true, | ||
| "/usr/bin/sh": true, | ||
| "/usr/bin/fish": true, | ||
| "/usr/bin/csh": true, | ||
| "/usr/bin/ksh": true, | ||
| "/usr/bin/bash": true, | ||
| "bash": true, | ||
| "/bin/zsh": true, | ||
| "/bin/sh": true, | ||
| "/bin/fish": true, | ||
| "/bin/csh": true, | ||
| "/bin/ksh": true, | ||
| "/bin/bash": true, | ||
| "/usr/bin/zsh": true, | ||
| "/usr/bin/sh": true, | ||
| "/usr/bin/fish": true, | ||
| "/usr/bin/csh": true, | ||
| "/usr/bin/ksh": true, | ||
| "/usr/bin/bash": true, | ||
| "/opt/homebrew/bin/bash": true, | ||
| } | ||
|
|
||
| func getEnvFromShell(shell string) string { | ||
|
|
||
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.
Maybe change the comment to read "We first read the Bash shell env to use as a fallback...." or similar?
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.
Better?