-
-
Notifications
You must be signed in to change notification settings - Fork 982
feat(lint): fix for schema version mismatch #9896
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
Closed
hamirmahal
wants to merge
2
commits into
biomejs:next
from
hamirmahal:feat/fix-schema-version-automatically
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@biomejs/biome": minor | ||
| --- | ||
|
|
||
| Added a `useBiomeSchemaVersion` lint rule that detects mismatched `$schema` URLs in `biome.json` and `biome.jsonc` and provides a safe quick fix to update the version segment to match the running CLI version. | ||
|
|
||
| Consolidated schema-version reporting by removing the duplicate deserialization diagnostic that previously emitted the same message during configuration deserialization. The lint is now the single source of truth for this diagnostic and provides the recommended quick-fix. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
crates/biome_configuration/src/generated/linter_options_check.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
194 changes: 194 additions & 0 deletions
194
crates/biome_json_analyze/src/lint/suspicious/use_biome_schema_version.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| use crate::JsonRuleAction; | ||
| use biome_analyze::{Ast, FixKind, Rule, RuleDiagnostic, context::RuleContext, declare_lint_rule}; | ||
| use biome_console::markup; | ||
| use biome_diagnostics::Severity; | ||
| use biome_json_factory::make::{json_string_literal, json_string_value}; | ||
| use biome_json_syntax::{JsonMember, JsonStringValue}; | ||
| use biome_rowan::{AstNode, BatchMutationExt}; | ||
| use std::cmp::Ordering; | ||
| const BIOME_SCHEMA_SUFFIX: &str = "/schema.json"; | ||
| const BIOME_SCHEMA_PREFIX: &str = "https://biomejs.dev/schemas/"; | ||
| const BIOME_VERSION: &str = match option_env!("BIOME_VERSION") { | ||
| Some(version) => version, | ||
| None => "0.0.0", | ||
| }; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct SchemaVersionState { | ||
| schema_value: JsonStringValue, | ||
| found_version: String, | ||
| expected_schema_url: String, | ||
| } | ||
|
|
||
| declare_lint_rule! { | ||
| /// Ensures that Biome configuration files use the current schema version. | ||
| /// | ||
| /// When a configuration contains a `$schema` URL that points to a different | ||
| /// Biome version, editor tooling can become inconsistent with the currently | ||
| /// running Biome CLI version. | ||
| /// | ||
| /// This rule applies only to top-level `$schema` fields in `biome.json` and | ||
| /// `biome.jsonc`, and only when the schema URL matches the official Biome | ||
| /// schema format: `https://biomejs.dev/schemas/{version}/schema.json`. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```json,ignore | ||
| /// { | ||
| /// "$schema": "https://biomejs.dev/schemas/1.9.0/schema.json" | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```json,ignore | ||
| /// { | ||
| /// "$schema": "https://biomejs.dev/schemas/2.4.10/schema.json" | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| pub UseBiomeSchemaVersion { | ||
| version: "2.4.10", | ||
| name: "useBiomeSchemaVersion", | ||
| language: "json", | ||
| recommended: true, | ||
| fix_kind: FixKind::Safe, | ||
| severity: Severity::Information, | ||
| } | ||
| } | ||
|
|
||
| impl Rule for UseBiomeSchemaVersion { | ||
| type Query = Ast<JsonMember>; | ||
| type State = SchemaVersionState; | ||
| type Signals = Option<Self::State>; | ||
| type Options = (); | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let node = ctx.query(); | ||
| if !is_biome_configuration_file(ctx.file_path()) || !is_top_level_schema_member(node) { | ||
| return None; | ||
| } | ||
|
|
||
| let schema_value = node.value().ok()?.as_json_string_value()?.clone(); | ||
| let schema_text = schema_value.inner_string_text().ok()?; | ||
| let schema_url = schema_text.text(); | ||
| let mismatch = parse_biome_schema_version(schema_url)?; | ||
| let expected_version = Version::new(BIOME_VERSION); | ||
|
|
||
| if mismatch.cmp(&expected_version) == Ordering::Equal { | ||
| return None; | ||
| } | ||
|
|
||
| Some(SchemaVersionState { | ||
| schema_value, | ||
| found_version: mismatch.0.to_string(), | ||
| expected_schema_url: expected_schema_url(), | ||
| }) | ||
| } | ||
|
|
||
| fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| state.schema_value.range(), | ||
| markup! { | ||
| "The configuration schema version does not match the CLI version "{BIOME_VERSION} | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "Expected schema URL: "<Emphasis>{state.expected_schema_url.as_str()}</Emphasis> | ||
| }) | ||
| .note(markup! { | ||
| "Found version: "<Emphasis>{state.found_version.as_str()}</Emphasis> | ||
| }), | ||
| ) | ||
| } | ||
|
|
||
| fn action(ctx: &RuleContext<Self>, state: &Self::State) -> Option<JsonRuleAction> { | ||
| let mut mutation = ctx.root().begin(); | ||
| let new_value = json_string_value(json_string_literal(state.expected_schema_url.as_str())); | ||
| mutation.replace_node(state.schema_value.clone(), new_value); | ||
|
|
||
| Some(JsonRuleAction::new( | ||
| ctx.metadata().action_category(ctx.category(), ctx.group()), | ||
| ctx.metadata().applicability(), | ||
| markup! { | ||
| "Use the schema URL that matches the running Biome version." | ||
| }, | ||
| mutation, | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| fn is_biome_configuration_file(path: &camino::Utf8Path) -> bool { | ||
| path.file_name().is_some_and(|file_name| { | ||
| file_name.ends_with("biome.json") || file_name.ends_with("biome.jsonc") | ||
| }) | ||
| } | ||
|
|
||
| fn is_top_level_schema_member(node: &JsonMember) -> bool { | ||
| let Ok(name) = node.name() else { | ||
| return false; | ||
| }; | ||
| if name | ||
| .inner_string_text() | ||
| .is_none_or(|text| text.text() != "$schema") | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| node.syntax() | ||
| .ancestors() | ||
| .skip(1) | ||
| .find_map(JsonMember::cast) | ||
| .is_none() | ||
| } | ||
|
|
||
| fn parse_biome_schema_version(schema_url: &str) -> Option<Version<'_>> { | ||
| let version = schema_url | ||
| .strip_prefix(BIOME_SCHEMA_PREFIX)? | ||
| .strip_suffix(BIOME_SCHEMA_SUFFIX)?; | ||
|
|
||
| if version.is_empty() || version.split('.').any(|part| part.parse::<u32>().is_err()) { | ||
| return None; | ||
| } | ||
|
|
||
| Some(Version::new(version)) | ||
| } | ||
|
|
||
| fn expected_schema_url() -> String { | ||
| format!("{BIOME_SCHEMA_PREFIX}{BIOME_VERSION}{BIOME_SCHEMA_SUFFIX}") | ||
| } | ||
|
|
||
| #[derive(Clone, Copy)] | ||
| struct Version<'a>(&'a str); | ||
|
|
||
| impl<'a> Version<'a> { | ||
| fn new(version: &'a str) -> Self { | ||
| Self(version) | ||
| } | ||
|
|
||
| fn cmp(&self, other: &Self) -> Ordering { | ||
| let left_parts: Vec<_> = self | ||
| .0 | ||
| .split('.') | ||
| .filter_map(|part| part.parse::<u32>().ok()) | ||
| .collect(); | ||
| let right_parts: Vec<_> = other | ||
| .0 | ||
| .split('.') | ||
| .filter_map(|part| part.parse::<u32>().ok()) | ||
| .collect(); | ||
|
|
||
| for (left, right) in left_parts.iter().zip(right_parts.iter()) { | ||
| match left.cmp(right) { | ||
| Ordering::Equal => {} | ||
| non_equal => return non_equal, | ||
| } | ||
| } | ||
|
|
||
| left_parts.len().cmp(&right_parts.len()) | ||
| } | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
crates/biome_json_analyze/tests/specs/suspicious/useBiomeSchemaVersion/invalid.biome.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "$schema": "https://biomejs.dev/schemas/1.0.0/schema.json" | ||
| } |
39 changes: 39 additions & 0 deletions
39
...s/biome_json_analyze/tests/specs/suspicious/useBiomeSchemaVersion/invalid.biome.json.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| --- | ||
| source: crates/biome_json_analyze/tests/spec_tests.rs | ||
| assertion_line: 99 | ||
| expression: invalid.biome.json | ||
| --- | ||
| # Input | ||
| ```json | ||
| { | ||
| "$schema": "https://biomejs.dev/schemas/1.0.0/schema.json" | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid.biome.json:2:14 lint/suspicious/useBiomeSchemaVersion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i The configuration schema version does not match the CLI version 0.0.0 | ||
|
|
||
| 1 │ { | ||
| > 2 │ "$schema": "https://biomejs.dev/schemas/1.0.0/schema.json" | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 3 │ } | ||
| 4 │ | ||
|
|
||
| i Expected schema URL: https://biomejs.dev/schemas/0.0.0/schema.json | ||
|
|
||
| i Found version: 1.0.0 | ||
|
|
||
| i Safe fix: Use the schema URL that matches the running Biome version. | ||
|
|
||
| 1 1 │ { | ||
| 2 │ - ··"$schema":·"https://biomejs.dev/schemas/1.0.0/schema.json" | ||
| 2 │ + ··"$schema":·"https://biomejs.dev/schemas/0.0.0/schema.json" | ||
| 3 3 │ } | ||
| 4 4 │ | ||
|
|
||
|
|
||
| ``` |
4 changes: 4 additions & 0 deletions
4
crates/biome_json_analyze/tests/specs/suspicious/useBiomeSchemaVersion/invalid.biome.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| // should generate diagnostics | ||
| { | ||
| "$schema": "https://biomejs.dev/schemas/2.4.10/schema.json" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.