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

fix(core): raise an error when invalid config extension is passed #4944

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cli: patch
---

# Fix a bug where `--config-path` accepted configuration files with unsupported extensions. Now only `.json` and `.jsonc` are accepted, and an error is raised otherwise.
25 changes: 25 additions & 0 deletions crates/biome_cli/tests/cases/config_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,28 @@ fn set_config_path_to_file() {
result,
));
}

#[test]
fn raises_an_error_when_the_config_file_is_not_json() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let config_path = Utf8Path::new("biome.yml");
fs.insert(config_path.into(), r#"blah: foo"#.as_bytes());

let (fs, result) = run_cli(
fs,
&mut console,
Args::from(["check", "--config-path=biome.yml", "src"].as_slice()),
);

assert!(result.is_err(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"raises_an_error_when_the_config_file_is_not_json",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: redactor(content)
snapshot_kind: text
---
## `biome.yml`

```yml
blah: foo
```

# Termination Message

```block
biome.yml configuration ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Invalid configuration file. Expected JSON or JSONC file, but got biome.yml.



```
24 changes: 24 additions & 0 deletions crates/biome_configuration/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use biome_deserialize::DeserializationDiagnostic;
use biome_diagnostics::ResolveError;
use biome_diagnostics::{Advices, Diagnostic, Error, LogCategory, MessageAndDescription, Visit};
use biome_rowan::SyntaxError;
use camino::Utf8Path;
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};

Expand Down Expand Up @@ -46,6 +47,9 @@ pub enum BiomeDiagnostic {
/// When something is wrong with the configuration
InvalidConfiguration(InvalidConfiguration),

/// When a user provide a configuration file path that isn't a JSON/JSONC file
InvalidConfigurationFile(InvalidConfigurationFile),

/// Thrown when the pattern inside the `ignore` field errors
InvalidIgnorePattern(InvalidIgnorePattern),

Expand Down Expand Up @@ -111,6 +115,12 @@ impl BiomeDiagnostic {
})
}

pub fn invalid_configuration_file(path: &Utf8Path) -> Self {
Self::InvalidConfigurationFile(InvalidConfigurationFile {
path: path.to_string(),
})
}

pub fn cant_resolve(path: impl Display, source: oxc_resolver::ResolveError) -> Self {
Self::CantResolve(CantResolve {
message: MessageAndDescription::from(
Expand Down Expand Up @@ -225,6 +235,20 @@ pub struct InvalidConfiguration {
message: MessageAndDescription,
}

#[derive(Debug, Serialize, Deserialize, Diagnostic)]
#[diagnostic(
category = "configuration",
severity = Error,
message(
description = "Invalid configuration file. Expected JSON or JSONC file, but got {path}.",
message("Invalid configuration file. Expected JSON or JSONC file, but got "{self.path}".")
)
)]
pub struct InvalidConfigurationFile {
#[location(resource)]
path: String,
}

#[derive(Debug, Serialize, Deserialize, Diagnostic)]
#[diagnostic(
category = "configuration",
Expand Down
26 changes: 25 additions & 1 deletion crates/biome_service/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,14 @@ fn load_config(fs: &dyn FileSystem, base_path: ConfigurationPathHint) -> LoadCon
let content = fs.read_file_from_path(config_file_path)?;
let parser_options = match config_file_path.extension() {
Some("json") => JsonParserOptions::default(),
_ => JsonParserOptions::default()
Some("jsonc") => JsonParserOptions::default()
.with_allow_comments()
.with_allow_trailing_commas(),
_ => {
return Err(
BiomeDiagnostic::invalid_configuration_file(config_file_path).into(),
)
}
};
let deserialized =
deserialize_from_json_str::<Configuration>(&content, parser_options, "");
Expand Down Expand Up @@ -571,3 +576,22 @@ impl ConfigurationExt for Configuration {
Ok((None, vec![]))
}
}

#[cfg(test)]
mod test {
use crate::configuration::load_configuration;
use biome_configuration::ConfigurationPathHint;
use biome_fs::MemoryFileSystem;
use camino::Utf8PathBuf;

#[test]
fn should_not_load_a_configuration_yml() {
let mut fs = MemoryFileSystem::default();
fs.insert(Utf8PathBuf::from("biome.yml"), "content".to_string());
let path_hint = ConfigurationPathHint::FromUser(Utf8PathBuf::from("biome.yml"));

let result = load_configuration(&fs, path_hint);

assert!(result.is_err());
}
}
Loading