Skip to content

Behavior of Lua.runtime.path not as expected (bug/question) #847

Description

@flrgh

Describe the bug

Starting with 2.5.0, I noticed that the language server no longer loaded files that correspond to require()-ed modules.

My neovim scripts generate the language server config at startup, which includes populating Lua.runtime.path and Lua.workspace.library with some custom locations, which includes the current working directory and/or workspace directory (repository root). It adds the following paths to Lua.runtime.path:

  • /absolute/path/to/workspace/?.lua
  • /absolute/path/to/workspace/?/init.lua
  • ./?.lua
  • ./?/init.lua

I have discovered that having ?.lua and ?/init.lua (the defaults in the vscode plugin) makes things work properly, but It's odd that the other versions (absolute and relative paths) do not. Is this intended?

To Reproduce

Note: Lua.runtime.pathStrict is set to false

~/git/scratch $ tree
.
├── mod.lua
├── subdir
│   └── sub_module.lua
└── test.lua

contents of subdir/sub_module.lua:

--- my submodule
local _M = {
  --- module name
  name = "sub_module",

  --- some important attribute
  attr = 456,
}

return _M

contents of test.lua:

local mod = require "mod"
print(mod.attr)

local submod = require "subdir.sub_module"
print(submod.attr)

local other = require "other.mod"
print(other.value)

With Lua.runtime.path set to ["?.lua", "?/init.lua"], files are correctly located and loaded:

Screenshot from 2021-12-06 08-59-22

Using relative path (./?.lua|./?/init.lua) or absolute path (/path/to/git/scratch/?.lua|/path/to/git/scratch/?/init.lua) leads to the module not being resolved/loaded:

Screenshot from 2021-12-06 09-00-49

I've also noted that files outside the workspace cannot be loaded via Lua.runtime.path. For instance, if a file exists at /path/to/other/mod.lua, and I add /path/to/other/?.lua to Lua.runtime.path, it is not discovered. I understand that adding /path/to/other to Lua.workspace.library solves this, but all of this makes me wonder: what is the purpose of Lua.runtime.path if only the default values (?.lua|?/init.lua) actually do anything?

Expected behavior

I expected ?.lua, ./?.lua, and /absolute/path/to/?.lua to be equal/equivalent in behavior, but as of 2.5.0 it seems that only ?.lua has any effect.

Ideally I would like to add certain paths to Lua.runtime.path so that files will be discovered when require()-ed but without adding them to Lua.workspace.library (since that causes the full directory to be scanned and pre-loaded, which leads to a slower startup time).

Environment (please complete the following information):

  • OS: Linux
  • Is WSL remote?
  • Client: VSCode and Neovim

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions