Skip to content
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

Support for custom module resolution #2947

Closed
rhys-vdw opened this issue Nov 10, 2024 · 2 comments
Closed

Support for custom module resolution #2947

rhys-vdw opened this issue Nov 10, 2024 · 2 comments

Comments

@rhys-vdw
Copy link

rhys-vdw commented Nov 10, 2024

We have a custom module loader that uses full file paths to resolve files.

local units = VFS.Include("luaui/configs/unit_buildmenu_config.lua")

To get proper type inference we need to add @module to every call.

---@module "unit_buildmenu_config"
local units = VFS.Include("luaui/configs/unit_buildmenu_config.lua")

It's possible to alias VFS.Include to act like require:

  "runtime.special": {
    "VFS.include": "require"
  },

But this won't work because VFS.Include accepts a path, and require accepts a module name. Is there any way to write a custom module resolver so that we can support VFS.Include project-wide?

@tomlau10
Copy link
Contributor

tomlau10 commented Nov 10, 2024

It is possible to change completion.requireSeparator to use / as separator, and then add ? to runtime.path
=> to make LuaLS's require to accept full path name

{
    "$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
    "completion.requireSeparator": "/",
    "runtime.path": [
        "?",
        "?.lua"
    ],
    "runtime.special": {
        "VFS.include": "require"
    }
}

Here is a similar question before (but in chinese): #2900 (comment)


However this affects standard require (or other specials that are of require type) as well 🤔
So with the above setting, you can't use . as separator in standard require:

require "lib.A" --> this equals to requiring a file named "lib.A" or "lib.A.lua", instead of "lib/A.lua"
require "lib/A" --> you have to use `/` if you want to require "lib/A"

@rhys-vdw
Copy link
Author

Thanks you @tomlau10! That appears to be working. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants