0

In short: When debugging my lualatex files in VS Code with Local Lua Debugger, breakpoints in lua files are ignored if they lie outside the current working directory; for example, if they lie in the parent folder of the file to be compiled.

I successfully set up Visual Studio Code with all the required extensions as described here to be able to debug lualatex code.

My setting: I have a folder that I opened in VS Code. It contains the following three files:

  • 'script.lua'
  • 'latex/main.tex' (that is, 'main.tex' is lying in a subfolder called 'latex')
  • .vscode/launch.json (the configuration file for the debugger)

script.lua (the content is not relevant for the issue):

    for i=1,10 do
        tex.print("Hello ")
    end

main.tex:

    % !TeX program = lualatex
    \documentclass{article}
    
    \directlua{%
    if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
        package.loaded["lldebugger"] = assert(loadfile(os.getenv("LOCAL_LUA_DEBUGGER_FILEPATH")))()
        require("lldebugger").start()
    end}
    
    \begin{document}
    \directlua{require("../script.lua")}
    \end{document}

launch.json:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Debug Custom Lua Environment",
                "type": "lua-local",
                "request": "launch",
                "stopOnEntry": false,
                "program": {
                    "command": "lualatex"
                },
                "args": ["${file}"],
                "cwd": "${fileDirname}"
            }
        ]
      }

Issue: When I put a breakpoint into script.lua (e.g., 2nd line), and press F5 (when main.tex is selected), then main.tex is compiled without errors, but the breakpoint is ignored.

Can I tell VS Code + Local Lua Debugger to pause at the breakpoints? Important: I want to keep the files where they are and I also do not want to change the path of script.lua in main.tex.

Observation 1: The debugger correctly pauses at the breakpoint, when I change the penultimate line of main.tex to \directlua{require("script.lua")} and either

  • remove "cwd": "${fileDirname}" from launch.json, or
  • move script.lua into the folder latex.

Observation 2:

  • The breakpoints work fine if the file in require( ) is given by an absolute path, e.g., require("c:/test/script.lua").
  • The breakpoints are ignored if the path is relative (to main.tex), e.g. require("..\script.lua").

But as noted above, I look for a different solution than the ones of the observations. The reasons: On one hand, I have many files where I would need to carefully update the paths. The path structure is actually more complicated and I want to keep it relative to the respective tex files. On the other hand, I want to be able to compile the files directly from within their directories also without (using the configuration files of) VS Code, e.g., by just using the terminal.

If possible, I would prefer a solution where I have only to configure Local Lua Debugger in an appropriate way without changing the code at all.

Some ideas: The description page of Local Lua Debugger mentions two options for the configuration file that sounded promising: scriptRoots and scriptFiles. However, I was not able to resolve the issue by using them. Maybe I used them wrongly.

1 Answer 1

1

I came up with the following, though not fully satisfactional, solution:

  1. Write the following code in the header of main.tex within a \begin{luacode}...\end{luacode} environment (provided by the package luacode), or even better in a separate file, say debug.lua:
        if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
            package.loaded["lldebugger"] = assert(loadfile(os.getenv("LOCAL_LUA_DEBUGGER_FILEPATH")))()
            require("lldebugger").start()
        end
        
        ------------------------------------------------- 
        -- Source: https://tex.stackexchange.com/questions/42417/full-path-of-current-file
        
        require 'lfs'
        
        function buildPath(...)
            local pathseparator = package.config:sub(1,1)
            local elements = {...}
            return table.concat(elements, pathseparator)
        end
        
        -- get the current path plus the file name.
        function getAbsolutePath(filename)
            return buildPath(lfs.currentdir(), filename)
        end
        -------------------------------------------------
        
        function requireFix(filename)
            return require(getAbsolutePath(filename))
        end
  1. In main.tex, replace require(filename) by requireFixed(filename) (after debug.lua has been included). The new main.tex file:
    % !TeX program = lualatex
    \documentclass{article}
    
    \directlua{require("debug.lua")}
    
    \begin{document}
    \directlua{requireFixed("../script.lua")}
    \end{document}

Now everything works fine: VS Code pauses at all the breakpoints, and main.tex can be also compiled directly from the terminal.

Why not fully satisfactional? The reason why the solution is not fully satisfactionally is that main.tex has been slightly changed. I would prefer a solution where I just set up Local Lua Debugger appropriately without changing the code at all. But, all in all, the solution is good enough for me.

Background: As observed in the question, the breakpoints work if the path to script.lua given in require( ) is absolute (starts with the disc's letter). The idea is now to wrap a lua function around the (relative) path that I to pass to require( ) that transforms the relative path into the corresponding absolute path, that is, I write require(getAbsolutePath("..\script.lua")).

Such a lua function was proposed in Paulo Cereda's answer to the question Full path of current file. I used it in my solution.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .