rouge-ruby / rouge

A pure Ruby code highlighter that is compatible with Pygments
https://rouge.jneen.net/
Other
3.3k stars 732 forks source link

Use File.basename instead sub to correctly handle long paths on Windows #1911

Closed CAMOBAP closed 1 year ago

CAMOBAP commented 1 year ago

Problem

Recently I faced a crash on windows:

C:/Users/RUNNER~1/AppData/Local/Temp/ocr219E.tmp/lib/ruby/gems/2.7.0/gems/rouge-4.0.1/lib/rouge/lexer.rb:532:in `load': cannot load such file -- C:/Users/RUNNER~1/AppData/Local/Temp/ocr219E.tmp/lib/ruby/gems/2.7.0/gems/rouge-4.0.1/lib/rouge/lexers/C:/Users/runneradmin/AppData/Local/Temp/ocr219E.tmp/lib/ruby/gems/2.7.0/gems/rouge-4.0.1/lib/rouge/lexers/abap.rb (LoadError)
    from C:/Users/RUNNER~1/AppData/Local/Temp/ocr219E.tmp/lib/ruby/gems/2.7.0/gems/rouge-4.0.1/lib/rouge/lexer.rb:532:in `load_lexer'
    from C:/Users/RUNNER~1/AppData/Local/Temp/ocr219E.tmp/lib/ruby/gems/2.7.0/gems/rouge-4.0.1/lib/rouge.rb:54:in `block in load_lexers'
    from C:/Users/RUNNER~1/AppData/Local/Temp/ocr219E.tmp/lib/ruby/gems/2.7.0/gems/rouge-4.0.1/lib/rouge.rb:53:in `each'
    from C:/Users/RUNNER~1/AppData/Local/Temp/ocr219E.tmp/lib/ruby/gems/2.7.0/gems/rouge-4.0.1/lib/rouge.rb:53:in `load_lexers'
    from C:/Users/RUNNER~1/AppData/Local/Temp/ocr219E.tmp/lib/ruby/gems/2.7.0/gems/rouge-4.0.1/lib/rouge.rb:69:in `<top (required)>'

It looks like __dir__ may return a short username (or path) RUNNER~1 instead runneradmin as result this prefix not "stripped"

Proposed solution

There is more reliable way to get filename instead f.sub(...) i.e. use File.basename

jneen commented 1 year ago

One potential issue here is that we load things that are in subfolders:

load_lexer `ocaml/common.rb`
CAMOBAP commented 1 year ago

One potential issue here is that we load things that are in subfolders:

load_lexer `ocaml/common.rb`

Good catch! I will update the PR

ronaldtse commented 1 year ago

Thank you @jneen for the great work and @CAMOBAP for the PR! Any luck that this will be merged soon? Thanks!

tancnle commented 1 year ago

Thank you for your contribution @CAMOBAP ❀️. The changes look good to me πŸ‘πŸΌ πŸš€

ronaldtse commented 1 year ago

Thank you @tancnle ! Look forward to the upcoming release πŸΎπŸ˜‰