Skip to content

[ruff server] Find config for files outside of open workspaces#22570

Open
ZedThree wants to merge 1 commit intoastral-sh:mainfrom
PlasmaFAIR:fix-17944-alt
Open

[ruff server] Find config for files outside of open workspaces#22570
ZedThree wants to merge 1 commit intoastral-sh:mainfrom
PlasmaFAIR:fix-17944-alt

Conversation

@ZedThree
Copy link
Contributor

Fixes #17944

There's two different routes we can go down for finding settings for such files:

  • the file is below an open workspace, so we can add any settings we find to that workspace
  • the file is not below an open workspace, in which case we can't cache them

Given this filesystem layout:

.
├── dir_0
│   ├── ruff.toml
│   └── test_0.py
└── dir_1
    ├── ruff.toml
    └── test_1.py

This fixes:

  • cwd at top-level, opening dir_0/test_0.py
  • cwd at top-level, opening dir_0/test_0.py, then ../dir_1/test_1.py in same session
  • cwd in dir_0, opening ../dir_1/test_1.py
  • cwd in dir_0, opening test_0.py first, then ../dir_1/test_1.py in same session

without negatively affecting non-default workspaces (such as opening the folder in VS Code or lsp-mode in Emacs).

The main downside to this approach is the lack of workspace for files in dir_1 -- we can't share already parsed settings.


This doesn't work for the situation where we're opening a file in a nested directory below the default workspace, where we do have a config file:

.
├── subdir
│   ├── ruff.toml
│   └── test_1.py
├── ruff.toml
└── test_0.py

Opening test_0.py first (in single file mode), and then opening subdir/test_1.py still uses the settings from ./ruff.toml instead of subdir/ruff.toml

Test Plan

Tested using the files described in this comment

I noticed that ty has e2e testing of its lsp server, but this hasn't been implemented for ruff yet. I think this would need something like that to test automatically

Fixes astral-sh#17944

There's two different routes we can go down for finding settings for such files:
- the file is below an open workspace, so we can add any settings we find to
  that workspace
- the file is not below an open workspace, in which case we can't cache them

Given this filesystem layout:

```
.
├── dir_0
│   ├── ruff.toml
│   └── test_0.py
└── dir_1
    ├── ruff.toml
    └── test_1.py
```

This fixes:

- cwd at top-level, opening `dir_0/test_0.py`
- cwd at top-level, opening `dir_0/test_0.py`, then `../dir_1/test_1.py` in same session
- cwd in `dir_0`, opening `../dir_1/test_1.py`
- cwd in `dir_0`, opening `test_0.py` first, then `../dir_1/test_1.py` in same session

without negatively affecting non-default workspaces (such as opening the folder
in VS Code or `lsp-mode` in Emacs).

The main downside to this approach is the lack of workspace for files in `dir_1`
-- we can't share already parsed settings.

---

This doesn't work for the situation where we're opening a file in a nested
directory below the default workspace, where we _do_ have a config file:

```
.
├── subdir
│   ├── ruff.toml
│   └── test_1.py
├── ruff.toml
└── test_0.py
```

Opening `test_0.py` first (in single file mode), and then opening
`subdir/test_1.py` still uses the settings from `./ruff.toml` instead of `subdir/ruff.toml`
@AlexWaygood AlexWaygood added the server Related to the LSP server label Jan 14, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 14, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@dhruvmanila
Copy link
Member

(I've this on my TODO, I'll look at this later today after a meeting.)

@dhruvmanila
Copy link
Member

Thank you for this PR!

The setting resolution is a little bit complicated for the Ruff language server. I think it would be very useful (and would also help me review this faster) if you can link the specific changes to the directory structure which it tries to solve. For example, which directory structure is RuffSettings::fallback trying to resolve?

@ZedThree
Copy link
Contributor Author

Previously, RuffSettings::fallback just tried to look for user-specific config files. Now, it looks in the parent directory of the file being opened, and then its ancestors. So it now ends up searching the following in order, for example:

  • /tmp/mvce/dir_0/
  • /tmp/mvce/
  • /tmp/
  • ~/.config/ruff/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the LSP server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server doesn't detect ruff config for files not under working directory of server

3 participants