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

Should custom config override local pyproject.toml / ruff.toml? #91

Open
iod-ine opened this issue May 16, 2024 · 9 comments
Open

Should custom config override local pyproject.toml / ruff.toml? #91

iod-ine opened this issue May 16, 2024 · 9 comments

Comments

@iod-ine
Copy link

iod-ine commented May 16, 2024

Currently, when custom config is set during setup, project-local configuration files are ignored. I wonder whether this behavior is the result of an explicit design decision, or if it is open for discussion and subject to change. I imagined the setting to be a default config, used not to set the same settings across all projects, but to separate the configuration of LSP and Ruff and overridden by local settings.

@jhossbach
Copy link
Member

python-lsp-ruff should not ignore project-local settings other than stuff like unsafe fixes, and extend-* settings. The idea is that any config settings like select are ignored if a project ruff config is present.
You can still add additional linter settings for your editor (e.g. documentation linting "D" codes) that are not used by the project but you find good-to-have, these will not be overriden if a ruff config exists.
Does that clear up things/make sense?

@iod-ine
Copy link
Author

iod-ine commented May 16, 2024

Yes, thank you :)

But more specifically, I am interested in the config setting. Here is the snippet of my Neovim config:

lspconfig.pylsp.setup({
    settings = {
        pylsp = {
            plugins = {
                ruff = {
                    enabled = true,
                    formatEnabled = true,
                    config = "~/.dotfiles/ruff.toml",
                }
            },
        },
    },
})

This makes any Python file use my standard Ruff config. But that also makes python-lsp-ruff ignore local ruff.toml, which I expected would take precedence over the default ~/.dotfiles/ruff.toml. Is that desired? Or should the presence of a local config file override not only select, exclude, and others, but also config?

@jhossbach
Copy link
Member

Interesting, the desired behavior should be to ignore the config setting if a local ruff.toml is present. I don't see any reason why this should not be the case, can you add

lspconfig.pylsp.setup({
    settings = {
        pylsp = {
            plugins = {
                ruff = {
                    enabled = true,
+                   executable = {"<path-to-pylsp>", "-vvv", "--log-file", "/tmp/lsp.log"},
                    formatEnabled = true,
                    config = "~/.dotfiles/ruff.toml",
                }
            },
        },
    },
})

And then search for a line like "Found existing configuration for ruff, skipping pylsp config." in the logs?
If it doesn't exist that means that your ruff.toml is not properly detected by pylsp.

@iod-ine
Copy link
Author

iod-ine commented May 17, 2024

Had to put it like this:

lspconfig.pylsp.setup({
    cmd = {"<path-to-pylsp>", "-vvv", "--log-file", "/tmp/lsp.log"},
    settings = {
        pylsp = {
            plugins = {
                ruff = {
                    enabled = true,
                    formatEnabled = true,
                    config = "~/.dotfiles/ruff.toml",
                }
            },
        },
    },
})

Found something interesting: a local ruff.toml does not override anything at all, even select. A local pyproject.toml however does everything as expected, overriding everything it should. At the same time, if nothing is configured in the setup call, a local ruff.toml works fine, but there is no "skipping pylsp config" line in the log.

@jhossbach
Copy link
Member

Hmm, can you post the full log?

@iod-ine
Copy link
Author

iod-ine commented May 21, 2024

Here are all three possible states I tried:
[REDACTED]

@jhossbach
Copy link
Member

jhossbach commented May 21, 2024

I took the liberty of removing the logs since your logs were showing the contents of the python file you were editing and I wasn't sure whether it contained sensitive information.
The problem is that pylsp cannot find your project root directory. As an example, this does not find a project root:

.
├── ruff.toml
└── test_project
    ├── __init__.py
    └── test.py

Adding a pyproject.toml to the . directory will tell pylsp that this is the root directory from what I can tell, @ccordoba12 can you shed some light on how pylsp figures out the root used as workspace.root_path?

In short: This method does not work since workspace.root_path is None:

ruff_toml = find_parents(
workspace.root_path, document_path, ["ruff.toml", ".ruff.toml"]
)

@iod-ine
Copy link
Author

iod-ine commented May 21, 2024

Thank you! I made sure to remove anything sensitive, just wanted to test on a realistic file that made it easy to see what configuration is being applied.

Yes, just tested that adding a blank pyproject.toml indeed makes a difference and the config is loaded properly. Adding a .git directory has the same effect. I can use either of those as a workaround for my use cases where it matters.

@ccordoba12
Copy link
Member

Can you shed some light on how pylsp figures out the root used as workspace.root_path?

Sorry for the late reply. workspace.root_path is the path of the directory currently opened by your IDE or editor.

Some IDEs always force you to work with a project, and in that case its path is workspace.root_path.
However, most editors don't do that (e.g. VSCode, Vim, etc) and in that case workspace.root_path can be your home or None (not so sure about the last one though).

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