Skip to content
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

Bug: sdk env clear is not always run when autoenv is turned on #1255

Open
rprcz opened this issue Nov 17, 2023 · 0 comments
Open

Bug: sdk env clear is not always run when autoenv is turned on #1255

rprcz opened this issue Nov 17, 2023 · 0 comments
Labels

Comments

@rprcz
Copy link

rprcz commented Nov 17, 2023

Bug report
When autoenv is turned on, sdk env clear is not run when exiting the current env's directory.

To reproduce

$ echo "sdkman_auto_env=true" >> ~/.sdkman/etc/config # turn on auto env
$ mkdir "project[tag]"
$ mkdir "projectg
$ echo "java=21-tem" >> "project[tag]"/.sdkmanrc # make sure "21-tem" is not your default for java
$ cd "project[tag]"

Using java version 21-tem in this shell.
$ cd ../projectg
$ sdk current java

Using java version 21-tem
$ # here the java version should have been switched to default version, but it was not

System info
Linux: Fedora release 39 (Thirty Nine) x86_64

$ bash --version
GNU bash, version 5.2.21(1)-release (x86_64-redhat-linux-gnu)
[...]
$ zsh --version
zsh 5.9 (x86_64-redhat-linux-gnu)
$ sdk version

SDKMAN!
script: 5.18.2
native: 0.4.3

Root cause
Each time the current directory is changed in the shell, SDKMAN's hook function is run to detect whether the new directory means the user has left the previous environment. The code (for bash) is here.

[[ ! $PWD =~ ^$SDKMAN_ENV ]]; 

The SDKMAN_ENV variable is used as a pattern. It is a path to the directory containing .sdkman.rc file and it can contain characters with special meaning when used in context of a regular expression (e.g. .[]^$() and others). What happened in my example was projectg matched the regular expression project[tag].

Furthermore, the possibility that the old directory name is a prefix of the new one is also not covered. It can cause the same error even without any special characters. You can try the steps to reproduce the problem, just change the directory names to project-1 and project-12 and the result will be the same.

Possible fix
Change the line to

if [[ -n "$SDKMAN_ENV" ]] && [[ ! "$PWD/" =~ ^"$SDKMAN_ENV/" ]]; then

Please note that it seems zsh code has the same problem. I'm not sure whether this fix is applicable for zsh as well, but for bash it seems to work. Quoting the variable seems to force an exact string match. The trailing slashes cover the prefix case.

I haven't bothered creating a pull request as it's overkill for an one-liner, but I can do it if needed.

@rprcz rprcz added the bug label Nov 17, 2023
@rprcz rprcz changed the title Bug: [A concise title] Bug: sdk env clear is not always run when autoenv is turned on Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant