Add extension mapping to configuration file options#23384
Conversation
|
| fs::write( | ||
| &pyproject_toml, | ||
| r#" | ||
| [tool.ruff] |
There was a problem hiding this comment.
Can you say a bit more about why this is a global option? I think it should be scoped to the linter and the formatter such that it can be configured individually for tool.ruff.lint and tool.ruff.format. The CLI option is also scoped to individual subcommands.
$ ruff check --extension ipynb:python ~/playground/ruff/notebooks/extension.ipynb
I002 [*] Missing required import: `from __future__ import annotations`
--> /Users/dhruv/playground/ruff/notebooks/extension.ipynb:1:1
help: Insert required import: `from __future__ import annotations`
F401 [*] `os` imported but unused
--> /Users/dhruv/playground/ruff/notebooks/extension.ipynb:1:8
|
1 | import os
| ^^
|
help: Remove unused import: `os`
Found 2 errors.
[*] 2 fixable with the `--fix` option.
$ ruff --extension ipynb:python check ~/playground/ruff/notebooks/extension.ipynb
error: unexpected argument '--extension' found
tip: 'check --extension' exists
Usage: ruff [OPTIONS] <COMMAND>
For more information, try '--help'.There was a problem hiding this comment.
A few reasons:
- It's already defined as a global value on the
Configurationstruct, and that value gets cloned to both the formatter and linter settings. - AFAICT passing
--extension, even though it's arguments to check/format commands, still sets the global value before creating the finalConfigurationobject, meaning both linter and formatter settings objects will have identicalExtensionMappingobjects. - I believe it would be rare (and very weird) for a user to want the checker and formatter to each treat the same file/extension as a different "language".
- I think the UX would be worse for the normal case, requiring users to duplicate this config value in two places (and make sure they stay in sync) in order to make ruff work with their custom file extensions.
There was a problem hiding this comment.
One more reason: to potentially include configured file extensions in discovery, this needs to be global because the FileResolverSettings are global and file discovery happens without knowing or caring whether ruff is being run for linting or formatting.
There was a problem hiding this comment.
Thanks for the context! I think those are reasonable 👍
There was a problem hiding this comment.
- I think the UX would be worse for the normal case, requiring users to duplicate this config value in two places (and make sure they stay in sync) in order to make ruff work with their custom file extensions.
Yeah, definitely, I didn't think of this. And, I think this use-case far outweigh duplicating it.
ntBre
left a comment
There was a problem hiding this comment.
This looks reasonable to me once Dhruv's concerns are resolved.
New `extension` configuration option takes a dictionary mapping custom file extensions (keys) to languages by name (values). Eg,
```toml
[tool.ruff]
extension = {qmd="markdown"}
```
Issue astral-sh#23204
Issue #23204