gnachman / iterm2-website

Website for iTerm2
http://iterm2.com/
GNU General Public License v2.0
80 stars 66 forks source link

Syntax Error in ZSH Shell Integration #65

Closed mrk-han closed 4 years ago

mrk-han commented 4 years ago
/Users/username/.iterm2_shell_integration.zsh: line 36: syntax error near unexpected token `}'
/Users/username/.iterm2_shell_integration.zsh: line 36: `      }'`

In Appium there is a method io.appium.java_client.service.local.AppiumServiceBuilder.findNodeInCurrentFileSystem(AppiumServiceBuilder.java:157)

            if (StringUtils.isBlank(instancePath) || !(defaultAppiumNode =
                new File(instancePath + File.separator
                    + APPIUM_FOLDER)).exists()) {
                String errorOutput = commandLine.getStdOut();
                throw new InvalidServerInstanceException(ERROR_NODE_NOT_FOUND,
                    new IOException(errorOutput));
            }

When executed in a test, this method executed my zsh shell integration script for some reason (maybe related to an Appium bug) and found

/Users/username/.iterm2_shell_integration.zsh: line 36: syntax error near unexpected token `}'
/Users/username/.iterm2_shell_integration.zsh: line 36: `      }'`

So my tests failed.

The ShellCheck Plugin found suggested "You need at least one command here. Use 'true;' as a no-op. when I analyzed the script and reported it as a syntax error.

So finally, the method should look like this:

    # Users can write their own version of this method. It should call
    # iterm2_set_user_var but not produce any other output.
    # e.g., iterm2_set_user_var currentDirectory $PWD
    # Accessible in iTerm2 (in a badge now, elsewhere in the future) as
    # \(user.currentDirectory).
    whence -v iterm2_print_user_vars > /dev/null 2>&1
    if [ $? -ne 0 ]; then
      iterm2_print_user_vars() { true;}
    fi

Also, is there a reason we don't want a shebang at the top?

gnachman commented 4 years ago

Thanks for pointing this out. zsh scripting is hard. Commit 928718f adds true.

Also, is there a reason we don't want a shebang at the top?

If you run it in a subshell it won't work, so you have to source it.