halcyon / asdf-java

A Java plugin for asdf-vm.
MIT License
465 stars 85 forks source link

Error when PROMPT_COMMAND already ends with semicolon #91

Closed janpieper closed 3 years ago

janpieper commented 4 years ago

When using direnv and set-java-home.bash, the PROMPT_COMMAND will be set to:

_direnv_hook;; prompt_command

This causes bash to cry, because it does not like ;; :cry:

-bash: PROMPT_COMMAND: line 0: syntax error near unexpected token `;;'
-bash: PROMPT_COMMAND: line 0: `_direnv_hook;; prompt_command'        

I thought of checking whether PROMPT_COMMAND already ends with ; to avoid adding another ;, but I am not a bash expert. Maybe there is a better way to solve this issue.

I am currently using this as a workaround:

export PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND} :}"

. ~/.asdf/plugins/java/set-java-home.sh

PROMPT_COMMAND will be set to _direnv_hook; :; prompt_command. See this comment at Stackoverflow to understand what : stands for.

olbrich commented 4 years ago

@janpieper I had a similar problem and tried adding asdf before direnv in my .bash_profile and that seems to have worked. YMMV

onionhead0708 commented 4 years ago

I got the same issue when using the powerline. i to put the asdf config before the powerline config in the .bashrc (as @olbrich mentioned) and worked.

jinmiaoluo commented 3 years ago

My PROMPT_COMMAND variable:

_direnv_hook;(_z --add "$(command pwd -P 2>/dev/null)" 2>/dev/null &); asdf_java_prompt_command;

How direnv deals with PROMPT_COMMAND variable:

_direnv_hook() {
  local previous_exit_status=$?;
  trap -- '' SIGINT;
  eval "$("/usr/bin/direnv" export bash)";
  trap - SIGINT;
  return $previous_exit_status;
};
if ! [[ "${PROMPT_COMMAND:-}" =~ _direnv_hook ]]; then
  PROMPT_COMMAND="_direnv_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
fi

direnv will add _direnv_hook; before the old content in PROMPT_COMMAND variable.

How command z deals with PROMPT_COMMAND variable:

[ "$_Z_NO_PROMPT_COMMAND" ] || {
# populate directory list. avoid clobbering other PROMPT_COMMANDs.
  grep "_z --add" <<< "$PROMPT_COMMAND" >/dev/null || {
    PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''(_z --add "$(command pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null &);'
  }
}

command z will add (_z --add "$(command pwd -P 2>/dev/null)" 2>/dev/null &); after the old content in PROMPT_COMMAND variable with ; suffix.

How can I fix this question?

First, remove export in set-java-home.bash.

change your .bashrc, let asdf java bash script source first, before direnv and command z(and any other commands will change PROMPT_COMMAND variable).

or:

let command z source first, and then use the patch in this commit, and then source the asdf java home bash script.

jinmiaoluo commented 3 years ago

Maybe we can change set-java-home.bash to:

if ! [[ "${PROMPT_COMMAND:-}" =~ prompt_command ]]; then
  PROMPT_COMMAND="prompt_command${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
fi
jinmiaoluo commented 3 years ago

BTW, we should use _asdf_java_prompt_command instead of prompt_command, which would be more specific to name a function from asdf-java plugin.