LuaLS/lua-language-server

Question: "Undefined globals" in plugin environment like Wireshark

Closed this issue · 7 comments

I'm using Lua for developing Wireshark plugins. Wireshark defines various global objects/functions that must be used by plugins.

This gives a lot of "Undefined global" errors, which I can fix by adding these in the workspace settings Lua.diagnostics.globals

But, is there a better way of managing this so I can create a file like those in meta/template/*.lua and have that added to the globals?

This is the recommended approach.
You could put the file in your workspace(but never require it in runtime), or use Lua.workspace.library.
Here is an example: https://github.com/Ketho/vscode-wow-api/blob/master/EmmyLua/API/GlobalAPI/compat.lua

Thanks, that's exactly what I need.

May I ask for clarification on how to do this please?

My lua script general.lua shows this error:

image

so I added a file called compat.lua to my script folder, containing:

---@param name The name of the protocol
---@param desc A Long Text description of the protocol (usually lowercase)
---[Documentation](https://www.wireshark.org/docs/wsdg_html_chunked/wsluarm_modules.html)
function Proto.new(name, desc) end

but general.lua still has the error and compat.lua also shows an error:

image

What am I doing wrong?

You've defined Proto.new(), but not Proto itself.

Add this to your compat.lua.
Proto = {}

I'm working on a more complete one here. https://github.com/roddypratt/router_dissectors/blob/main/wireshark.lua
Feel free to copy/contribute

@roddypratt Thanks very much for your answer and for the file.

Z-LL commented

You've defined Proto.new(), but not Proto itself.

Add this to your compat.lua.
Proto = {}

I'm working on a more complete one here. https://github.com/roddypratt/router_dissectors/blob/main/wireshark.lua
Feel free to copy/contribute

Thanks very much!But there is still a small problem: package.prepend_path() undefined , hope it can be fixed.

package.prepend_path() is not a standard Lua function, and AFACIT it's not mentioned in the Wireshark docs either. But, it does exist in Wireshark , and works as expected :)