Skip to content

Commit 9836d64

Browse files
authored
Gate TOML support on the Ruff version (#1163)
## Summary This PR gates TOML registration behind a Ruff version check to avoid the issue reported in #1127 (comment), where older versions of Ruff try to lint TOML files as Python code. The version check uses 0.16.2 because that includes the formatting fix in astral-sh/ruff#27332, in addition to the basic TOML support added to the LSP in astral-sh/ruff#26862 (shipped in 0.16.1) and to the linter in astral-sh/ruff#26772 (shipped in 0.15.22). TOML files are also excluded when `ruff-lsp` is used. ## Test plan Manual testing in VS Code: <img width="649" height="496" alt="image" src="https://github.com/user-attachments/assets/735ae6c7-50a1-4d77-86b5-5d861c7a9bf5" /> No problem is detected now, but if I use the same executable with the published version of the extension, I reproduce the diagnostic from the comment.
1 parent 66e7286 commit 9836d64

5 files changed

Lines changed: 71 additions & 17 deletions

File tree

src/common/server.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
supportsStableNativeServer,
4343
NATIVE_SERVER_STABLE_VERSION,
4444
} from "./version";
45-
import { updateServerKind, updateStatus } from "./status";
45+
import { updateDocumentSelector, updateServerKind, updateStatus } from "./status";
4646
import { getDocumentSelector } from "./utilities";
4747
import { execFile } from "child_process";
4848
// eslint-disable-next-line @typescript-eslint/no-require-imports
@@ -294,8 +294,8 @@ async function createNativeServer(
294294
};
295295

296296
const clientOptions = {
297-
// Register the server for python documents
298-
documentSelector: getDocumentSelector(),
297+
// Register the server for supported documents.
298+
documentSelector: getDocumentSelector(ruffVersion),
299299
outputChannel,
300300
traceOutputChannel,
301301
revealOutputChannelOn: RevealOutputChannelOn.Never,
@@ -723,6 +723,7 @@ export async function startServer(
723723
},
724724
resolution,
725725
);
726+
updateDocumentSelector(newLSClient.clientOptions.documentSelector ?? []);
726727
logger.info(`Server: Start requested.`);
727728

728729
_disposables.push(

src/common/status.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { LanguageStatusItem, Disposable, l10n, LanguageStatusSeverity } from "vscode";
4+
import {
5+
LanguageStatusItem,
6+
Disposable,
7+
l10n,
8+
LanguageStatusSeverity,
9+
DocumentSelector,
10+
} from "vscode";
511
import { createLanguageStatusItem } from "./vscodeapi";
612
import { Command } from "vscode-languageclient";
713
import { getDocumentSelector } from "./utilities";
@@ -27,6 +33,12 @@ export function updateServerKind(native: boolean): void {
2733
_serverKind = native ? "native" : "ruff-lsp";
2834
}
2935

36+
export function updateDocumentSelector(selector: DocumentSelector): void {
37+
if (_status) {
38+
_status.selector = selector;
39+
}
40+
}
41+
3042
export function updateStatus(
3143
status: string | undefined,
3244
severity: LanguageStatusSeverity,

src/common/utilities.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from "path";
33
import { Uri, WorkspaceFolder } from "vscode";
44
import { DocumentSelector } from "vscode-languageclient";
55
import { getWorkspaceFolders, isVirtualWorkspace } from "./vscodeapi";
6+
import { supportsToml, VersionInfo } from "./version";
67

78
export async function getProjectRoot(): Promise<WorkspaceFolder> {
89
const workspaces: readonly WorkspaceFolder[] = getWorkspaceFolders();
@@ -35,16 +36,23 @@ export async function getProjectRoot(): Promise<WorkspaceFolder> {
3536
}
3637
}
3738

38-
export function getDocumentSelector(): DocumentSelector {
39-
return isVirtualWorkspace()
40-
? [{ language: "python" }, { language: "markdown" }]
41-
: [
42-
{ scheme: "file", language: "python" },
43-
{ scheme: "untitled", language: "python" },
44-
{ scheme: "vscode-notebook", language: "python" },
45-
{ scheme: "vscode-notebook-cell", language: "python" },
46-
{ scheme: "file", language: "markdown" },
47-
{ scheme: "untitled", language: "markdown" },
48-
{ scheme: "file", pattern: "**/{pyproject.toml,ruff.toml,.ruff.toml}" },
49-
];
39+
export function getDocumentSelector(ruffVersion?: VersionInfo): DocumentSelector {
40+
if (isVirtualWorkspace()) {
41+
return [{ language: "python" }, { language: "markdown" }];
42+
}
43+
44+
const selector: DocumentSelector = [
45+
{ scheme: "file", language: "python" },
46+
{ scheme: "untitled", language: "python" },
47+
{ scheme: "vscode-notebook", language: "python" },
48+
{ scheme: "vscode-notebook-cell", language: "python" },
49+
{ scheme: "file", language: "markdown" },
50+
{ scheme: "untitled", language: "markdown" },
51+
];
52+
53+
if (ruffVersion != null && supportsToml(ruffVersion)) {
54+
selector.push({ scheme: "file", pattern: "**/{pyproject.toml,ruff.toml,.ruff.toml}" });
55+
}
56+
57+
return selector;
5058
}

src/common/version.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,15 @@ export const INLINE_CONFIGURATION_VERSION: VersionInfo = { major: 0, minor: 9, p
7070
export function supportsInlineConfiguration(version: VersionInfo): boolean {
7171
return versionGte(version, INLINE_CONFIGURATION_VERSION);
7272
}
73+
74+
/**
75+
* The minimum Ruff version required for TOML document support.
76+
*/
77+
export const TOML_SUPPORT_VERSION: VersionInfo = { major: 0, minor: 16, patch: 2 };
78+
79+
/**
80+
* Check if the given version of the Ruff executable supports TOML documents.
81+
*/
82+
export function supportsToml(version: VersionInfo): boolean {
83+
return versionGte(version, TOML_SUPPORT_VERSION);
84+
}

src/test/utils.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,30 @@ import {
99
resolvePythonEnvironment,
1010
} from "../common/server";
1111
import type { ISettings } from "../common/settings";
12-
import { isWindows } from "./helper";
12+
import { getDocumentSelector } from "../common/utilities";
13+
import { getDocumentUri, isWindows } from "./helper";
1314

1415
suite("Utils tests", () => {
16+
test("TOML documents are excluded before Ruff 0.16.2", async () => {
17+
const document = await vscode.workspace.openTextDocument(getDocumentUri("pyproject.toml"));
18+
const selector = getDocumentSelector({ major: 0, minor: 16, patch: 1 });
19+
20+
assert.strictEqual(vscode.languages.match(selector, document), 0);
21+
});
22+
23+
test("TOML documents are included starting with Ruff 0.16.2", async () => {
24+
const document = await vscode.workspace.openTextDocument(getDocumentUri("pyproject.toml"));
25+
const selector = getDocumentSelector({ major: 0, minor: 16, patch: 2 });
26+
27+
assert.ok(vscode.languages.match(selector, document) > 0);
28+
});
29+
30+
test("TOML documents are excluded without a native Ruff version", async () => {
31+
const document = await vscode.workspace.openTextDocument(getDocumentUri("pyproject.toml"));
32+
33+
assert.strictEqual(vscode.languages.match(getDocumentSelector(), document), 0);
34+
});
35+
1536
test("Check execFile shell mode", () => {
1637
assert.strictEqual(
1738
execFileShellModeRequired("/use/random/python"),

0 commit comments

Comments
 (0)