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

ALE tells ShellCheck to treat files with the ".bash" extension as sh files when vim-polyglot is enabled #3988

Open
openjck opened this issue Nov 19, 2021 · 1 comment
Labels

Comments

@openjck
Copy link

openjck commented Nov 19, 2021

Information

VIM version

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Nov 08 2021 14:19:29)
Included patches: 1-2434, 3402-3403, 3409, 3428, 3487, 3564, 3581-3582

Operating System: Ubuntu 21.04

What went wrong

When ALE and vim-polyglot are both enabled, ALE tells ShellCheck that files ending with a .bash extension are of type sh.

Reproducing the bug

Steps to reproduce

  1. Install ShellCheck
  2. mv ~/.vim ~/.vim.bak.ale-issue-3988.$(date '+%s')
  3. git clone --depth 1 https://github.com/dense-analysis/ale.git ~/.vim/pack/plugins/start/ale
  4. git clone --depth 1 https://github.com/sheerun/vim-polyglot ~/.vim/pack/plugins/start/vim-polyglot
  5. vim example.bash
  6. Enter the following contents:
function example() {
  echo 'example'
}

Actual results

:ALEInfo reports that option -s sh is passed to ShellCheck. ShellCheck reports the following error, which only applies to POSIX sh:

SC2112: 'function' keyword is non-standard. Delete it.

Expected results

:ALEInfo reports that option -s bash is passed to ShellCheck and ShellCheck reports no errors.

:ALEInfo

 Current Filetype: sh
Available Linters: ['bashate', 'cspell', 'language_server', 'shell', 'shellcheck']
  Enabled Linters: ['bashate', 'cspell', 'language_server', 'shell', 'shellcheck']
  Ignored Linters: []
 Suggested Fixers: 
  'remove_trailing_lines' - Remove all blank lines at the end of a file.
  'shfmt' - Fix sh files with shfmt.
  'trim_whitespace' - Remove all trailing whitespace characters at the end of every line.
 Linter Variables:

let g:ale_sh_bashate_executable = 'bashate'
let g:ale_sh_bashate_options = ''
let g:ale_sh_language_server_executable = 'bash-language-server'
let g:ale_sh_language_server_use_global = 0
let g:ale_sh_shell_default_shell = 'bash'
let g:ale_sh_shellcheck_change_directory = 1
let g:ale_sh_shellcheck_dialect = 'auto'
let g:ale_sh_shellcheck_exclusions = ''
let g:ale_sh_shellcheck_executable = 'shellcheck'
let g:ale_sh_shellcheck_options = ''
 Global Variables:

let g:ale_cache_executable_check_failures = v:null
let g:ale_change_sign_column_color = 0
let g:ale_command_wrapper = ''
let g:ale_completion_delay = v:null
let g:ale_completion_enabled = 0
let g:ale_completion_max_suggestions = v:null
let g:ale_disable_lsp = 0
let g:ale_echo_cursor = 1
let g:ale_echo_msg_error_str = 'Error'
let g:ale_echo_msg_format = '%code: %%s'
let g:ale_echo_msg_info_str = 'Info'
let g:ale_echo_msg_warning_str = 'Warning'
let g:ale_enabled = 1
let g:ale_fix_on_save = 0
let g:ale_fixers = {}
let g:ale_history_enabled = 1
let g:ale_history_log_output = 1
let g:ale_keep_list_window_open = 0
let g:ale_lint_delay = 200
let g:ale_lint_on_enter = 1
let g:ale_lint_on_filetype_changed = 1
let g:ale_lint_on_insert_leave = 1
let g:ale_lint_on_save = 1
let g:ale_lint_on_text_changed = 'normal'
let g:ale_linter_aliases = {}
let g:ale_linters = {}
let g:ale_linters_explicit = 0
let g:ale_linters_ignore = {}
let g:ale_list_vertical = 0
let g:ale_list_window_size = 10
let g:ale_loclist_msg_format = '%code: %%s'
let g:ale_max_buffer_history_size = 20
let g:ale_max_signs = -1
let g:ale_maximum_file_size = v:null
let g:ale_open_list = 0
let g:ale_pattern_options = v:null
let g:ale_pattern_options_enabled = v:null
let g:ale_root = {}
let g:ale_set_balloons = 0
let g:ale_set_highlights = 1
let g:ale_set_loclist = 1
let g:ale_set_quickfix = 0
let g:ale_set_signs = 1
let g:ale_sign_column_always = 0
let g:ale_sign_error = '>>'
let g:ale_sign_info = '--'
let g:ale_sign_offset = 1000000
let g:ale_sign_style_error = '>>'
let g:ale_sign_style_warning = '--'
let g:ale_sign_warning = '--'
let g:ale_sign_highlight_linenrs = 0
let g:ale_statusline_format = v:null
let g:ale_type_map = {}
let g:ale_use_global_executables = v:null
let g:ale_virtualtext_cursor = 0
let g:ale_warn_about_trailing_blank_lines = 1
let g:ale_warn_about_trailing_whitespace = 1
  Command History:

(executable check - failure) bashate
(executable check - failure) cspell
(executable check - success) sh
(finished - exit code 0) ['/bin/bash', '-c', 'sh -n ''/tmp/vZUaM5c/1/example.bash''']

<<<NO OUTPUT RETURNED>>>

(executable check - success) shellcheck
(finished - exit code 0) ['/bin/bash', '-c', 'cd ''/home/john'' && ''shellcheck'' --version']

<<<OUTPUT STARTS>>>
ShellCheck - shell script analysis tool
version: 0.7.1
license: GNU General Public License, version 3
website: https://www.shellcheck.net
<<<OUTPUT ENDS>>>

(finished - exit code 0) ['/bin/bash', '-c', 'cd ''/home/john'' && ''shellcheck'' -s sh -x -f gcc - < ''/tmp/vZUaM5c/2/example.bash''']

<<<NO OUTPUT RETURNED>>>

(executable check - failure) bashate
(executable check - failure) cspell
(finished - exit code 2) ['/bin/bash', '-c', 'sh -n ''/tmp/vZUaM5c/3/example.bash''']

<<<OUTPUT STARTS>>>
/tmp/vZUaM5c/3/example.bash: 1: Syntax error: "(" unexpected
<<<OUTPUT ENDS>>>

(finished - exit code 1) ['/bin/bash', '-c', 'cd ''/home/john'' && ''shellcheck'' -s sh -x -f gcc - < ''/tmp/vZUaM5c/4/example.bash''']

<<<OUTPUT STARTS>>>
-:1:1: note: The mentioned syntax error was in this function. [SC1009]
-:1:20: error: Couldn't parse this brace group. Fix to allow more checks. [SC1073]
-:2:1: error: You need at least one command here. Use 'true;' as a no-op. [SC1055]
-:2:2: error: Unexpected keyword/token. Fix any mentioned problems and try again. [SC1072]
<<<OUTPUT ENDS>>>

(executable check - failure) bashate
(executable check - failure) cspell
(finished - exit code 2) ['/bin/bash', '-c', 'sh -n ''/tmp/vZUaM5c/5/example.bash''']

<<<OUTPUT STARTS>>>
/tmp/vZUaM5c/5/example.bash: 1: Syntax error: "(" unexpected
<<<OUTPUT ENDS>>>

(finished - exit code 1) ['/bin/bash', '-c', 'cd ''/home/john'' && ''shellcheck'' -s sh -x -f gcc - < ''/tmp/vZUaM5c/6/example.bash''']

<<<OUTPUT STARTS>>>
-:1:1: warning: 'function' keyword is non-standard. Delete it. [SC2112]
<<<OUTPUT ENDS>>>

Other notes

This works as expected if vim-polyglot is not enabled when Vim starts up, e.g., if the steps above are repeated and step 4 is skipped.

@openjck openjck added the bug label Nov 19, 2021
@openjck openjck changed the title ALE tells ShellCheck that files with the "bash" extension are POSIX sh files when vim-polyglot is installed ALE tells ShellCheck to treat files with the ".bash" extension as sh files when vim-polyglot is enabled Nov 19, 2021
@hsanson
Copy link
Contributor

hsanson commented Nov 25, 2021

ALE determines the shell dialect in this order:

  1. Check for shellcheck directive (see https://github.com/koalaman/shellcheck/wiki/Directive#shell)
  2. Check for shebang #! line at the start of the script.
  3. Check vim's local buffer variables (e.g. b:is_bash, b:is_sh, and b:is_kornshell). Set by vim ftdetect.
  4. Check vim's global variables (e.g. g:is_bash, g:is_sh, and g:is_kornshell). Set by users to override vim ftdetect.
  5. Check the filetype.

In your example 1 and 2 do not apply. For 3 I could test that b:is_bash is properly set when vim-polyglot is not installed but not defined if vim-polyglot is installed. Somehow vim-polyglot is messing with vim's default auto-detect logic and undefining these buffer variables.

You can test this by running ":echo b:is_bash" with the vim-polyglot plugin installed and when not installed.

Simple solution is to add a shellcheck directive to the script so ALE properly detects it as bash. Also would be good idea to report an issue in vim-polyglot to investigate the problem on their side.

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

2 participants