-
Notifications
You must be signed in to change notification settings - Fork 13
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 5 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,119 @@ 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 user and local configuration. | ||
| // First Bash's env is read (as a fallback), then the user's preferred SHELL's env is read, then the configuration files. | ||
| // The Bash env PATH is appended to the existing PATH (as a fallback), any other new PATH read is prepended (preferential). | ||
| // See LoadShellEnvironment and LoadConfigFiles. | ||
| func LoadConfiguredEnvironment(customConfigFiles []string, workingDirectory string) { | ||
| bashOutput := getEnvFromShell("bash") | ||
| LoadShellEnvironment() | ||
|
|
||
| // this is applied at the end always, as it does not overwrite existing variables | ||
| defer func() { _ = gotenv.Apply(strings.NewReader(bashOutput)) }() //nolint:errcheck // we can't do anything with the error | ||
| LoadConfigFiles(customConfigFiles, workingDirectory) | ||
| } | ||
|
|
||
| env := gotenv.Parse(strings.NewReader(bashOutput)) | ||
| specificShell, ok := env[ShellEnvVarName] | ||
| if ok { | ||
| fromSpecificShell := getEnvFromShell(specificShell) | ||
| _ = gotenv.Apply(strings.NewReader(fromSpecificShell)) //nolint:errcheck // we can't do anything with the error | ||
| // LoadShellEnvironment loads the user's shell environment with special handling of PATHs. | ||
| // First Bash's env is read (as a fallback), then the user's preferred SHELL's env is read. | ||
| // The Bash env PATH is appended to the existing PATH (as a fallback), the user's preferred SHELL's env PATH is prepended (preferential). | ||
| func LoadShellEnvironment() { | ||
| bashEnvOutput := getEnvFromShell("bash") | ||
|
|
||
| // We first append the Bash PATH as a fallback for if the user's SHELL's env fails to read / parse. | ||
| // Note: We do this as the more info scraping the better to help ensure we can find binaries for OSS scans. | ||
| bashEnv := gotenv.Parse(strings.NewReader(bashEnvOutput)) | ||
| if val, ok := bashEnv[PathEnvVarName]; ok { | ||
| UpdatePath(val, false) | ||
| } | ||
|
|
||
| // process config files | ||
| for _, file := range customConfigFiles { | ||
| if !filepath.IsAbs(file) { | ||
| file = filepath.Join(workingDirectory, file) | ||
| // this is applied at the end always, as we do not want it to overwrite existing variables from the user's preferred SHELL | ||
| defer func() { _ = gotenv.Apply(strings.NewReader(bashEnvOutput)) }() //nolint:errcheck // we can't do anything with the error | ||
|
|
||
| preferredShell, ok := bashEnv[ShellEnvVarName] | ||
| if ok { | ||
| preferredShellEnvOutput := getEnvFromShell(preferredShell) | ||
| _ = gotenv.Apply(strings.NewReader(preferredShellEnvOutput)) //nolint:errcheck // we can't do anything with the error | ||
|
|
||
| preferredShellEnv := gotenv.Parse(strings.NewReader(preferredShellEnvOutput)) | ||
| if val, ok := preferredShellEnv[PathEnvVarName]; ok { | ||
| UpdatePath(val, true) | ||
| } | ||
| loadFile(file) | ||
| } | ||
| } | ||
|
|
||
| func loadFile(fileName string) { | ||
| // preserve path | ||
| previousPath := os.Getenv(PathEnvVarName) | ||
| // LoadConfigFiles loads environment variables from configuration files. | ||
| // With special handling for PATH and SDK environment variables. | ||
| // 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) | ||
| } | ||
|
||
|
|
||
| // overwrite existing variables with file config | ||
| err := gotenv.OverLoad(fileName) | ||
| if err != nil { | ||
| return | ||
| // Process config files | ||
| for _, envFilePath := range customConfigFiles { | ||
| if !filepath.IsAbs(envFilePath) { | ||
| envFilePath = filepath.Join(workingDirectory, envFilePath) | ||
| } | ||
|
|
||
| // 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.
nitpick: this could be moved into the deferred function