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

Shellcheck checker #267

Closed
swsnr opened this issue Nov 20, 2013 · 39 comments
Closed

Shellcheck checker #267

swsnr opened this issue Nov 20, 2013 · 39 comments

Comments

@swsnr
Copy link
Contributor

swsnr commented Nov 20, 2013

See http://www.shellcheck.net/about.html

@yasuyk
Copy link
Contributor

yasuyk commented Jan 29, 2014

I will try it. I'm going to name this syntax checker sh-shellcheck. what do you think about it?

@swsnr
Copy link
Contributor Author

swsnr commented Jan 29, 2014

Don't know. Seems fitting, but I'm not sure what Shellcheck actually checks. Is it Bash or POSIX sh?

@yasuyk
Copy link
Contributor

yasuyk commented Jan 29, 2014

Quoting http://www.shellcheck.net/about.html:

ShellCheck is a static analysis and linting tool for sh/bash scripts

It seems Bash and POSIX sh.

@swsnr
Copy link
Contributor Author

swsnr commented Jan 29, 2014

Cool :-) I wonder how it tells them apart, though. Is there some kind of an option?

@yasuyk
Copy link
Contributor

yasuyk commented Jan 29, 2014

See source code, https://github.com/koalaman/shellcheck/blob/0ec62390d57480a449afb7d450054a28de63fc8a/ShellCheck/Analytics.hs#L80-L87 .

It decide shell by shebang. Oh, shellcheck support zsh and ksh, also!

@swsnr
Copy link
Contributor Author

swsnr commented Jan 30, 2014

Wow. But what if the file being checked has no shebang? Can we force shellcheck to assume a specific shell?

Emacs' shell mode already guesses the shell, so shellcheck should not guess again, lest it guesses something different and reports false errors.

@swsnr
Copy link
Contributor Author

swsnr commented Jan 30, 2014

Sorry for asking all these questions, SVD letting you do all the research. I'm not at my computer, and reading source code on the phone just doesn't work well.

@yasuyk
Copy link
Contributor

yasuyk commented Jan 30, 2014

@lunaryorn No problem.

Wow. But what if the file being checked has no shebang?

If there is no sheebang, shellcheck regard shell as Bash.

Can we force shellcheck to assume a specific shell?

shellcheck currently has no option to assume a specific shell.

@swsnr
Copy link
Contributor Author

swsnr commented Jan 30, 2014

If there is no sheebang, shellcheck regard shell as Bash.

That's bad. My .zshrc doesn't have any Shebang, and it's surely no Bash code :)

shellcheck currently has no option to assume a specific shell.

Imho, we need such an option before we can think about adding this syntax checker. Should we open an issue at Shellcheck?

@yasuyk
Copy link
Contributor

yasuyk commented Jan 30, 2014

OK, I opened an issue.

@swsnr
Copy link
Contributor Author

swsnr commented Jan 30, 2014

@yasuyk Thank you. We'll see. I hope that the issue is closed quickly. Shellcheck looks really cool :)

@yasuyk
Copy link
Contributor

yasuyk commented Jan 30, 2014

Right! I had not known about Shellcheck until I see this issue, and Shellcheck is very useful after I used it. :-)

@koalaman
Copy link

koalaman commented Feb 3, 2014

Shellcheck's commit 4968e7d9f adds a -s for this, e.g. shellcheck -s ksh filename!

It supports bash, sh, ksh and zsh, along with some aliases like dash, ash and ksh93.

@yasuyk
Copy link
Contributor

yasuyk commented Feb 3, 2014

@koalaman Great! Thank you for the awesome work! When do you update version which includes this feature on hackage?

@swsnr
Copy link
Contributor Author

swsnr commented Feb 3, 2014

@yasuyk With this new option in Shellcheck, we should add a single syntax checker sh-shellcheck, along the following lines:

(defconst flycheck-shellcheck-supported-shells '((bash . "bash") (sh . "sh") (ksh88 . "ksh") (zsh . "zsh")))

(flycheck-define-checker sh-shellcheck
  :command ("shellcheck" "-s" (eval (cdr (assq sh-shell flycheck-shellcheck-supported-shells))) source)
  :modes sh-mode
  :predicate (lambda () (assq sh-shell flycheck-shellcheck-supported-shells))
  :error-patterns …)

As part of this PR, we should rename all existing shell script checkers to follow the pattern sh-<SHELLTYPE>, e.g. sh-zsh, sh-bash, etc. The current sh-* checkers would go as sh-posix-dash and sh-posix-bash then. That'd easy chaining to shell check.

What do you think?

@yasuyk
Copy link
Contributor

yasuyk commented Feb 3, 2014

Excellent! I agree with you. 👍

@yasuyk
Copy link
Contributor

yasuyk commented Feb 4, 2014

@lunaryorn Shall I rename all existing shell script checkers?

@swsnr
Copy link
Contributor Author

swsnr commented Feb 4, 2014

I'm already at it.

@yasuyk
Copy link
Contributor

yasuyk commented Feb 4, 2014

OK, then I will tackle Spellcheck checker.

@swsnr
Copy link
Contributor Author

swsnr commented Feb 4, 2014

@yasuyk See 049babb

@swsnr
Copy link
Contributor Author

swsnr commented Feb 4, 2014

Since Shellcheck now supports ksh88, we can simplify it a bit imho. Also I noticed that Shellcheck can output Checkstyle XML, so we do not need custom error patterns. We can just use the builtin flycheck-parse-checkstyle parser:

(defconst flycheck-shellcheck-supported-shells '(bash sh ksh88 zsh)))

(flycheck-define-checker sh-shellcheck
  :command ("shellcheck" "-f" "checkstyle" "-s" (eval (symbol-name sh-shell)) source)
  :modes sh-mode
  :predicate (lambda () (memq sh-shell flycheck-shellcheck-supported-shells))
  :error-parser flycheck-parse-checkstyle)

@yasuyk
Copy link
Contributor

yasuyk commented Feb 4, 2014

Wow, quick work!

@swsnr
Copy link
Contributor Author

swsnr commented Feb 4, 2014

@yasuyk Please take a look at the shell-check branch. I added the necessary code to install shellcheck on the VM, and I also added the syntax checker itself, including the chaining from the shell checkers. Consider it as a draft of this checker.

I didn't test it, nor add any tests, though. Let me know what you think, and whether you can work with these changes.

@yasuyk
Copy link
Contributor

yasuyk commented Feb 4, 2014

@lunaryorn OK. I have written test. I will merge tests to shell-check brantch.

@yasuyk
Copy link
Contributor

yasuyk commented Feb 4, 2014

@lunaryorn I added test for shel-check. make test on OS X is passed. make vagrant-test is in process now. My Vagrant environment is very slow..

@yasuyk
Copy link
Contributor

yasuyk commented Feb 4, 2014

make vagrant-test has been done, and test cases for sh-* checkers are passed.

@swsnr
Copy link
Contributor Author

swsnr commented Feb 4, 2014

@yasuyk By the way, if you add a new syntax checker, you usually don't need to run the entire test suite.

It's ok to just run the tests for the specific language and the documentation tests, i.e. cask exec ert-runner -t documentation,language-sh for shell scripts, or cask exec ert-runner -t documentation,language-python for Python.

Syntax checkers for other languages aren't affected by adding a new checker, and the rest of the tests runs on Travis CI.

@yasuyk
Copy link
Contributor

yasuyk commented Feb 4, 2014

I see! I will do so from next time.

@swsnr swsnr closed this as completed in e785d79 Feb 4, 2014
@swsnr
Copy link
Contributor Author

swsnr commented Feb 4, 2014

@yasuyk Merged. Thank you for your help.

@koalaman
Copy link

koalaman commented Feb 5, 2014

If one of you will take a screenshot similar to the ones on http://www.shellcheck.net/about.html , I can upload it and link to flycheck

@yasuyk
Copy link
Contributor

yasuyk commented Feb 5, 2014

@lunaryorn Great! Thanks.

@yasuyk
Copy link
Contributor

yasuyk commented Feb 5, 2014

I taked a screenshot with Spolsky theme and Source Code Pro font.

@lunaryorn @koalaman What do you say?

flycheck

@swsnr
Copy link
Contributor Author

swsnr commented Feb 5, 2014

@yasuyk @koalaman I'd like to have Flycheck's error list in the screenshot as well, so I did another one with Zenburn and Source Code Pro:
bildschirmfoto 2014-02-05 um 12 42 15

It's slightly larger, but imho better conveys how Flycheck would look like when being used in a real editing session, e.g. with window borders, etc.

@yasuyk
Copy link
Contributor

yasuyk commented Feb 5, 2014

@lunaryorn Looks good. 👍

@koalaman
Copy link

koalaman commented Feb 8, 2014

@yasuyk @lunaryorn Great, thanks! Small is good, It's basically just a thumbnail to link to the project.

@swsnr
Copy link
Contributor Author

swsnr commented Feb 8, 2014

@yasuyk @koalaman I used Shellcheck for the last days, and from my personal experience, I'd like to disable it for Zsh again.

It's reporting too many false positives, and often entirely fails to parse Zsh code. For instance, it chokes on glob modifiers (e.g. /foo/*/bar(N)), and doesn't handle many special expansion parameters (e.g. $+commands[foo], for which it reports “$ is not used specially and should therefore be escaped.”). Apparently it lacks support for many advanced (seemingly non-bash, non-posix) features of Zsh.

Comments?

@yasuyk
Copy link
Contributor

yasuyk commented Feb 8, 2014

@lunaryorn You can say that again. ShellCheck report errors in my correct .zshrc.

@koalaman
Copy link

koalaman commented Feb 8, 2014

Sounds reasonable. I haven't put a lot of work into advanced zsh syntax, and there haven't been any bug reports.

@swsnr
Copy link
Contributor Author

swsnr commented Feb 9, 2014

@koalaman Try Shellcheck on prezto sources. Some of its modules use fairly advanced expressions, which Shellcheck currently fails to parse. I think it'd serve quite well as a general test case for Zsh support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants