sharkdp / bat

A cat(1) clone with wings.
Apache License 2.0
49.28k stars 1.25k forks source link

Panic: "`Option::unwrap()` on a `None` value" for Bash script #704

Closed eth-p closed 4 years ago

eth-p commented 4 years ago

Syntect tries to unwrap a None value when highlighting a Bash script. A file that causes this to happen can be found here.

This occurs on macOS 10.14.6 with bat 1.12.1.

Backtrace:

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/libcore/option.rs:347:21
stack backtrace:
   0: std::sys_common::backtrace::print
   1: std::panicking::default_hook::{{closure}}
   2: std::panicking::default_hook
   3: std::panicking::rust_panic_with_hook
   4: std::panicking::continue_panic_fmt
   5: rust_begin_unwind
   6: core::panicking::panic_fmt
   7: core::panicking::panic
   8: <syntect::highlighting::highlighter::HighlightIterator as core::iter::traits::iterator::Iterator>::next
   9: <syntect::highlighting::highlighter::HighlightIterator as core::iter::traits::iterator::Iterator>::next
  10: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter
  11: syntect::easy::HighlightLines::highlight
  12: <bat::printer::InteractivePrinter as bat::printer::Printer>::print_line
  13: bat::controller::Controller::run
  14: bat::main
  15: std::rt::lang_start::{{closure}}
  16: std::panicking::try::do_call
  17: __rust_maybe_catch_panic
  18: std::rt::lang_start_internal
  19: main
sharkdp commented 4 years ago

I can not reproduce this with bat-0.12.1 or the current master (on Linux).

Could you please show the output of this debug script?

set -x

bat --version
bat --config-file
bat --cache-dir
less --version

bat "$(bat --config-file)"
ls "$(bat --cache-dir)"

set +x

echo "BAT_PAGER = '$BAT_PAGER'"
echo "BAT_CONFIG_PATH = '$BAT_CONFIG_PATH'"
echo "BAT_STYLE = '$BAT_STYLE'"
echo "BAT_THEME = '$BAT_THEME'"
echo "BAT_TABS = '$BAT_TABS'"
echo "PAGER = '$PAGER'"
echo "LESS = '$LESS'"
eth-p commented 4 years ago

I played around with it a bit, and this seems to only happen when using a cache built with bat cache --build --source=/path/to/bat/repo/assets. After a bit of investigating, removing the ShellScript folder from the Packages submodule appears to prevent this from happening.

Could there be an issue with Syntect and the ShellScript syntax?

sharkdp commented 4 years ago

I still can not reproduce this.

Are you using master with an up-to-date submodule state?

eth-p commented 4 years ago

I tried again with 9b1930b2b303309374502dc98989322a8ddd15be, and I still run into this issue.

Here's my output from git submodule status:

 1ce5f90d444d80b12af41bc051507e914730d4ef syntaxes/AWK (heads/master)
 d0089991e5db83a6cb2c505b2ea3548f559ed2af syntaxes/AsciiDoc (heads/master)
 08f33d80ded128a708d83017148dba52ebdfb04a syntaxes/Assembly (ARM) (v1.0.0)
 aba96a0862369e9f960bb63a38e2d7563ea6475e syntaxes/CMake (heads/master)
 4786d037a761bc45e516f6b0624a839919ec6d05 syntaxes/CSV (1.0.8-18-g4786d03)
 1c054d9b3684d4f8278f934099614674217fea4d syntaxes/Cabal (1.2.5-246-g1c054d9)
 2f96cec2cae2f1efee17a2f82e82f7175290c6f6 syntaxes/Crystal (v0.0.7-12-g2f96cec)
 d891fb36c98ca0b111a35cba109b05a16b6c4b83 syntaxes/Dart (1.5.1-16-gd891fb3)
 9e9a518aed93031042c54710f8f02c839301de26 syntaxes/Docker (v1.2.0-30-g9e9a518)
 a1c917633d3f3c4222cdcfac5b46831abff54d65 syntaxes/DotENV (2.0.6)
 044d9af8a1cc5c4da234e60f4a22ce8603b2d772 syntaxes/Elixir (v1.1.1)
 dfbb8bdc9a8403092c884d3eb893a270538cfa72 syntaxes/Elm (0.22.0-8-gdfbb8bd)
 c18616d6bd55b188672276da837837070170d967 syntaxes/FSharp (heads/master)
 0f7d31b43e260bb3c7fe3fbc9d8f0024c1b39cf4 syntaxes/Fish (st3-3.0.0)
 66ce8713d8915e74a2822c6f052cc9d3449cde0d syntaxes/GraphQL (1.5.0)
-77def406d70b90dff33d006478198b729e23d22c syntaxes/HTML (Twig)
 8dd3e316ada5cc8bffd3982bc2a9384345729f34 syntaxes/INI (heads/master)
 a9a908fe1d39ec84893e6edcbb146f09e9e60cc6 syntaxes/JavaScript (Babel) (v8.6.3-41-ga9a908f)
 45355633d17ee562481ca1d2ad0e3502c238f58a syntaxes/Jinja2 (heads/master)
 e2c917ed7d6020e2ba6c18b06c7278c094db03f7 syntaxes/Jsonnet (v0.2.4)
 0ca4cc29907e9476cb603fed00d1d5fd9dc13c9c syntaxes/Julia (heads/master)
 aeeed2780b04aea3d293c547c24cae27cafef0c5 syntaxes/Kotlin (kotlin-sublime-package-1.0-35-gaeeed27)
 4eada94009ae7185b1f518d3f4b0d4db2b2b217c syntaxes/LESS (2.6.1-1-g4eada94)
 ebf06571f3cc2b30a7f88bbd4eb371b72ce828c2 syntaxes/Nix (v2.1.0-9-gebf0657)
 4976d8f84eeecd94df7da872bf404c125df04c73 syntaxes/Org mode (v1.0.0-16-g4976d8f)
 71fd0ec9e4b42c73e560d088a05ccc14ef0f54fc syntaxes/Packages (v3189-232-g71fd0ec9)
 12b7d7257eb493e45a9af0af9094ec0c2a996712 syntaxes/PowerShell (12b7d72)
 726e21d74dac23cbb036f2fbbd626decdc954060 syntaxes/Protobuf (2.0.0-2-g726e21d)
 2a224343d2151b2896b6d9c48343919c06d1e695 syntaxes/Puppet (1.7.0)
 5acebc18503697be09df047591964e68e80fcf8e syntaxes/PureScript (1.2.3-1-g5acebc1)
 8e5a349a3ea64f2e75d224f6ed1dcef6b9fbc525 syntaxes/Robot (st3-6.3.0-3-g8e5a349)
 0e8e0f9d2ffa56981fcb6a0beee3648e5e27c851 syntaxes/SCSS_Sass (2.3.10-1-g0e8e0f9)
 f8a8cf90b1185acb63127dfdfe53379a6a16c0fd syntaxes/SLS (0.1.0-19-gf8a8cf9)
 3ec51e0bca39bf60cab7f96cecfc5d003c3ea59b syntaxes/Strace (0.1.0-2-g3ec51e0)
 fef17119ceef28a3c4b8cf72268d6192b595365d syntaxes/Swift (v0.0.9)
 7628d4e0b89d3b4f0dcc014bfb4a3e3433f5133a syntaxes/Syslog (heads/master)
 0f14b7caf3c775a5d18551a9563a9debdb10c9a9 syntaxes/TOML (v1.0.4-28-g0f14b7c)
 c683495c6414a172c58a39beaa952c822cbbcc84 syntaxes/Terraform (0.1.0-56-gc683495)
 8bfce299be3b899e996663f8a27e2013bd2e65b8 syntaxes/TypeScript (3.7.2)
 532e87c1b40114be14ccfe382a141247daa54be4 syntaxes/Verilog (heads/master)
 ed40c3bc814cd0c8107dbbb8fcd95946b33217f0 syntaxes/VimL (v0.1.4)
 21250a7117655b0525821237b98b882dcc231350 syntaxes/hosts (heads/master)
 775e6dd62aa16f7b28632a200882ff6ad0315b01 syntaxes/requirementstxt (st3-0.3.1-2-g775e6dd)
 17e23d7d38e5cf1c63e5c0242514965ffb080499 syntaxes/ssh-config (st3-3.1.0)
 04f30d4b831232ea23ffceb5b2da61e12dae5db6 syntaxes/varlink (heads/master)
 ca6a329cfda8307449d405b70f8fab34b8fd23b5 themes/1337-Scheme (v2.0)
 174bd535dafffd68df7a6a8fd21312aff32600a4 themes/DarkNeon (1.0-22-g174bd53)
 8e0f6fa5b59d196658a22288f519fd8320de4c87 themes/TwoDark (1.1.1)
 b7e8961afa4c11d620ad26abe28d76929c7ff90b themes/dracula-sublime (v1.4.2)
 508740b2430c3c3a9e785fc93ee1d7c6f233af53 themes/github-sublime-theme (heads/master)
 fdcbffe3175c29bb78d19dcaf9b2a456482fcf1e themes/onehalf (0.1.3-13-gfdcbffe)
 0ca4e75291515c4d47e2d455e598e03e0dc53745 themes/sublime-monokai-extended (heads/master)
 70343201f1d7539adbba3c79e2fe81c2559a0431 themes/sublime-snazzy (heads/master)
 b7f27e2f6567813991496b387fe902a3a2ef0747 themes/zenburn (v2.5.0)

And the debug script from above:

+ bat --version
bat 0.12.1
+ bat --config-file
/Users/User/.config/bat/config
+ bat --cache-dir
/Users/User/.cache/bat
+ less --version
less 530 (PCRE regular expressions)
Copyright (C) 1984-2017  Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
++ bat --config-file
+ bat /Users/User/.config/bat/config
───────┬──────────────────────────────────────────────────────────────────────────────
       │ File: /Users/User/.config/bat/config
───────┼──────────────────────────────────────────────────────────────────────────────
   1   │ --tabs 4
   2   │
   3   │ # Syntaxes
   4   │ #--map-syntax From:to
   5   │
───────┴──────────────────────────────────────────────────────────────────────────────
++ bat --cache-dir
+ ls /Users/User/.cache/bat
syntaxes.bin    themes.bin
+ set +x
BAT_PAGER = ''
BAT_CONFIG_PATH = ''
BAT_STYLE = ''
BAT_THEME = ''
BAT_TABS = ''
PAGER = '/usr/local/bin/less'
LESS = ''

As a side note, this only seems to happen with large Bash scripts. Running bat on the debug script you wrote above works perfectly fine, but not with some of my more complex scripts.

sharkdp commented 4 years ago

I ran bat on a collection of all 300+ bash scripts in my home folder (and on a concatenated version of all of these scripts), and couldn't reproduce the error.

I played around with it a bit, and this seems to only happen when using a cache built with bat cache --build --source=/path/to/bat/repo/assets.

So it works fine with the binary assets from the bat repo (once you call bat cache --clear)?

Does it also fail if you use assets/create.sh and compile a new version?

This looks like a bug in the serialization/deserialization or in the .sublime-syntax parser on your system. Very strange. Could you maybe provide the binaries?

sharkdp commented 4 years ago

@eth-p I guess this is still relevant?

eth-p commented 4 years ago

I ended up deleting my custom syntax set as a solution, and haven't tested it in a while. I could check to see if this is still valid later tomorrow and get back to you on it?

eth-p commented 4 years ago

This doesn't seem to happen anymore. Whatever changed between now and back then, it fixed it. 🎉