rchaput/acronymsdown

Use package with Quarto: `attempt to index a nil value (global 'Acronyms')`

Closed this issue · 8 comments

I used acronymsdown without issues up until last week. Then something happened and now I cannot render my report. The yml is the following:

---
title: "Paper"
metadata-files:
  - ../../../all_phd/authors.yml
format:
  pdf:
    pdf-engine: pdflatex
    number-sections: true
    toc: false
    margin-left: 15mm
    margin-right: 15mm
    margin-bottom: 20mm
    margin-top: 15mm
    include-in-header:
      text: |
        \usepackage{rotating}
        \usepackage{float}
        \usepackage{typearea}
bibliography: ../../../PhD.bib
link-citations: true
linkcolor: blue
execute:
  echo: false
  warning: false
  error: false
  message: false
editor_options: 
  chunk_output_type: console
filters:
  - acronyms
  - ../../../all_phd/scholarly-metadata.lua
  - ../../../all_phd/author-info-blocks.lua
acronyms:
  fromfile: ../../../all_phd/acronyms.yml
  insert_links: false
  insert_loa: false
  sorting: alphabetical
---

This is what I obtain when trying to render it:

Error running filter /home/lorenzo/applications/quarto-cli/src/resources/filters/main.lua:
...lixSC-neuro/docs/_extensions/acronyms/parse-acronyms.lua:109: attempt to index a nil value (global 'Acronyms')
stack traceback:
	[C]: in ?
	[C]: in method 'walk'
	...s/quarto-cli/src/resources/filters/./ast/customnodes.lua:72: in function 'run_emulated_filter'
	.../quarto-cli/src/resources/filters/./ast/runemulation.lua:34: in local 'callback'
	[string "..."]:1875: in field 'withScriptFile'
	.../quarto-cli/src/resources/filters/./ast/runemulation.lua:43: in upvalue 'run_emulated_filter_chain'
	.../quarto-cli/src/resources/filters/./ast/runemulation.lua:78: in function <.../quarto-cli/src/resources/filters/./ast/runemulation.lua:75>
stack traceback:
	...s/quarto-cli/src/resources/filters/./ast/customnodes.lua:72: in function 'run_emulated_filter'
	.../quarto-cli/src/resources/filters/./ast/runemulation.lua:34: in local 'callback'
	[string "..."]:1875: in field 'withScriptFile'
	.../quarto-cli/src/resources/filters/./ast/runemulation.lua:43: in upvalue 'run_emulated_filter_chain'
	.../quarto-cli/src/resources/filters/./ast/runemulation.lua:78: in function <.../quarto-cli/src/resources/filters/./ast/runemulation.lua:75>
ERROR: Error
    at renderFiles (file:///home/lorenzo/applications/quarto-cli/src/command/render/render-files.ts:540:23)
    at eventLoopTick (ext:core/01_core.js:181:11)
    at async render (file:///home/lorenzo/applications/quarto-cli/src/command/render/render-shared.ts:98:18)
    at async renderForPreview (file:///home/lorenzo/applications/quarto-cli/src/command/preview/preview.ts:400:24)
    at async render (file:///home/lorenzo/applications/quarto-cli/src/command/preview/preview.ts:159:22)
    at async preview (file:///home/lorenzo/applications/quarto-cli/src/command/preview/preview.ts:176:18)
    at async Command.fn (file:///home/lorenzo/applications/quarto-cli/src/command/preview/cmd.ts:356:7)
    at async Command.execute (file:///home/lorenzo/applications/quarto-cli/src/vendor/deno.land/x/cliffy@v0.25.4/command/command.ts:1790:7)
    at async quarto (file:///home/lorenzo/applications/quarto-cli/src/quarto.ts:126:3)
    at async file:///home/lorenzo/applications/quarto-cli/src/quarto.ts:158:5

As of now, I tried to create a _extensions directory in the same folder of the Quarto document (not the root of the project, though), whitin which I created a directory named acronyms which contain the .lua filters and the file _extension.yml:

title: acronyms
author: rchaput
version: 1.0.0
quarto-required: ">=1.2.0"
contributes:
  filters:
    - parse-acronyms.lua

Other informations:

> R.version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          1.2                         
year           2021                        
month          11                          
day            01                          
svn rev        81115                       
language       R                           
version.string R version 4.1.2 (2021-11-01)
nickname       Bird Hippie

I am using Quarto version 99.9.9. The output of quarto check is the following:

[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.1.2: OK
      Dart Sass version 1.55.0: OK
      Deno version 1.33.2: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 99.9.9
      Path: /home/lorenzo/applications/quarto-cli/package/dist/bin

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK
      Version: 3.10.6
      Path: /usr/bin/python3
      Jupyter: 5.2.0
      Kernels: python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........OK
      Version: 4.1.2
      Path: /usr/lib/R
      LibPaths:
        - /home/lorenzo/Documents/edu/phd/papers/paper3_helixSC_neuro/paper-helixSC-neuro/renv/library/R-4.1/x86_64-pc-linux-gnu
        - /home/lorenzo/.cache/R/renv/sandbox/R-4.1/x86_64-pc-linux-gnu/9a444a72
      knitr: 1.42
      rmarkdown: 2.21

[✓] Checking Knitr engine render......OK

The version of acronymsdown is 0.11.1, and I downloaded the lua filters last Friday.

The content of acronyms.yml is of the form:

---
acronyms:
  keys:
    - key: edc
      shortname: EDC
      longname: endocrine disruptor
---

I have not made any recent modification that would explain this issue.
Basically, it seems from your error log that Quarto finds and executes the parse-acronyms.lua script, but this script cannot find the acronyms.lua file (which should be in the same folder).
I have no idea what would be causing this issue, but my best guess would be something in the way Quarto executes the filter?

Can you manually edit the parse-acronyms.lu file in your _extensions/acronyms/ folder to add the following lines after line 54 (after the definition of the warn function):

warn("current_dir=", current_dir)
warn("package.path=", package.path)

This should print two additional lines when you try to render your document, looking like this:

[WARNING][acronymsdown] current_dir=<some path to the _extensions/acronyms/ folder>
[WARNING][acronymsdown] package.path=<some list of paths separated by ; that should contain the path to the extensions folder>

Ultimately, a brute-force solution could be to merge all the Lua files into parse-acronyms.lua (and remove the require(...) calls), since it is successfully found by Quarto.

[WARNING][acronymsdown] current_dir=/home/lorenzo/Documents/edu/phd/papers/paper3_helixSC_neuro/paper-helixSC-neuro/docs/_extensions/acronyms/
[WARNING][acronymsdown] package.path=/usr/local/share/lua/5.4/?.lua;/usr/local/share/lua/5.4/?/init.lua;/usr/local/lib/lua/5.4/?.lua;/usr/local/lib/lua/5.4/?/init.lua;./?.lua;./?/init.lua;/home/lorenzo/applications/quarto-cli/src/resources//pandoc/datadir/?.lua;/home/lorenzo/Documents/edu/phd/papers/paper3_helixSC_neuro/paper-helixSC-neuro/docs/_extensions/acronyms//?.lua

In the .yml of the extension I added the following:

contributes:
  filters:
    - acronyms.lua
    - parse-acronyms.lua
    - acronyms_styles.lua
    - sort_acronyms.lua

Now I get this error:

Error running filter /home/lorenzo/applications/quarto-cli/src/resources/filters/main.lua:
...to-cli/src/resources/filters/./common/wrapped-filter.lua:152: bad argument #1 to 'for iterator' (table expected, got function)
stack traceback:
	...to-cli/src/resources/filters/./common/wrapped-filter.lua:152: in local 'makeSingleHandler'
	...to-cli/src/resources/filters/./common/wrapped-filter.lua:169: in local 'callback'
	[string "..."]:1875: in function <[string "..."]:1873>
	(...tail calls...)
	...uarto-cli/src/resources/filters/./ast/emulatedfilter.lua:27: in function 'make_wrapped_user_filters'
	...o/applications/quarto-cli/src/resources/filters/main.lua:194: in main chunk
ERROR: Error
    at renderFiles (file:///home/lorenzo/applications/quarto-cli/src/command/render/render-files.ts:540:23)
    at eventLoopTick (ext:core/01_core.js:181:11)
    at async render (file:///home/lorenzo/applications/quarto-cli/src/command/render/render-shared.ts:98:18)
    at async renderForPreview (file:///home/lorenzo/applications/quarto-cli/src/command/preview/preview.ts:400:24)
    at async render (file:///home/lorenzo/applications/quarto-cli/src/command/preview/preview.ts:159:22)
    at async preview (file:///home/lorenzo/applications/quarto-cli/src/command/preview/preview.ts:176:18)
    at async Command.fn (file:///home/lorenzo/applications/quarto-cli/src/command/preview/cmd.ts:356:7)
    at async Command.execute (file:///home/lorenzo/applications/quarto-cli/src/vendor/deno.land/x/cliffy@v0.25.4/command/command.ts:1790:7)
    at async quarto (file:///home/lorenzo/applications/quarto-cli/src/quarto.ts:126:3)
    at async file:///home/lorenzo/applications/quarto-cli/src/quarto.ts:158:5
[WARNING][acronymsdown] current_dir=/home/lorenzo/Documents/edu/phd/papers/paper3_helixSC_neuro/paper-helixSC-neuro/docs/_extensions/acronyms/
[WARNING][acronymsdown] package.path=/usr/local/share/lua/5.4/?.lua;/usr/local/share/lua/5.4/?/init.lua;/usr/local/lib/lua/5.4/?.lua;/usr/local/lib/lua/5.4/?/init.lua;./?.lua;./?/init.lua;/home/lorenzo/applications/quarto-cli/src/resources//pandoc/datadir/?.lua;/home/lorenzo/Documents/edu/phd/papers/paper3_helixSC_neuro/paper-helixSC-neuro/docs/_extensions/acronyms//?.lua

Hmm, that's curious, because the paths seem to be OK. I initially thought the trailing '/' could cause problems, but I have a similar path on my machine and it works.
I cannot see any problem here, except maybe if one of the preceding paths already has an acronyms.lua file, in which case mine could not be loaded, but I do not think it's the case.

I cannot do it right now, but I will try to provide you with a "merged" script that avoids this require issue later today.

If I remove the require and replace it with the content of the coressponding lua filter, everything works fine.

In the .yml of the extension I added the following:

contributes:
  filters:
    - acronyms.lua
    - parse-acronyms.lua
    - acronyms_styles.lua
    - sort_acronyms.lua

Now I get this error:

Error running filter /home/lorenzo/applications/quarto-cli/src/resources/filters/main.lua:
...to-cli/src/resources/filters/./common/wrapped-filter.lua:152: bad argument #1 to 'for iterator' (table expected, got function)
(...)

Yes, that is because Quarto expects each filter file to return a very specific value, but only the parse-acronyms.lua file returns this value. The other files return other values (that are important to parse-acronyms.lua) but which can only be used internally.
I will have to check with Quarto's documentation for other ways to import files if I want to solve this issue in the long-term.

If I remove the require and replace it with the content of the coressponding lua filter, everything works fine.

Nice, at least your problem is solved. That was why I intended to do in the short-term. I am curious though, did you replace only the require('acronyms.lua'), or all of the require(...) calls?
If only the acronyms.lua one needs to be replaced, there is some black magic underlying that I do not understand, but on the other hand it gives me an easy way to fix this issue in the long-term, by making the acronyms.lua more akin to the other imported files.

Only that call!