You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature Request] Multiple-project/Monorepo support #2962
Loading this entire repo at once doesn't work well because of how the require paths are established via runtime.path. The primary issue is that it leads to ambiguous require resolution if two of the roots have files with the same name.
Obviously, one workaround is to use a multi-root workspace and only add the folders for the projects I currently care about, but that's tedious since I'd be adding and removing folders from the workspace constantly.
One idea that I had was being able to specify an "entrypoint" glob. The plugin could traverse up the directory tree looking for a file that matches the glob, and then use that as the root for all local requires. But that might not be a good "general" solution even though it works well for my use case.
The feature request might be reasonable, but it seems difficult to implement for the current LuaLS. It requires separating the indexing/cache of the project to different projects based on URI, which might need significant changes to the current LuaLS.
Obviously, one workaround is to use a multi-root workspace and only add the folders for the projects I currently care about, but that's tedious since I'd be adding and removing folders from the workspace constantly.
it will auto search any folders containing a package.json in your root workspace (because it is designed for nodejs projects I guess)
and then it has a command Monorepo: Select Workspace Folder for you to dynamically add / remove any detected packages folder
but as said it requires package.json in your package folder first, you might have to write a script to create each for your sub folder. The following is a script generated by chatgpt, then modified a bit by me, which creates a package.json with a name field equals to that of the sub folder:
I see, but how do you dynamically add / remove folders to the workspace?
Regenerate the .code-workspace json?
Or just by vscode's standard UI Add/Remove Folder to Workspace? 🤔
The above suggested plugin's selection UI can do filtering on all detected sub folders, also it provides a select/unselect all option in that UI.
Another case of this, with a concrete repro and a suggested resolution rule (LuaLS 3.19.1).
Layout, with the workspace root set to workspace/:
workspace/project-a/.luarc.json
workspace/project-a/main.lua
workspace/project-a/lib/mod.lua
workspace/project-b/.luarc.json
workspace/project-b/lib/mod.lua -- an older, different copy of mod.lua
workspace/project-a/main.lua:
localmod=require("lib.mod")
mod.pick(1, 2)
Settings (the default runtime.path and runtime.pathStrict = false apply; both .luarc.json files only hold ordinary diagnostics settings).
What happens:
require("lib.mod") in project-a/main.lua can resolve to project-b/lib/mod.lua.
Find References on function mod.pick in project-a/lib/mod.lua returns only the definition. The callers in project A are not found, because their require went to project B's copy.
Diagnostics in project A (for example argument-count warnings) are computed against project B's copy.
Why the existing settings do not fit:
runtime.path patterns such as ?.lua and ?/init.lua are relative to the workspace root, so lib/mod.lua matches in every project. runtime.pathStrict only anchors the pattern to the root, which still matches every project under it.
workspace.ignoreDir and workspace.library are the same for all files, so they cannot hide project B from project A and project A from project B at once.
A multi-root workspace works, but each project has to be added by hand, which is what this issue already describes. It also does not help when a client sets one root.
Suggested behavior: when several workspace files match a require path, resolve it to the one nearest the requiring file, for example by searching from the requiring file's folder upward. Or treat a folder that has its own .luarc.json as its own root for runtime.path. This avoids the "entrypoint glob" and needs no extra setting.
Activity
CppCXY commentedon Nov 21, 2024
The feature request might be reasonable, but it seems difficult to implement for the current LuaLS. It requires separating the indexing/cache of the project to different projects based on URI, which might need significant changes to the current LuaLS.
tomlau10 commentedon Nov 23, 2024
(non-perfect solution)
I just found an extension Monorepo Workspace: https://marketplace.visualstudio.com/items?itemName=folke.vscode-monorepo-workspace
package.jsonin your root workspace (because it is designed for nodejs projects I guess)Monorepo: Select Workspace Folderfor you to dynamically add / remove any detected packages folderpackage.jsonin your package folder first, you might have to write a script to create each for your sub folder. The following is a script generated by chatgpt, then modified a bit by me, which creates apackage.jsonwith anamefield equals to that of the sub folder:Monorepo: Select Workspace Foldercommand:hope that helps a bit before LuaLS can have a better support for monorepos 🙂
dljsjr commentedon Nov 24, 2024
What I actually did was write a bash + jq script that generates a
_.code-workspaceJSON file instead 😅tomlau10 commentedon Nov 24, 2024
I see, but how do you dynamically add / remove folders to the workspace?
Regenerate the
.code-workspacejson?Or just by vscode's standard UI
Add/Remove Folder to Workspace? 🤔The above suggested plugin's selection UI can do filtering on all detected sub folders, also it provides a
select/unselect alloption in that UI.dljsjr commentedon Dec 6, 2024
Right now I just have it add everything 🤷♂️
dgutson commentedon Nov 28, 2026
Another case of this, with a concrete repro and a suggested resolution rule (LuaLS 3.19.1).
Layout, with the workspace root set to
workspace/:workspace/project-a/main.lua:Settings (the default
runtime.pathandruntime.pathStrict = falseapply; both.luarc.jsonfiles only hold ordinary diagnostics settings).What happens:
require("lib.mod")inproject-a/main.luacan resolve toproject-b/lib/mod.lua.function mod.pickinproject-a/lib/mod.luareturns only the definition. The callers in project A are not found, because theirrequirewent to project B's copy.Why the existing settings do not fit:
runtime.pathpatterns such as?.luaand?/init.luaare relative to the workspace root, solib/mod.luamatches in every project.runtime.pathStrictonly anchors the pattern to the root, which still matches every project under it.workspace.ignoreDirandworkspace.libraryare the same for all files, so they cannot hide project B from project A and project A from project B at once.Suggested behavior: when several workspace files match a
requirepath, resolve it to the one nearest the requiring file, for example by searching from the requiring file's folder upward. Or treat a folder that has its own.luarc.jsonas its own root forruntime.path. This avoids the "entrypoint glob" and needs no extra setting.