neoclide / coc.nvim

Nodejs extension host for vim & neovim, load extensions like VSCode and host language servers.
Other
24.15k stars 953 forks source link

Coc not updating user workspace folder config on demand #4957

Closed asmodeus812 closed 3 months ago

asmodeus812 commented 3 months ago

Result from CocInfo

2024-03-24T17:18:10.315 INFO (pid:19291) [attach] - receive notification: updateConfig [
  'java.codeGeneration.toString.template',
  '${object.className}{${member.name()}=${member.value}, ${otherMembers} project2}'
]
2024-03-24T17:18:18.173 INFO (pid:19291) [attach] - Request action: codeActions [ '', [ '', 'source', 'refactor' ] ]
2024-03-24T17:18:21.692 INFO (pid:19291) [attach] - receive notification: doCodeAction [
  {
    diagnostics: [],
    providerId: 'c970cf79-452d-490e-90d1-3a19ea0e7b21',
    kind: 'source.generate.toString',
    title: 'Generate toString()...',
    command: {
      arguments: [Array],
      title: 'Generate toString()...',
      command: 'java.action.generateToStringPrompt'
    }
  }
]
2024-03-24T17:18:46.615 INFO (pid:19291) [attach] - receive notification: updateConfig [
  'java.codeGeneration.toString.template',
  '${object.className}{${member.name()}=${member.value}, ${otherMembers} project1}'
]
2024-03-24T17:19:45.699 INFO (pid:19291) [attach] - Request action: codeActions [ '', [ '', 'source', 'refactor' ] ]
2024-03-24T17:19:46.616 INFO (pid:19291) [attach] - receive notification: doCodeAction [
  {
    diagnostics: [],
    providerId: 'c970cf79-452d-490e-90d1-3a19ea0e7b21',
    kind: 'source.generate.toString',
    title: 'Generate toString()...',
    command: {
      arguments: [Array],
      title: 'Generate toString()...',
      command: 'java.action.generateToStringPrompt'
    }
  }
]

Describe the bug

Updating configurations while switching from one workspace / working directory to another in a single nvim instance does not work, or is not applied or respected by the language server. Even though notifications are sent, the server would keep the old configurations and never update itself when using coc#config

Note that if i manually go an save the settings json file for a project before i invoke the toString, watchman (i guess) will trigger and correctly update the settings, however if i manually do coc#config it will not update and apply the settings in the same way

Note2: If i save the settings file CocInfo does not register any information, even though, saving manually seems to actually be the only way to correctly apply the settings

Reproduce the bug

{
    "java.codeGeneration.toString.template": "${object.className}{${member.name()}=${member.value}, ${otherMembers} project1}"
}
{
    "java.codeGeneration.toString.template": "${object.className}{${member.name()}=${member.value}, ${otherMembers} project2}"
}
     local function json_decode(data)
        local ok, result = pcall(vim.fn.json_decode, data)
        if ok then
            return result
        else
            return nil
        end
    end

    local function json_load(path)
        if vim.fn.filereadable(path) == 0 then
            return nil
        end
        local lines = vim.fn.readfile(path)
        return json_decode(table.concat(lines, "\n"))
    end

    _G.json = function()
        local result = json_load(string.format(
                        "%s/.vim/coc-settings.json", vim.fn.getcwd()))
        for key, value in pairs(result or {}) do
            vim.fn["coc#config"](key, value)
        end
        vim.print(result)
    end

    vim.api.nvim_create_autocmd("DirChanged", {
            pattern = "*",
            callback = _G.json,
    })
  1. Open two different projects in the same vim instance
  2. Create the local json settings for each of them
  3. Open two java files from the two projects
  4. Depending on which one you load first the local settings from that will take precedence
  5. Generate to string action - observe both to string actions are using the same template
  6. Go to one project, change the settings, save file, generate toString - config is updated correctly
  7. Go to other project, change the settings, save file, generate toString - config is updated correctly
  8. Navigate to the first project, set cd/tcd, that triggers the DirChanged and coc#config
  9. Config of the first project does not take effect, rather the one from project two is still active

From brief inspection of the implementation it seems that coc#config does an InMemory config store, but not sure if it tries to update it.

The final goal here is to be able to update the settings ideally on DirChanged / Workspace folder changed, and not have to manually go and save the settings each time i visit a specific workspace.

Note: For some reason, upon further testing, if the event is not DirChanged, but rather BufEnter (for example) the coc#config seems to be loading correctly, and i have no idea why.

fannheyward commented 3 months ago

coc#config sets/overrides the configurations, but this API won't write to global/local coc-settings.json.

asmodeus812 commented 3 months ago

Yeah i am not saying it should write to global or local json config file, what i was saying is that if was using DirChanged event after doing :tcd, the aucmd callback would trigger call coc#config but the language server would still be out of sync and was still using the previous workspace local settings, however using BufEnter, then navigating between the two buffers from the two workspaces, the aucmd would trigger do a coc#config of the workspace local configs and it then works, the server's config is correctly picked up from the buffer's current root workspace of the current buffer. What is the deal with DirChanged ? Is it too early of an event to use in this case ?

fannheyward commented 3 months ago

What is the deal with DirChanged

Have no idea about this, sorry