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

Your workspace is set to 'x'. Lua language server refused to load this dir. #2975

Open
leet0rz opened this issue Nov 29, 2024 · 6 comments
Open

Comments

@leet0rz
Copy link

leet0rz commented Nov 29, 2024

How are you using the lua-language-server?

NeoVim

Which OS are you using?

Windows

What is the issue affecting?

Other

Expected Behaviour

For this error not to occur, everything else is working just fine.

Actual Behaviour

LSP[lua_ls] Your workspace is set to `C:\Users\waffle`. Lua language server refused to load this directory. Please check your configuration.[learn more here](https://github.com/LuaLS/lua-language-server/wiki/FAQ#why-is-the-server-scanning-the-wrong-folder)

error is occuring even though the LSP works just fine and has been working for years prior to this with the exact same setup with no problems. Not sure what happened here but this error comes up, I remove it and everything is back working like normal. Only happens in the home dir.

Reproduction steps

  1. Install neovim and luals, open neovim from home path and this error comes up.

Additional Notes

No response

Log File

No response

@sumneko
Copy link
Collaborator

sumneko commented Dec 6, 2024

Add --force-accept-workspace

@leet0rz
Copy link
Author

leet0rz commented Dec 6, 2024

Add --force-accept-workspace

This is the template for lua_ls in neovim, where exactly do I add it? Thanks!

require'lspconfig'.lua_ls.setup {
  on_init = function(client)
    if client.workspace_folders then
      local path = client.workspace_folders[1].name
      if vim.loop.fs_stat(path..'/.luarc.json') or vim.loop.fs_stat(path..'/.luarc.jsonc') then
        return
      end
    end

    client.config.settings.Lua = vim.tbl_deep_extend('force', client.config.settings.Lua, {
      runtime = {
        -- Tell the language server which version of Lua you're using
        -- (most likely LuaJIT in the case of Neovim)
        version = 'LuaJIT'
      },
      -- Make the server aware of Neovim runtime files
      workspace = {
        checkThirdParty = false,
        library = {
          vim.env.VIMRUNTIME
          -- Depending on the usage, you might want to add additional paths here.
          -- "${3rd}/luv/library"
          -- "${3rd}/busted/library",
        }
        -- or pull in all of 'runtimepath'. NOTE: this is a lot slower and will cause issues when working on your own configuration (see https://github.com/neovim/nvim-lspconfig/issues/3189)
        -- library = vim.api.nvim_get_runtime_file("", true)
      }
    })
  end,
  settings = {
    Lua = {}
  }
}

@tomlau10
Copy link
Contributor

tomlau10 commented Dec 8, 2024

--force-accept-workspace is a launch cmd argument: https://luals.github.io/wiki/usage/#--force-accept-workspace

I don't know much about neovim, but according to the nvim-lspconfig's doc: https://github.com/neovim/nvim-lspconfig/blob/master/doc/configs.md#lua_ls

The default cmd assumes that the lua-language-server binary can be found in $PATH.
Default config:

  • cmd :
{ "lua-language-server" }

maybe you have to this flag to the cmd?

require'lspconfig'.lua_ls.setup {
  cmd = { "lua-language-server", "--force-accept-workspace" },
  on_init = function(client)
    ... -- your original code above
  end,
  settings = {
    Lua = {}
  }
}

@leet0rz
Copy link
Author

leet0rz commented Dec 23, 2024

--force-accept-workspace is a launch cmd argument: https://luals.github.io/wiki/usage/#--force-accept-workspace

I don't know much about neovim, but according to the nvim-lspconfig's doc: https://github.com/neovim/nvim-lspconfig/blob/master/doc/configs.md#lua_ls

The default cmd assumes that the lua-language-server binary can be found in $PATH.
Default config:

  • cmd :
{ "lua-language-server" }

maybe you have to this flag to the cmd?

require'lspconfig'.lua_ls.setup {
  cmd = { "lua-language-server", "--force-accept-workspace" },
  on_init = function(client)
    ... -- your original code above
  end,
  settings = {
    Lua = {}
  }
}

Thanks for answering! I tried to add --force-accept-workspace but the message seems to persist. Not entirely sure what's going on as I don't see the message anywhere other than in ~ dir with lua files.

@tomlau10
Copy link
Contributor

tomlau10 commented Dec 23, 2024

I don't see the message anywhere other than in ~ dir with lua files.

By looking into the source code of luals, this error msg is generated during the workspace init logic, when the workspace path is / or ~ without setting the force accept workspace flag:

--- 初始化工作区
function m.create(uri)
log.info('Workspace create: ', uri)
local scp = scope.createFolder(uri)
m.folders[#m.folders+1] = scp
if uri == furi.encode '/'
or uri == furi.encode(os.getenv 'HOME' or '') then
if not FORCE_ACCEPT_WORKSPACE then
client.showMessage('Error', lang.script('WORKSPACE_NOT_ALLOWED', furi.decode(uri)))

I guess this logic is to prevent LuaLS preloading too much files, because by default LuaLS will load every files recursively under root workspace path.


I tried to add --force-accept-workspace but the message seems to persist.

Anyway I tried to add a log.info("FORCE_ACCEPT_WORKSPACE", FORCE_ACCEPT_WORKSPACE) there to see its value when I specify a --force-accept-workspace flag, and to my surprise a nil is logged... 🙄

There is another bug 🐛

By further debugging, seems that the arg parsing pattern in the main.lua logic has a bug:

local function loadArgs()
---@type string?
local lastKey
for _, v in ipairs(arg) do
---@type string?
local key, tail = v:match '^%-%-([%w_]+)(.*)$'
local value
if key then
value = tail:match '=(.+)'

  • The ([%w_]+) pattern can only match flags with underscore _ but not a dash -
  • i.e. it can match --force_accept_workspace and set the value into _G["FORCE_ACCEPT_WORKSPACE"]
  • but (currently) cannot match --force-accept-workspace ... 🤦‍♂️

The workaround

Try to use --force_accept_workspace for now @leet0rz
I can log the value of FORCE_ACCEPT_WORKSPACE when I specify this flag


cc @sumneko

main.lua 中解析 arg 所用的 pattern 似是有 bug.

  • '^%-%-([%w_]+)(.*)$' 只能 match 到 --force_accept_workspace (underscore 版) 而不能 match --force-accept-workspace
  • 因為 pattern 用了 [%w_]+
  • 似是要改成 [%w_-]+

@leet0rz
Copy link
Author

leet0rz commented Dec 23, 2024

--force_accept_workspace

It did not work initially but then I updated lua_ls and now that command you gave me the last time worked, thanks :)

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

3 participants