2

Is there a way of debugging Lua scripts using ZeroBraneStudio but having the scripts loaded/initialized via NLua in C# from Visual Studio for macOS or even Windows (Not VS Code)?

The primary reason to do so is to get the CLR package interoperability included in NLua available for Lua in ZBS.

I have not been able to get the remote debugging feature found in ZBS to work with NLua. The call to require("mobdebug").start() in a Lua script does trigger a breakpoint in Visual Studio when the program is in debug mode, but no breakpoints set ZBS get triggered. Is there more configuration needed?

Please and thanks

-- UPDATE 1:

Output from line print:

BASEDIR /Users/B1313/Desktop/MyProject/
DELB * 0
SETB Modules/Test.lua 88
SETB Modules/Test.lua 94
LOAD 1272 ScriptCode/Main.lua
--[[
G.T.D.
  • 386
  • 1
  • 5
  • 21

1 Answers1

1

This is likely to be caused by the mismatch between the paths that the debugger gets and the paths that are set in the IDE (and used when the breakpoints are set). You may want to check the section on breakpoints in the FAQ (https://studio.zerobrane.com/doc-faq#why-breakpoints-are-not-triggered), especially items 3 and 4 on the list.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • According to ZBS, when executing via ZBS, the `source = "@./ScriptCode/Main.lua"`, according to VS for Mac, when executing VS, the `source = "@/Users/B1313/Desktop/MyProject/ScriptCode/Main.lua"`. It looks like the file capitalization is correct, but the paths are somewhat different. How would I go about getting them to be the same (they appear normalized per the documentation)? I would assume ZBS is doing something with regards to setting the project directory that is causing the path to be less than fully qualified, correct? – G.T.D. Apr 29 '19 at 01:51
  • Correct, ZBS sets the `basedir` value for the debugger based on the project path set in the IDE. You can try adding `require('mobdebug').basedir('/Users/B1313/Desktop/MyProject/')` *before* `.start()` call. – Paul Kulchenko Apr 29 '19 at 02:07
  • Thanks Paul! I got further now but it looks like there are some incompatibilities with LuaSocket and Lua 5.2+ which NLua uses. Specifically, `error loading module 'socket.core' ... 'Symbol not found: _luaL_openlib'` I doubt there is a way to run the debugging features of ZBS without LuaSocket, correct? Curiously, have there been any other C# - Lua interoperability libraries that perhaps you/others have tried that work with ZBS? – G.T.D. Apr 29 '19 at 03:39
  • That's strange, because Lua 5.2 doesn't have `openlib`. Are you sure you are loading the correct luasocket version? You may want to check that `package.cpath` points to libraries in `ZeroBraneStudio/bin/clibs52` instead of `clibs` (ZBS sets the environment for you, but since you run outside of it, you may need to set the correct libraries yourself). – Paul Kulchenko Apr 29 '19 at 04:16
  • Ok, I fixed that by adding the version suffix to the cpath reference to the folder. - That gets the ZBS integration working, kind of - now MobDebug throws the following error (in the output window) when flow transfers to ZBS `Can't start debugging session due to internal error 'lualibs/mobdebug/mobdebug.lua:1506: bad argument #1 to 'find' (string expected, got nil)` and the following error in VS `...Contents/ZeroBraneStudio/lualibs/mobdebug/mobdebug.lua:809: bad argument #2 to 'sub' (number expected, got nil)` when running under a debug configuration. Is this a bug? – G.T.D. Apr 29 '19 at 04:40
  • The first error is shown because the application process got terminated before the debugging got started, which happened because of the second error. The second error is strange, as there has to be a command received by the debugger. Can you "print" the value of `line` variable before line 809? This will show the command that was received by the application. – Paul Kulchenko Apr 29 '19 at 05:00
  • I get the following output when I print `line` at 808 (just before the string.sub call) ... `BASEDIR /Users/B1313/Desktop/MyProject/\n DELB * 0\n SETB Modules/Test.lua 88\n SETB Modules/Test.lua 94\n LOAD 1272 ScriptCode/Main.lua\n --[[` – G.T.D. Apr 29 '19 at 05:24
  • I updated the original post to show a clearer output (comment markdown is not great for multi-line output). – G.T.D. Apr 29 '19 at 05:31
  • 1
    I figured out the problem. I was doing the `require("mobdebug").start()` call far too late into my scripts (i.e. once my modules loaded and ran). I misinterpreted the directions for remote debugging as putting the call for the debugger to start right before the first breakpoint -- that is wrong. It needs to be put as the first call / few lines of code (e.g. about the same place where one might setup package path fields) for the entire scripting environment. -- I will update the OP with more details later. Thanks Paul for all of your help (and ZBS)! I never would have gotten this far without it – G.T.D. Apr 29 '19 at 10:23