You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 javaUsing 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.
The text was updated successfully, but these errors were encountered:
Bug report
When autoenv is turned on,
sdk env clear
is not run when exiting the current env's directory.To reproduce
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.
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 wasprojectg
matched the regular expressionproject[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
andproject-12
and the result will be the same.Possible fix
Change the line to
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.
The text was updated successfully, but these errors were encountered: