-
-
Notifications
You must be signed in to change notification settings - Fork 358
Support for module-style includes? #3195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
By default, i.e. when you do import 'game.physics' => it will only look for I would suggest you look into your log files to see if TBH I would turn it on personally, because if
In your case, according to your description, I would suggest setting it to |
Checking the log file, I've turned it on and set my path as you describe, but still nothing :( "Lua.runtime.pathStrict": true,
"Lua.runtime.path": [
"src/script-game/?.lua"
],
"Lua.runtime.special": {
"import" : "require"
}
Thanks, I guess I can work around it by doing that for now. |
I have run out of ideas... 🙈 I just tried to setup a dummy workspace folder structure like you described, and I cannot reproduce this issue 🤔
contents for each file in this test setup
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"hint.enable": true,
"hint.setType": true,
"runtime.pathStrict": false,
"runtime.path": [
"src/script-game/?.lua"
],
"runtime.special": {
"import": "require"
}
}
---@class game.world
local world = {}
function world.hello()
return "hello world"
end
return world
local world = import "game.world"
world. |
Ok, I've tried your demo, exact same setup. I think I've found the issue. When I said So these 2 snippets are roughly equivalent (I think): import 'game.world' local world = require 'game.world' Sorry, I should have noticed that. It does seem that even though I get the 'undefined global' diagnostic I can still hit go to function on Is there some way I can tell the language server about this? |
AFAIK, LuaLS doesn't have a load package and insert concept. For example, if the ---@class game.world
world = {}
function world.hello()
return "hello world"
end
Ah~ now I see what you mean, but unfortunately they are not the same in the view of LuaLS 🙈 .
|
Need more on your actual library/framework implementation to give better suggestions 😕
Without further details, the following is the least intrusive way to add global type annotation to the modules:
here is an example modified from the above demo setup:
-- i assume you library have no annotation, and follow the `return module` practice
local world = {}
function world.hello()
return "hello world"
end
return world
---@meta _ # an "_" means this is not a module and will not show up as suggestion when you do `require "xxx"`
---@module "game.world" # this makes the following `world` global variable becomes `world = require "game.world"`
world = {}
import "game.world"
world.<try trigger completion here> edit: oh wait~all the above doesn't explain why everything starts to work by just adding |
Thanks for the detailed replies! This might be the most helpful anyone has been on a GitHub issue for me. I'm not sure on some of the answers here, since I'm mostly just used to our "flavour" of lua. For instance, I've never used
No, not like you show at least. Here's what the core function of local callingEnv = getfenv(2)
if callingEnv == _G then
error("attempting to import into the global environment")
end
callingEnv[packageName] = P
They are much more basic than that: -- src/script-game/world.lua
function hello()
print("hello world")
end -- src/script-game/main.lua
import 'world'
world.hello()
No, see above.
Unfortunately no, that would mean changing thousands of files.
Yeah. Maybe the diagnostic system is just failing to diagnose once it crosses some number of files? I'll try using that option again, and making sure the diagnostic doesn't return to be sure that it actually is fixed by that. EDIT: Yeah, today adding those files to the workspace doesn't seem to help with the diagnostic errors :(. I swear it was fixed the other time. |
With your further explanation, now I have a basic understanding of how your framework works 👀
module("world", package.seeall)
-- this function will be under the package `world`
-- and should be seen as `world.hello()` instead of just a global `hello()`
function hello()
print("hello world")
end But unfortunately LuaLS doesn't support this AFAIK, it doesn't support
Judging from the given snippet, I doubt if it ever works at the beginning 🙈 because LuaLS will just treat all your library functions as globals. I believe LuaLS doesn't support this kind of framework (defining functions in global styles, but they are actually under some namespace) |
All good, thank you for your help :) I guess I'll just have to disable those diagnostics and live with it. Someone can close the ticket if it's not planned to support this. |
Lua.workspace.library
path for completions to work.
I know some people use the setfenv function/_ENV variable to create functionality similar to |
How are you using the lua-language-server?
Other
Which OS are you using?
Windows
What is the issue affecting?
Diagnostics/Syntax Checking
Expected Behaviour
Lua files from my project are found automatically without adding a workspace library path.
Actual Behaviour
Incorrect diagnostics due to LSP seemingly being unable to find the lua files in the path. Also go to definition and such don't work.
Reproduction steps
.
Additional Notes
I have a lot of files in my project which I import like so:
Note that we use
import
instead ofrequire
. It doesn't do anything special regarding the paths, and I have the following setting:These paths are relative to the workspace, plus some folders. The actual paths are like so:
From reading the documentation, it seems like the default

Lua.runtime.path
should pick these files up no problem, but I get diagnostics like "Undefined global "physics"", and I can't go to definition or get completions for those files (details omitted, sorry):If I then add a config for
Lua.workspace.library
, I can make it work:But this feels wrong. These are my files and they're part of my project. I shouldn't need to add them as a library path, should I? Additionally, I could only get it to work as an absolute path, so I need to update this whenever I switch projects. I tried "src/script-game" and "src\script-game" and both reverted back to the broken behaviour.
Am I missing something? Do I need to set something custom in
Lua.runtime.path
?Log File
Unfortunately I can't provide this as most of it is under NDA.
The text was updated successfully, but these errors were encountered: