From 37ad994318512716eca727b4fc5b8dbb514f5437 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 27 May 2024 21:06:34 +0530 Subject: [PATCH] Use default settings if initialization options is empty or not provided (#11566) ## Summary This PR fixes the bug to avoid flattening the global-only settings for the new server. This was added in https://github.com/astral-sh/ruff/pull/11497, possibly to correctly de-serialize an empty value (`{}`). But, this lead to a bug where the configuration under the `settings` key was not being read for global-only variant. By using #[serde(default)], we ensure that the settings field in the `GlobalOnly` variant is optional and that an empty JSON object `{}` is correctly deserialized into `GlobalOnly` with a default `ClientSettings` instance. fixes: #11507 ## Test Plan Update the snapshot and existing test case. Also, verify the following settings in Neovim: 1. Nothing ```lua ruff = { cmd = { '/Users/dhruv/work/astral/ruff/target/debug/ruff', 'server', '--preview', }, } ``` 2. Empty dictionary ```lua ruff = { cmd = { '/Users/dhruv/work/astral/ruff/target/debug/ruff', 'server', '--preview', }, init_options = vim.empty_dict(), } ``` 3. Empty `settings` ```lua ruff = { cmd = { '/Users/dhruv/work/astral/ruff/target/debug/ruff', 'server', '--preview', }, init_options = { settings = vim.empty_dict(), }, } ``` 4. With some configuration: ```lua ruff = { cmd = { '/Users/dhruv/work/astral/ruff/target/debug/ruff', 'server', '--preview', }, init_options = { settings = { configuration = '/tmp/ruff-repro/pyproject.toml', }, }, } ``` --- .../test/fixtures/settings/global_only.json | 28 ++++++++++--------- crates/ruff_server/src/session/settings.rs | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/crates/ruff_server/resources/test/fixtures/settings/global_only.json b/crates/ruff_server/resources/test/fixtures/settings/global_only.json index 29c9956c77..0ed3bf16d5 100644 --- a/crates/ruff_server/resources/test/fixtures/settings/global_only.json +++ b/crates/ruff_server/resources/test/fixtures/settings/global_only.json @@ -1,15 +1,17 @@ { - "codeAction": { - "disableRuleComment": { - "enable": false - } - }, - "lint": { - "ignore": ["RUF001"], - "run": "onSave" - }, - "fixAll": false, - "logLevel": "warn", - "lineLength": 80, - "exclude": ["third_party"] + "settings": { + "codeAction": { + "disableRuleComment": { + "enable": false + } + }, + "lint": { + "ignore": ["RUF001"], + "run": "onSave" + }, + "fixAll": false, + "logLevel": "warn", + "lineLength": 80, + "exclude": ["third_party"] + } } diff --git a/crates/ruff_server/src/session/settings.rs b/crates/ruff_server/src/session/settings.rs index bfbfeef5c4..c929d0a9ce 100644 --- a/crates/ruff_server/src/session/settings.rs +++ b/crates/ruff_server/src/session/settings.rs @@ -130,7 +130,7 @@ enum InitializationOptions { workspace_settings: Vec, }, GlobalOnly { - #[serde(flatten)] + #[serde(default)] settings: ClientSettings, }, }