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

Automatically ignore unsupported command-line arguments #145

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,20 @@ Upon successful installation, you should see errors surfaced directly in your ed

![](https://user-images.githubusercontent.com/1309177/209418462-ae106d1f-dbc3-4d53-bae2-66bfccc3e841.png)


### Example: Kate

To use `ruff-lsp` with [Kate](https://kate-editor.org/), add something like the following to
the LSP client's `settings.json`:

```json
{
"servers": {
"python": {
"command": ["ruff-lsp"],
"url": "https://github.com/charliermarsh/ruff-lsp",
"highlightingModeRegex": "^Python$"
}
"servers": {
"python": {
"command": ["ruff-lsp"],
"url": "https://github.com/charliermarsh/ruff-lsp",
"highlightingModeRegex": "^Python$"
}
}
}
```

Expand All @@ -181,15 +180,15 @@ the LSP client's `settings.json`:
The exact mechanism by which settings will be passed to `ruff-lsp` will vary by editor. However,
the following settings are supported:

| Settings | Default | Description |
| ---------------- | ------- | ---------------------------------------------------------------------------------------- |
| args | `[]` | Custom arguments passed to `ruff`. E.g `"args": ["--config=/path/to/pyproject.toml"]`. |
| logLevel | `error` | Sets the tracing level for the extension. |
| path | `[]` | Setting to provide custom `ruff` executables, to try in order. E.g. `["/path/to/ruff"]`. |
| interpreter | `[]` | Path to a Python interpreter to use to run the linter server. |
| showNotification | `off` | Setting to control when a notification is shown. |
| organizeImports | `true` | Whether to register Ruff as capable of handling `source.organizeImports` actions. |
| fixAll | `true` | Whether to register Ruff as capable of handling `source.fixAll` actions. |
| Settings | Default | Description |
| ---------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| args | `[]` | Additional command-line arguments to pass to `ruff`, e.g., `"args": ["--config=/path/to/pyproject.toml"]`. Supports a subset of Ruff's command-line arguments, ignoring those that are required to operate the LSP, like `--force-exclude` and `--verbose`. |
| logLevel | `error` | Sets the tracing level for the extension: `error`, `warn`, `info`, or `debug`. |
| path | `[]` | Path to a custom `ruff` executable, e.g., `["/path/to/ruff"]`. |
| interpreter | `[]` | Path to a Python interpreter to use to run the linter server. |
| showNotification | `off` | Setting to control when a notification is shown. |
| organizeImports | `true` | Whether to register Ruff as capable of handling `source.organizeImports` actions. |
| fixAll | `true` | Whether to register Ruff as capable of handling `source.fixAll` actions. |

## Development

Expand Down
41 changes: 40 additions & 1 deletion ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@

TOOL_MODULE = "ruff.exe" if platform.system() == "Windows" else "ruff"
TOOL_DISPLAY = "Ruff"

# Arguments provided to every Ruff invocation.
TOOL_ARGS = [
"--force-exclude",
"--no-cache",
Expand All @@ -99,6 +101,37 @@
"-",
]

# Arguments that are not allowed to be passed to Ruff.
TOOL_NON_ARGS = [
# Arguments that enforce required behavior. These can be ignored with a warning.
"--force-exclude",
"--no-cache",
"--no-fix",
"--quiet",
# Arguments that contradict the required behavior. These can be ignored with a
# warning.
"--diff",
"--exit-non-zero-on-fix",
"-e",
"--exit-zero",
"--fix",
"--fix-only",
"-h",
"--help",
"--no-force-exclude",
"--show-files",
"--show-fixes",
"--show-settings",
"--show-source",
"--silent",
"--statistics",
"--verbose",
"-w",
"--watch",
# Arguments that are not supported at all, and will error when provided.
# "--stdin-filename",
# "--format",
Comment on lines +132 to +133
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@charliermarsh why are these commented out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because they aren’t flags, they’re positional arguments, so they’re followed by another argument. I figured this wasn’t worth handling since they would error anyway.

]

###
# Linting.
Expand Down Expand Up @@ -1017,7 +1050,13 @@ def _run_tool_on_document(
settings = _get_settings_by_document(document)

executable = _executable_path(settings)
argv: list[str] = [executable] + TOOL_ARGS + settings["args"] + list(extra_args)
argv: list[str] = [executable] + TOOL_ARGS + list(extra_args)

for arg in settings["args"]:
if arg in TOOL_NON_ARGS:
log_to_output(f"Ignoring unsupported argument: {arg}")
else:
argv.append(arg)

# If we're trying to run a single rule, add it to the command line, and disable
# all other rules (if the Ruff version is sufficiently recent).
Expand Down