Skip to content

Commit 7c6b464

Browse files
authored
Allow arbitrary config keys in ruff.configuration (#702)
## Summary Closes: #6 This PR adds support for astral-sh/ruff#16296 for the VS Code extension by allowing any arbitrary object in `ruff.configuration` Additionally, it will provide a warning message to the user if they're using inline configuration but the Ruff version does not support it. It has one disadvantage that the user will see two popups where the error one is coming from the server because it cannot deserialize the options. We could not show the warning popup if this is too much. I'm worried that it might go unnoticed because the "Show Logs" in the below popup will open the server logs while the message would be in the client logs. <img width="491" alt="Screenshot 2025-02-25 at 1 24 15 PM" src="https://github.com/user-attachments/assets/8bafbd69-f8fa-4604-8ab3-1f6efa745045" /> We'll still log it on the console (client log channel): ``` 2025-02-25 13:24:10.067 [warning] Inline configuration support was added in Ruff 0.9.8 (current version is 0.9.7). Please update your Ruff version to use this feature. ``` I haven't provided more details in the warning message based on the assumption that if the user is using inline configuration then they're aware of it but it's just that the Ruff version is too old. ## Test Plan Refer to the test plan in astral-sh/ruff#16296
1 parent 72c04ba commit 7c6b464

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

package.json

+15-2
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,22 @@
102102
},
103103
"ruff.configuration": {
104104
"default": null,
105-
"markdownDescription": "Path to a `ruff.toml` or `pyproject.toml` file to use for configuration. By default, Ruff will discover configuration for each project from the filesystem, mirroring the behavior of the Ruff CLI.\n\n**This setting is used only by the native server.**",
105+
"markdownDescription": "Configuration overrides for Ruff. See [the documentation](https://docs.astral.sh/ruff/editors/settings/#configuration) for more details.\n\n**This setting is used only by the native server.**",
106106
"scope": "window",
107-
"type": "string"
107+
"type": [
108+
"string",
109+
"object"
110+
],
111+
"oneOf": [
112+
{
113+
"type": "string",
114+
"markdownDescription": "Path to a `ruff.toml` or `pyproject.toml` file to use for configuration."
115+
},
116+
{
117+
"type": "object",
118+
"markdownDescription": "Inline JSON configuration for Ruff settings (e.g., `{ \"line-length\": 100 }`). *Added in Ruff 0.9.8.*"
119+
}
120+
]
108121
},
109122
"ruff.args": {
110123
"default": [],

src/common/server.ts

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
import { logger } from "./logger";
2222
import { getDebuggerPath } from "./python";
2323
import {
24+
checkInlineConfigSupport,
2425
getExtensionSettings,
2526
getGlobalSettings,
2627
getUserSetLegacyServerSettings,
@@ -191,6 +192,8 @@ async function createNativeServer(
191192
vscode.window.showErrorMessage(message);
192193
return Promise.reject();
193194
}
195+
196+
checkInlineConfigSupport(ruffVersion, serverId);
194197
}
195198

196199
let ruffServerArgs: string[];

src/common/settings.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import {
88
import { getInterpreterDetails } from "./python";
99
import { getConfiguration, getWorkspaceFolders } from "./vscodeapi";
1010
import { logger } from "./logger";
11+
import {
12+
INLINE_CONFIGURATION_VERSION,
13+
VersionInfo,
14+
supportsInlineConfiguration,
15+
versionToString,
16+
} from "./version";
1117

1218
type ImportStrategy = "fromEnvironment" | "useBundled";
1319

@@ -50,7 +56,7 @@ export interface ISettings {
5056
path: string[];
5157
ignoreStandardLibrary: boolean;
5258
interpreter: string[];
53-
configuration: string | null;
59+
configuration: string | object | null;
5460
importStrategy: ImportStrategy;
5561
codeAction: CodeAction;
5662
enable: boolean;
@@ -135,8 +141,8 @@ export async function getWorkspaceSettings(
135141
interpreter = resolveVariables(interpreter, workspace);
136142
}
137143

138-
let configuration = config.get<string>("configuration") ?? null;
139-
if (configuration !== null) {
144+
let configuration = config.get<string | object>("configuration") ?? null;
145+
if (configuration !== null && typeof configuration === "string") {
140146
configuration = resolveVariables(configuration, workspace);
141147
}
142148

@@ -199,7 +205,7 @@ export async function getGlobalSettings(namespace: string): Promise<ISettings> {
199205
path: getGlobalValue<string[]>(config, "path", []),
200206
ignoreStandardLibrary: getGlobalValue<boolean>(config, "ignoreStandardLibrary", true),
201207
interpreter: [],
202-
configuration: getGlobalValue<string | null>(config, "configuration", null),
208+
configuration: getGlobalValue<string | object | null>(config, "configuration", null),
203209
importStrategy: getGlobalValue<ImportStrategy>(config, "importStrategy", "fromEnvironment"),
204210
codeAction: getGlobalValue<CodeAction>(config, "codeAction", {}),
205211
lint: {
@@ -333,6 +339,25 @@ function getPreferredGlobalSetting<T>(
333339
return newSettings?.defaultValue;
334340
}
335341

342+
export function checkInlineConfigSupport(ruffVersion: VersionInfo, serverId: string) {
343+
if (supportsInlineConfiguration(ruffVersion)) {
344+
return;
345+
}
346+
347+
getWorkspaceFolders().forEach((workspace) => {
348+
const config = getConfiguration(serverId, workspace.uri).get<string | object>("configuration");
349+
if (typeof config === "object") {
350+
const message = `Inline configuration support was added in Ruff ${versionToString(
351+
INLINE_CONFIGURATION_VERSION,
352+
)} (current version is ${versionToString(
353+
ruffVersion,
354+
)}). Please update your Ruff version to use this feature.`;
355+
logger.warn(message);
356+
vscode.window.showWarningMessage(message);
357+
}
358+
});
359+
}
360+
336361
/**
337362
* Check if the user have configured `notebook.codeActionsOnSave` with non-notebook prefixed code actions.
338363
*/

src/common/version.ts

+12
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,15 @@ export const NATIVE_SERVER_STABLE_VERSION: VersionInfo = { major: 0, minor: 5, p
5858
export function supportsStableNativeServer(version: VersionInfo): boolean {
5959
return versionGte(version, NATIVE_SERVER_STABLE_VERSION);
6060
}
61+
62+
/**
63+
* The minimum version of the Ruff executable that supports inline configuration.
64+
*/
65+
export const INLINE_CONFIGURATION_VERSION: VersionInfo = { major: 0, minor: 9, patch: 8 };
66+
67+
/**
68+
* Check if the given version of the Ruff executable supports inline configuration.
69+
*/
70+
export function supportsInlineConfiguration(version: VersionInfo): boolean {
71+
return versionGte(version, INLINE_CONFIGURATION_VERSION);
72+
}

0 commit comments

Comments
 (0)