pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Pyrefly LSP Does Not Apply Any Configuration From LSP Client (settings/ initializationOptions)

Open dvctl opened this issue 1 year ago • 9 comments

Describe the Bug

Pyrefly’s LSP server currently ignores all configuration sent from the LSP client, including:

  • settings
  • initializationOptions
  • workspace/didChangeConfiguration

This means users cannot configure Pyrefly globally via Neovim, VSCode, or any other LSP client. The only configuration source that actually affects behavior is [tool.pyrefly] inside pyproject.toml.

This prevents enabling diagnostics, strict mode, analyzer mode, or any other options globally. Everything must be repeated inside every project.


Environment

  • Pyrefly version: 0.42.2
  • Editor: Neovim (LazyVim + nvim-lspconfig)
  • OS: Linux (Arch-based)

What I Expected

I expected Pyrefly to respect LSP configuration the same way other language servers do. Example configuration:

lspconfig.pyrefly.setup({
  cmd = { "pyrefly", "lsp" },
  settings = {
    pyrefly = {
      analyzer = true,
      typechecking = "strict",
      diagnostics = true,
    },
  },
})

Or:

lspconfig.pyrefly.setup({
  cmd = { "pyrefly", "lsp" },
  initializationOptions = {
    analyzer = true,
    typechecking = "strict",
    diagnostics = true,
  },
})

Expected outcome: The server should enable analyzer, diagnostics, and strict mode.


What Actually Happens

Pyrefly receives the settings (verified via :LspInfo):

"settings": {
  "pyrefly": {
    "analyzer": true,
    "diagnostics": true,
    "typechecking": "strict"
  }
}

But the server does not apply them. Diagnostics stay disabled unless pyproject.toml contains:

[tool.pyrefly]
analyzer = true
typechecking = "strict"
diagnostics = true

In other words:

➤ Pyrefly reads configuration from pyproject.toml

➤ Pyrefly completely ignores configuration from LSP clients

➤ There is currently no way to apply global configuration


How to Reproduce

  1. Remove all Pyrefly configuration from pyproject.toml
  2. Configure the LSP client (Neovim/VSCode/etc.) to send analyzer/strict/diagnostics in settings or initializationOptions
  3. Start Pyrefly LSP
  4. Verify with :LspInfo that the settings were indeed passed to the server
  5. Observe that diagnostics and analysis remain disabled

Repro code that should trigger diagnostics:

def f(x: int) -> str:
    return x

Expected: type mismatch error Actual: no diagnostics unless [tool.pyrefly] is defined in a pyproject.


Root Cause (Based on Code and Behavior)

Pyrefly’s LSP server only calls:

load_config_from_pyproject(root_dir)

There is no corresponding logic for:

  • reading settings from LSP initialization params
  • merging them with pyproject
  • applying runtime configuration changes

This part from Pyright (which Pyrefly is based on) appears to be missing entirely.


Why This Is a Problem

  • Users must add [tool.pyrefly] to every Python project
  • No global settings are possible
  • Editors cannot control configuration
  • LSP-based configuration (which is standard across all LSP servers) simply doesn’t work

This is especially painful for users with many projects or a unified dev environment.

And yes i have used LLM to write this, based on what i know.


What Would Fix It

Implement LSP-side configuration handling:

  1. Accept settings in initialize request
  2. Merge them with the project config
  3. Respect workspace/didChangeConfiguration
  4. Prioritize LSP overrides when both are present

This is how most language servers behave (Pyright, Pylyzer, Ruff, Jedi Language Server).


Conclusion

Pyrefly LSP ignores client-side configuration entirely, and only pyproject.toml is respected. This makes global configuration impossible and forces project-specific duplication.

Please consider adding support for reading configuration from LSP settings or initializationOptions. This will dramatically improve Pyrefly’s usability in realistic development setups.

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

dvctl avatar Nov 20 '25 13:11 dvctl

you're correct that we don't apply configuration from the initialize message. but we do support workspace/didChangeConfiguration as long as the language client has the workspace configuration capability. is there a way to have neovim send that while we work on a fix for you?

let me see if I can patch a change to support configuration in the initialize handshake right now

kinto0 avatar Nov 20 '25 20:11 kinto0

@kinto0 Thanks for the clarification!

Neovim does advertise workspace.configuration = true, but it does NOT implement the workspace/configuration request. So when the LSP server calls request_settings_for_all_workspaces, Neovim returns null.

This explains the log:

"Could not decode LspConfig from Null"

Since Pyrefly does not read params.settings from workspace/didChangeConfiguration, and only tries to load config through workspace/configuration, the server never receives a valid config object.

This means:

  • settings sent during initialize are ignored
  • settings sent in didChangeConfiguration are ignored
  • workspace/configuration is not implemented in Neovim

The only configuration source that works today is pyproject.toml.

A fix would be:

  1. Apply config from didChangeConfiguration.params.settings, OR
  2. Accept config from initialize.settings, OR
  3. Avoid requiring workspace/configuration for LSP config.

Any of these would make the LSP side fully functional in Neovim.

or do you have any other suggestions for some quick fix or work arounds for now???

dvctl avatar Nov 21 '25 04:11 dvctl

I’ve done some additional digging and here’s what I’ve verified:

Findings

  • Neovim sends LSP settings under the initialize request inside initializationOptions and also through workspace/didChangeConfiguration.
  • Pyrefly currently does not read any client-provided configuration during the initialize handshake.
  • It expects configuration to come only from files (pyproject.toml, pyrefly.toml, etc.), and the incoming client settings are ignored.
  • I confirmed that settings do reach the server — the settings table is visible under vim.lsp.get_active_clients() — but they never get applied because the fields are not consumed anywhere in the initialization flow.
  • Adding the same settings inside pyproject.toml works immediately, which confirms the LSP path is the only missing piece.

Repro

With this Neovim config:

settings = {
  pyrefly = {
    analyzer = true,
    diagnostics = true,
    typechecking = "strict",
  },
}

vim.lsp: Active Clients shows:

Settings = {
  pyrefly = {
    analyzer = true,
    diagnostics = true,
    typechecking = "strict"
  }
}

But Pyrefly logs:

WARN: Extra keys found in config: analyzer, diagnostics, typechecking 

and the server behaves as if no settings were provided.

Temporary Workaround

Put the same values inside:

[tool.pyrefly]
analyzer = true
diagnostics = true
typechecking = "strict"

This applies correctly and enables diagnostics/typechecking.

Proposed Fix

The fix appears to be simply allowing client-side settings to be applied during initialization. The linked PR (#1640) is exactly the missing piece — exposing and merging client configuration inside the initialize handshake.

If the maintainers are open to it, I can help test or refine that behavior.

dvctl avatar Nov 21 '25 11:11 dvctl

this'll merge soon and it'll be available in the next release (monday). the task will auto-close when it merges, but free free to reopen if it doesn't fix your issue. thanks for reporting!

kinto0 avatar Nov 21 '25 21:11 kinto0

Solved

Edit: I was using the wrong setting, the issue is solved, sorry for the trouble.

Here is the configuration I am using with coc.nvim:

{
    "languageserver": {
        "pyrefly": {
            "command": "pyrefly",
            "args": ["lsp"],
            "filetypes": ["python"],
            "rootPatterns": ["pyrefly.toml", "pyproject.toml", ".git"],
            "initializationOptions": {
                "pyrefly": {
                  "displayTypeErrors": "force-on"
                }
            }
    }
}

the parameter is documented here: https://pyrefly.org/en/docs/IDE/#customization

The following content (below) is outdated

I still encounter this issue, even after the fix.

Environment

  • pyrefly 0.43.1 (built from source: 527325a67) (after the fix)
  • OS: Arch Linux
  • nvim v0.11.5 + coc.nvim 0.0.82-d425554 2025-06-01 02:02:45 +0800

Coc configuration

I asked the coc.nvim project how to provide the initialization options to pyrefly, the discussion is here (https://github.com/neoclide/coc.nvim/discussions/5489)

My coc-settings.json configuration is the following:

{
    "languageserver": {
        "pyrefly": {
            "command": "pyrefly",
            "args": ["lsp"],
            "filetypes": ["python"],
            "rootPatterns": ["pyrefly.toml", "pyproject.toml", ".git"],
            "initializationOptions": {
                "pyrefly": {
                  "diagnostics": true
                }
            }
    }
}

Despite this configuration the diagnostics are not enabled.

When I explicitly toggle diagnostics ON inside the pyproject.toml it works fine:

[tool.pyrefly]
diagnostics = true

I can provide additional logs if needed, thanks for your help.

PicoJr avatar Nov 25 '25 18:11 PicoJr

Solved

Edit: I was using the wrong setting, the issue is solved, sorry for the trouble.

Here is the configuration I am using with coc.nvim:

{ "languageserver": { "pyrefly": { "command": "pyrefly", "args": ["lsp"], "filetypes": ["python"], "rootPatterns": ["pyrefly.toml", "pyproject.toml", ".git"], "initializationOptions": { "pyrefly": { "displayTypeErrors": "force-on" } } } }

the parameter is documented here: https://pyrefly.org/en/docs/IDE/#customization

The following content (below) is outdated

I still encounter this issue, even after the fix.

Environment

  • pyrefly 0.43.1 (built from source: 527325a) (after the fix)
  • OS: Arch Linux
  • nvim v0.11.5 + coc.nvim 0.0.82-d425554 2025-06-01 02:02:45 +0800

Coc configuration

I asked the coc.nvim project how to provide the initialization options to pyrefly, the discussion is here (https://github.com/neoclide/coc.nvim/discussions/5489)

My coc-settings.json configuration is the following:

{ "languageserver": { "pyrefly": { "command": "pyrefly", "args": ["lsp"], "filetypes": ["python"], "rootPatterns": ["pyrefly.toml", "pyproject.toml", ".git"], "initializationOptions": { "pyrefly": { "diagnostics": true } } } } Despite this configuration the diagnostics are not enabled.

When I explicitly toggle diagnostics ON inside the pyproject.toml it works fine:

[tool.pyrefly] diagnostics = true I can provide additional logs if needed, thanks for your help.

hey, thanks for the detailed report. what was changed to fix it? i'd love to make it easier to setup if there's an obvious confusion here.

kinto0 avatar Nov 26 '25 16:11 kinto0

@kinto0

A bit of context: I recently switched from pyright to pyrefly as my default LSP for developing Python code on neovim. I was surprised that neovim displayed no red squiggly lines even when my code was blatantly wrong, I had done minimal configuration of pyrefly at this point, I only copied the section from the official documentation for using pyrefly with nvim + coc.nvim (https://pyrefly.org/en/docs/IDE/#vimneovim--cocnvim). I browsed the github issues of pyrefly for someone using nvim + coc.nvim facing the same issue, that's when I found this issue, and when I added the tool.pyrefly to my project.toml the diagnostics were there, that's when I figured out I needed to supply additional configuration so that pyrefly enables diagnostics globally rather than editing pyproject.toml everywhere. This specific issue is not about coc.nvim and I wasn't sure how to supply the equivalent of diagnostics = true to pyrefly using coc-settings.json, that's when I figured out I should ask the coc.nvim project. Once I knew how to supply initializationOptions I figure out there was no diagnostics parameter (I read pyrefly source code), by grepping around I found the documentation of initializationOptions, that's when I figured out what to supply to force displaying type errors.

I think it might help future users to add

            "initializationOptions": {
                "pyrefly": {
                  "displayTypeErrors": "default"
                }
            }

To the configuration described here https://pyrefly.org/en/docs/IDE/#vimneovim--cocnvim

And maybe mention to set it to "force-on" to force displaying type errors globally but I am not sure what's best: I don't know how other users use pyrefly.

PicoJr avatar Nov 26 '25 17:11 PicoJr

I'm writing a tool that uses pygls.lsp.client and I can't succeed in pointing pyrefly to a project directory. I can start the server, point it at individual files and self.client.text_document_document_symbol_async(params) works as expected.

But once I try to use self.client.workspace_symbol_async(params) I don't even know where it is looking at.

✅ Found 9 global functions matching 'main' language python.
  • main in /tmp/pyrefly_bundled_typeshed_646739975e33/uuid.pyi L106 [Function]
  • main in /tmp/pyrefly_bundled_typeshed_646739975e33/ast.pyi L2107 [Function]
  • main_thread in /tmp/pyrefly_bundled_typeshed_646739975e33/threading.pyi L68 [Function]

This is how I initialize the server (but it seems it is ignored)

    self._server_task = asyncio.create_task(
        self.client.start_io(*command, cwd=str(self.project_root))
    )
...
    project_uri = self.project_root.as_uri()
    project_path = str(self.project_root)
    print(f"uri: {project_uri} path: {project_path}")
    workspace_folder = types.WorkspaceFolder(uri=project_uri, name=self.project_root.name)
    # workspace_folder = None
    params = types.InitializeParams(
      process_id=os.getpid(),
      root_uri=project_uri,
      initialization_options={
        "projectRoot" : project_path,
        "indexing": True,
        "analysis": {
          "autoSearchPaths": True,
          "useLibraryCodeForTypes": True,
          "diagnosticMode": "workspace",
          "extraPaths": [project_path],
        },
        "settings": {
          "python": {
            "analysis": {"indexing": True}
            }
          }
      },
      workspace_folders=[workspace_folder],
      capabilities=types.ClientCapabilities(
        window=types.WindowClientCapabilities(work_done_progress=True),
        workspace=types.WorkspaceClientCapabilities(
          configuration=True,  # Tells server we can handle the pythonPath request
          workspace_folders=True,
          symbol=types.WorkspaceSymbolClientCapabilities(
            dynamic_registration=True,
          ),
        ),
        # general=types.GeneralClientCapabilities(
        #   stale_request_support=True
        # ),
        text_document=types.TextDocumentClientCapabilities(
          synchronization=types.TextDocumentSyncClientCapabilities(
            dynamic_registration=True,
            will_save=True,
            did_save=True,
          ),
          document_symbol=types.DocumentSymbolClientCapabilities(hierarchical_document_symbol_support=True),
        ),
      ),
    )

and this is the pyrefly generated part of my pyproject.toml

[tool.pyrefly]
project-includes = ["llm_lite"]
project-excludes = [
    "**/node_modules",
    "**/__pycache__",
]
python-version = "3.13.0"
infer-with-first-use = false

somehow the same program works as expected with clangd and can find global symbols.

Ivan-SB avatar Mar 27 '26 01:03 Ivan-SB

I'm writing a tool that uses pygls.lsp.client and I can't succeed in pointing pyrefly to a project directory. I can start the server, point it at individual files and self.client.text_document_document_symbol_async(params) works as expected.

But once I try to use self.client.workspace_symbol_async(params) I don't even know where it is looking at.

✅ Found 9 global functions matching 'main' language python.
  • main in /tmp/pyrefly_bundled_typeshed_646739975e33/uuid.pyi L106 [Function]
  • main in /tmp/pyrefly_bundled_typeshed_646739975e33/ast.pyi L2107 [Function]
  • main_thread in /tmp/pyrefly_bundled_typeshed_646739975e33/threading.pyi L68 [Function]

This is how I initialize the server (but it seems it is ignored)

    self._server_task = asyncio.create_task(
        self.client.start_io(*command, cwd=str(self.project_root))
    )
...
    project_uri = self.project_root.as_uri()
    project_path = str(self.project_root)
    print(f"uri: {project_uri} path: {project_path}")
    workspace_folder = types.WorkspaceFolder(uri=project_uri, name=self.project_root.name)
    # workspace_folder = None
    params = types.InitializeParams(
      process_id=os.getpid(),
      root_uri=project_uri,
      initialization_options={
        "projectRoot" : project_path,
        "indexing": True,
        "analysis": {
          "autoSearchPaths": True,
          "useLibraryCodeForTypes": True,
          "diagnosticMode": "workspace",
          "extraPaths": [project_path],
        },
        "settings": {
          "python": {
            "analysis": {"indexing": True}
            }
          }
      },
      workspace_folders=[workspace_folder],
      capabilities=types.ClientCapabilities(
        window=types.WindowClientCapabilities(work_done_progress=True),
        workspace=types.WorkspaceClientCapabilities(
          configuration=True,  # Tells server we can handle the pythonPath request
          workspace_folders=True,
          symbol=types.WorkspaceSymbolClientCapabilities(
            dynamic_registration=True,
          ),
        ),
        # general=types.GeneralClientCapabilities(
        #   stale_request_support=True
        # ),
        text_document=types.TextDocumentClientCapabilities(
          synchronization=types.TextDocumentSyncClientCapabilities(
            dynamic_registration=True,
            will_save=True,
            did_save=True,
          ),
          document_symbol=types.DocumentSymbolClientCapabilities(hierarchical_document_symbol_support=True),
        ),
      ),
    )

and this is the pyrefly generated part of my pyproject.toml

[tool.pyrefly]
project-includes = ["llm_lite"]
project-excludes = [
    "**/node_modules",
    "**/__pycache__",
]
python-version = "3.13.0"
infer-with-first-use = false

somehow the same program works as expected with clangd and can find global symbols.

maybe it's easier to chat about this in our discord or make a separate github issue? I think your use case is different - I wonder if you are just seending the workspace symbol request too early (before the workspace is indexed) or the workspace indexing is disabled

kinto0 avatar Mar 27 '26 19:03 kinto0