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

LSP: Format File -- ruff.toml settings ignored #92

Open
AndydeCleyre opened this issue Dec 10, 2024 · 5 comments
Open

LSP: Format File -- ruff.toml settings ignored #92

AndydeCleyre opened this issue Dec 10, 2024 · 5 comments
Labels
question Further information is requested

Comments

@AndydeCleyre
Copy link

Hello!

I have the following ~/.config/ruff/ruff.toml, and I'd like this plugin to use that configuration, at least in the absence of a more local configuration file.

ruff.toml
line-length = 99

[format]
quote-style = "preserve"
skip-magic-trailing-comma = true

[lint]
select = ["ALL"]
ignore = ["BLE", "COM812", "EM", "FIX", "ISC001", "PERF203", "PGH003", "Q000", "T20", "TD002", "TD003", "TRY003", "TRY203"]

[lint.flake8-annotations]
suppress-none-returning = true

[lint.isort]
combine-as-imports = true
split-on-trailing-comma = false

[lint.pydocstyle]
convention = "pep257"

[lint.pylint]
max-args = 6

To that end, I've configured this plugin as:

{
  "initializationOptions": {
    "settings": {
      "configuration": "~/.config/ruff/ruff.toml",
      "configurationPreference": "filesystemFirst"
    }
  }
}

I test with the following main.py:

X_PATTERN = r'https://x\.com/[^/]+/status/\d+'
single = 'single'
double = "double"
(single,double,)

On the command line, I can see what ruff format will do, following my configuration:

$ ruff format --diff main.py
--- main.py
+++ main.py
@@ -1,4 +1,4 @@
 X_PATTERN = r'https://x\.com/[^/]+/status/\d+'
 single = 'single'
 double = "double"
-(single,double,)
+(single, double)

But when I choose LSP: Format File from the command palette, the following changes are made:

--- main.py
+++ main.py
@@ -1,4 +1,7 @@
-X_PATTERN = r'https://x\.com/[^/]+/status/\d+'
-single = 'single'
+X_PATTERN = r"https://x\.com/[^/]+/status/\d+"
+single = "single"
 double = "double"
-(single,double,)
+(
+    single,
+    double,
+)

Here is my troubleshooting output:

LSP: Troubleshoot Server

Troubleshooting: LSP-ruff

Version

  • LSP: 2.3.0
  • Sublime Text: 4180

Server Test Run

  • exit code: 0
  • output

Server Configuration

  • command
[
  "$server_path",
  "server"
]
  • shell command
"/home/andy/.cache/sublime-text/Package Storage/LSP-ruff/bin/ruff" server
  • selector
source.python
  • priority_selector
source.python
  • init_options
{
  "settings": {
    "codeAction": {
      "disableRuleComment": {
        "enable": true
      },
      "fixViolation": {
        "enable": true
      }
    },
    "configuration": "~/.config/ruff/ruff.toml",
    "configurationPreference": "filesystemFirst",
    "exclude": null,
    "fixAll": true,
    "format": {
      "preview": null
    },
    "lineLength": null,
    "lint": {
      "enable": true,
      "extendSelect": null,
      "ignore": null,
      "preview": null,
      "select": null
    },
    "logLevel": "error",
    "organizeImports": true
  }
}
  • settings
{}
  • env
{
  "PATH": "/home/andy/.cache/sublime-text/Package Storage/LSP-ruff/bin:"
}

Active view

  • File name
/home/andy/Code/rufflsptest/main.py
  • Settings
{
  "auto_complete_selector": "meta.tag, source - comment - string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc",
  "lsp_active": true,
  "syntax": "Packages/Python/Python.sublime-syntax"
}
  • base scope
source.python

Project / Workspace

  • folders
[
  "/home/andy/Code/rufflsptest"
]
  • is project: False

LSP configuration

{
  "clients": {
    "nimlsp": {
      "command": [
        "nimlsp"
      ],
      "enabled": true,
      "selector": "source.nim"
    }
  }
}

System PATH

  • /home/andy/.local/share/venvs/b85b12618c824167e5af3f14cdf57a1b/venv/bin
  • /home/andy/.local/share/mise/installs/cargo-broot/1.44.2/bin
  • /home/andy/.local/share/mise/installs/cargo-ncspot/1.2.1/bin
  • /home/andy/.local/share/mise/installs/cargo-prqlc/0.13.2/bin
  • /home/andy/.local/share/mise/installs/cargo-skim/0.15.5/bin
  • /home/andy/.local/share/mise/installs/ubi-asciinema-agg/1.5.0/bin
  • /home/andy/.local/share/mise/installs/ubi-bensadeh-tailspin/4.0.0/bin
  • /home/andy/.local/share/mise/installs/ubi-containers-podlet/0.3.0/bin
  • /home/andy/.local/share/mise/installs/ubi-eza-community-eza/0.20.11/bin
  • /home/andy/.local/share/mise/installs/ubi-input-username-rescrobbled/0.7.1/bin
  • /home/andy/.local/share/mise/installs/ubi-jhbabon-scout/2.8.0/bin
  • /home/andy/.local/share/mise/installs/ubi-koki-develop-gat/0.19.1/bin
  • /home/andy/.local/share/mise/installs/ubi-libxmq-xmq/2.18.0/bin
  • /home/andy/.local/share/mise/installs/ubi-rami3l-pacaptr/0.22.0/bin
  • /home/andy/.local/share/mise/installs/ubi-shshemi-tabiew/0.7.1/bin
  • /home/andy/.local/share/mise/installs/ubi-walles-moar/1.30.0/bin
  • /home/andy/.local/share/mise/installs/ubi-walles-riff/3.3.7/bin
  • /home/andy/.local/share/mise/installs/dasel/2.8.1/bin
  • /home/andy/.local/share/mise/installs/difftastic/0.61.0/bin
  • /home/andy/.local/share/mise/installs/fzf/0.56.3/bin
  • /home/andy/.local/share/mise/installs/gh/2.63.2/bin
  • /home/andy/.local/share/mise/installs/hyperfine/1.19.0/bin
  • /home/andy/.local/share/mise/installs/just/1.37.0/bin
  • /home/andy/.local/share/mise/installs/lua/5.4.7/bin
  • /home/andy/.local/share/mise/installs/lua/5.4.7/luarocks/bin
  • /home/andy/.local/share/mise/installs/python/3.13.0/bin
  • /home/andy/.local/share/mise/installs/shellcheck/0.10.0/bin
  • /home/andy/.local/share/mise/installs/usage/1.3.4/bin
  • /home/andy/.local/share/mise/installs/cargo-pacdef/1.6.0/bin
  • /home/andy/.local/share/mise/installs/ubi-glehmann-yage/0.5.0/bin
  • /home/andy/.local/share/mise/installs/ubi-schollz-croc/10.1.1/bin
  • /home/andy/.local/share/mise/installs/ubi-yaml-yamlscript/0.1.86/bin
  • /home/andy/.local/bin
  • /home/andy/.cargo/bin
  • /usr/local/sbin
  • /usr/local/bin
  • /usr/sbin
  • /usr/bin
  • /sbin
  • /bin
  • /usr/games
  • /usr/local/games
  • /snap/bin
  • /usr/lib/execline/bin
@LDAP
Copy link
Collaborator

LDAP commented Dec 10, 2024

Can you provide an LSP log panel output when formatting?

@AndydeCleyre
Copy link
Author

LSP Log Panel Content after launch and format
:: [18:14:05.120] --> LSP-ruff initialize (1): {'processId': 100344, 'clientInfo': {'name': 'Sublime Text LSP', 'version': '2.3.0'}, 'rootUri': 'file:///home/andy/Code/rufflsptest', 'rootPath': '/home/andy/Code/rufflsptest', 'workspaceFolders': [{'name': 'rufflsptest', 'uri': 'file:///home/andy/Code/rufflsptest'}], 'capabilities': {'general': {'regularExpressions': {'engine': 'ECMAScript'}, 'markdown': {'parser': 'Python-Markdown', 'version': '3.2.2'}}, 'textDocument': {'synchronization': {'dynamicRegistration': True, 'didSave': True, 'willSave': True, 'willSaveWaitUntil': True}, 'hover': {'dynamicRegistration': True, 'contentFormat': ['markdown', 'plaintext']}, 'completion': {'dynamicRegistration': True, 'completionItem': {'snippetSupport': True, 'deprecatedSupport': True, 'documentationFormat': ['markdown', 'plaintext'], 'tagSupport': {'valueSet': [1]}, 'resolveSupport': {'properties': ['detail', 'documentation', 'additionalTextEdits']}, 'insertReplaceSupport': True, 'insertTextModeSupport': {'valueSet': [2]}, 'labelDetailsSupport': True}, 'completionItemKind': {'valueSet': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]}, 'insertTextMode': 2, 'completionList': {'itemDefaults': ['editRange', 'insertTextFormat', 'data']}}, 'signatureHelp': {'dynamicRegistration': True, 'contextSupport': True, 'signatureInformation': {'activeParameterSupport': True, 'documentationFormat': ['markdown', 'plaintext'], 'parameterInformation': {'labelOffsetSupport': True}}}, 'references': {'dynamicRegistration': True}, 'documentHighlight': {'dynamicRegistration': True}, 'documentSymbol': {'dynamicRegistration': True, 'hierarchicalDocumentSymbolSupport': True, 'symbolKind': {'valueSet': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]}, 'tagSupport': {'valueSet': [1]}}, 'documentLink': {'dynamicRegistration': True, 'tooltipSupport': True}, 'formatting': {'dynamicRegistration': True}, 'rangeFormatting': {'dynamicRegistration': True, 'rangesSupport': True}, 'declaration': {'dynamicRegistration': True, 'linkSupport': True}, 'definition': {'dynamicRegistration': True, 'linkSupport': True}, 'typeDefinition': {'dynamicRegistration': True, 'linkSupport': True}, 'implementation': {'dynamicRegistration': True, 'linkSupport': True}, 'codeAction': {'dynamicRegistration': True, 'codeActionLiteralSupport': {'codeActionKind': {'valueSet': ['quickfix', 'refactor', 'refactor.extract', 'refactor.inline', 'refactor.rewrite', 'source.fixAll', 'source.organizeImports']}}, 'dataSupport': True, 'isPreferredSupport': True, 'resolveSupport': {'properties': ['edit']}}, 'rename': {'dynamicRegistration': True, 'prepareSupport': True, 'prepareSupportDefaultBehavior': 1}, 'colorProvider': {'dynamicRegistration': True}, 'publishDiagnostics': {'relatedInformation': True, 'tagSupport': {'valueSet': [1, 2]}, 'versionSupport': True, 'codeDescriptionSupport': True, 'dataSupport': True}, 'diagnostic': {'dynamicRegistration': True, 'relatedDocumentSupport': True}, 'selectionRange': {'dynamicRegistration': True}, 'foldingRange': {'dynamicRegistration': True, 'foldingRangeKind': {'valueSet': ['comment', 'imports', 'region']}}, 'codeLens': {'dynamicRegistration': True}, 'inlayHint': {'dynamicRegistration': True, 'resolveSupport': {'properties': ['textEdits', 'label.command']}}, 'semanticTokens': {'dynamicRegistration': True, 'requests': {'range': True, 'full': {'delta': True}}, 'tokenTypes': ['namespace', 'type', 'class', 'enum', 'interface', 'struct', 'typeParameter', 'parameter', 'variable', 'property', 'enumMember', 'event', 'function', 'method', 'macro', 'keyword', 'modifier', 'comment', 'string', 'number', 'regexp', 'operator', 'decorator', 'label'], 'tokenModifiers': ['declaration', 'definition', 'readonly', 'static', 'deprecated', 'abstract', 'async', 'modification', 'documentation', 'defaultLibrary'], 'formats': ['relative'], 'overlappingTokenSupport': False, 'multilineTokenSupport': True, 'augmentsSyntaxTokens': True}, 'callHierarchy': {'dynamicRegistration': True}, 'typeHierarchy': {'dynamicRegistration': True}}, 'workspace': {'applyEdit': True, 'didChangeConfiguration': {'dynamicRegistration': True}, 'executeCommand': {}, 'workspaceEdit': {'documentChanges': True, 'failureHandling': 'abort'}, 'workspaceFolders': True, 'symbol': {'dynamicRegistration': True, 'resolveSupport': {'properties': ['location.range']}, 'symbolKind': {'valueSet': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]}, 'tagSupport': {'valueSet': [1]}}, 'configuration': True, 'codeLens': {'refreshSupport': True}, 'inlayHint': {'refreshSupport': True}, 'semanticTokens': {'refreshSupport': True}, 'diagnostics': {'refreshSupport': True}}, 'window': {'showDocument': {'support': True}, 'showMessage': {'messageActionItem': {'additionalPropertiesSupport': True}}, 'workDoneProgress': True}}, 'initializationOptions': {'settings': {'codeAction': {'disableRuleComment': {'enable': True}, 'fixViolation': {'enable': True}}, 'configuration': '~/.config/ruff/ruff.toml', 'configurationPreference': 'filesystemFirst', 'exclude': None, 'fixAll': True, 'format': {'preview': None}, 'lineLength': None, 'lint': {'enable': True, 'extendSelect': None, 'ignore': None, 'preview': None, 'select': None}, 'logLevel': 'error', 'organizeImports': True}}}
:: [18:14:05.126] <<< LSP-ruff (1) (duration: 6ms): {'capabilities': {'codeActionProvider': {'codeActionKinds': ['quickfix', 'source.fixAll.ruff', 'source.organizeImports.ruff', 'notebook.source.fixAll.ruff', 'notebook.source.organizeImports.ruff'], 'resolveProvider': True, 'workDoneProgress': True}, 'diagnosticProvider': {'identifier': 'Ruff', 'interFileDependencies': False, 'workDoneProgress': True, 'workspaceDiagnostics': False}, 'documentFormattingProvider': True, 'documentRangeFormattingProvider': True, 'executeCommandProvider': {'commands': ['ruff.applyFormat', 'ruff.applyAutofix', 'ruff.applyOrganizeImports', 'ruff.printDebugInformation'], 'workDoneProgress': False}, 'hoverProvider': True, 'notebookDocumentSync': {'notebookSelector': [{'cells': [{'language': 'python'}]}], 'save': False}, 'positionEncoding': 'utf-16', 'workspace': {'workspaceFolders': {'changeNotifications': True, 'supported': True}}, 'textDocumentSync': {'change': {'syncKind': 2}, 'didOpen': {}, 'didClose': {}}}, 'serverInfo': {'name': 'ruff', 'version': '0.6.1'}}
:: [18:14:05.127]  -> LSP-ruff initialized: {}
:: [18:14:05.137]  -> LSP-ruff textDocument/didOpen: {'textDocument': {'uri': 'file:///home/andy/Code/rufflsptest/main.py', 'languageId': 'python', 'version': 0, 'text': 'X_PATTERN = r\'https://x\\.com/[^/]+/status/\\d+\'\nsingle = \'single\'\ndouble = "double"\n(single,double,)\n'}}
:: [18:14:05.144] --> LSP-ruff textDocument/diagnostic (2): {'textDocument': {'uri': 'file:///home/andy/Code/rufflsptest/main.py'}, 'identifier': 'Ruff'}
:: [18:14:05.147] <<< LSP-ruff (2) (duration: 3ms): {'items': [], 'kind': 'full'}
:: [18:14:11.892] --> LSP-ruff textDocument/diagnostic (3): {'textDocument': {'uri': 'file:///home/andy/Code/rufflsptest/main.py'}, 'identifier': 'Ruff'}
:: [18:14:11.892] --> LSP-ruff textDocument/codeAction (4): {'textDocument': {'uri': 'file:///home/andy/Code/rufflsptest/main.py'}, 'range': {'start': {'line': 4, 'character': 0}, 'end': {'line': 4, 'character': 0}}, 'context': {'diagnostics': [], 'triggerKind': 2}}
:: [18:14:11.893] <<< LSP-ruff (4) (duration: 1ms): [{'data': 'file:///home/andy/Code/rufflsptest/main.py', 'kind': 'source.fixAll.ruff', 'title': 'Ruff: Fix all auto-fixable problems'}, {'data': 'file:///home/andy/Code/rufflsptest/main.py', 'kind': 'source.organizeImports.ruff', 'title': 'Ruff: Organize imports'}]
:: [18:14:11.893] <<< LSP-ruff (3) (duration: 1ms): {'items': [], 'kind': 'full'}
:: [18:14:22.885] --> LSP-ruff textDocument/formatting (5): {'textDocument': {'uri': 'file:///home/andy/Code/rufflsptest/main.py'}, 'options': {'tabSize': 2, 'insertSpaces': True, 'trimTrailingWhitespace': True, 'insertFinalNewline': True, 'trimFinalNewlines': True}, 'workDoneToken': '$ublime-work-done-progress-5'}
:: [18:14:22.889] <<< LSP-ruff (5) (duration: 4ms): [{'newText': 'X_PATTERN = r"https://x\\.com/[^/]+/status/\\d+"\nsingle = "single"\ndouble = "double"\n(\n    single,\n    double,\n)\n', 'range': {'end': {'character': 0, 'line': 4}, 'start': {'character': 0, 'line': 0}}}]
:: [18:14:23.196]  -> LSP-ruff textDocument/didChange: {'textDocument': {'uri': 'file:///home/andy/Code/rufflsptest/main.py', 'version': 1}, 'contentChanges': [{'range': {'start': {'line': 0, 'character': 0}, 'end': {'line': 4, 'character': 0}}, 'rangeLength': 100, 'text': 'X_PATTERN = r"https://x\\.com/[^/]+/status/\\d+"\nsingle = "single"\ndouble = "double"\n(\n    single,\n    double,\n)\n'}]}
:: [18:14:23.200] --> LSP-ruff textDocument/diagnostic (6): {'textDocument': {'uri': 'file:///home/andy/Code/rufflsptest/main.py'}, 'identifier': 'Ruff'}
:: [18:14:23.201] <<< LSP-ruff (6) (duration: 1ms): {'items': [], 'kind': 'full'}
:: [18:14:23.206] --> LSP-ruff textDocument/diagnostic (7): {'textDocument': {'uri': 'file:///home/andy/Code/rufflsptest/main.py'}, 'identifier': 'Ruff'}
:: [18:14:23.206] --> LSP-ruff textDocument/codeAction (8): {'textDocument': {'uri': 'file:///home/andy/Code/rufflsptest/main.py'}, 'range': {'start': {'line': 7, 'character': 0}, 'end': {'line': 7, 'character': 0}}, 'context': {'diagnostics': [], 'triggerKind': 2}}
:: [18:14:23.209] <<< LSP-ruff (8) (duration: 2ms): [{'data': 'file:///home/andy/Code/rufflsptest/main.py', 'kind': 'source.fixAll.ruff', 'title': 'Ruff: Fix all auto-fixable problems'}, {'data': 'file:///home/andy/Code/rufflsptest/main.py', 'kind': 'source.organizeImports.ruff', 'title': 'Ruff: Organize imports'}]
:: [18:14:23.209] <<< LSP-ruff (7) (duration: 3ms): {'items': [], 'kind': 'full'}

@rchl
Copy link
Member

rchl commented Dec 20, 2024

Running ruff format --diff main.py would not use your custom config because how would it know to use that one? I bet you have another config in ~/ruff.toml that it uses instead.

If I put the ruff.toml config that you've provided in ~/ruff.toml then running format from CLI throws:

ruff failed
  Cause: Failed to parse /Users/rafal/ruff.toml
  Cause: TOML parse error at line 7, column 1
  |
7 | [lint]
  | ^^^^^^
Unknown rule selector: `TRY203`

@rchl rchl added the question Further information is requested label Dec 20, 2024
@AndydeCleyre
Copy link
Author

@rchl

From ruff docs: config file discovery:

If no config file is found in the filesystem hierarchy, Ruff will fall back to using a default configuration. If a user-specific configuration file exists at ${config_dir}/ruff/pyproject.toml, that file will be used instead of the default configuration, with ${config_dir} being determined via etcetera's native strategy, and all relative paths being again resolved relative to the current working directory.

$ ls -l ~/ruff.toml
"/home/andy/ruff.toml": No such file or directory (os error 2)
$ ruff -v format --diff main.py 1>/dev/null
[2024-12-20][10:11:10][ruff::resolve][DEBUG] Using configuration file (via cwd) at: /home/andy/.config/ruff/ruff.toml
[2024-12-20][10:11:10][ruff_workspace::pyproject][DEBUG] `project.requires_python` in `pyproject.toml` will not be used to set `target_version` when using `ruff.toml`.
[2024-12-20][10:11:10][ignore::gitignore][DEBUG] opened gitignore file: /home/andy/.gitignore
[2024-12-20][10:11:10][ruff::commands::format][DEBUG] format_path; path=/home/andy/Code/rufflsptest/main.py
[2024-12-20][10:11:10][tracing::span][DEBUG] Printer::print;
[2024-12-20][10:11:10][ruff::commands::format][DEBUG] Formatted 1 files in 451.04µs
1 file would be reformatted
$ ruff -V
ruff 0.8.4

See also: useless-try-except (TRY203)

@rchl
Copy link
Member

rchl commented Dec 20, 2024

Could be that the behavior difference comes from the fact that ruff used by this package is not at the latest version (see #91).

When testing I have also used some older version of ruff so that's why my results might not be consistent with yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants