Skip to content

Commit

Permalink
fix(editorconfig): fix editorconfig not being loaded when running `bi…
Browse files Browse the repository at this point in the history
…ome ci` (#3870)
  • Loading branch information
dyc3 authored Sep 13, 2024
1 parent cccaea3 commit 2dd0e8b
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

### CLI

#### Bug fixes

- `useEditorConfig` now loads the editorconfig when running `biome ci` [#3864](https://github.com/biomejs/biome/issues/3864). Contributed by @dyc3

### Configuration

### Editors
Expand Down
39 changes: 37 additions & 2 deletions crates/biome_cli/src/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use crate::{execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execu
use biome_configuration::analyzer::assists::PartialAssistsConfiguration;
use biome_configuration::{organize_imports::PartialOrganizeImports, PartialConfiguration};
use biome_configuration::{PartialFormatterConfiguration, PartialLinterConfiguration};
use biome_console::{markup, ConsoleExt};
use biome_deserialize::Merge;
use biome_diagnostics::PrintDiagnostic;
use biome_service::configuration::{
load_configuration, LoadedConfiguration, PartialConfigurationExt,
load_configuration, load_editorconfig, LoadedConfiguration, PartialConfigurationExt,
};
use biome_service::workspace::{RegisterProjectFolderParams, UpdateSettingsParams};
use std::ffi::OsString;
Expand Down Expand Up @@ -49,11 +51,44 @@ pub(crate) fn ci(session: CliSession, payload: CiCommandPayload) -> Result<(), C
cli_options.verbose,
)?;

let editorconfig_search_path = loaded_configuration.directory_path.clone();
let LoadedConfiguration {
configuration: mut fs_configuration,
configuration: biome_configuration,
directory_path: configuration_path,
..
} = loaded_configuration;

let should_use_editorconfig = configuration
.as_ref()
.and_then(|c| c.formatter.as_ref())
.and_then(|f| f.use_editorconfig)
.unwrap_or(
biome_configuration
.formatter
.as_ref()
.and_then(|f| f.use_editorconfig)
.unwrap_or_default(),
);
let mut fs_configuration = if should_use_editorconfig {
let (editorconfig, editorconfig_diagnostics) = {
let search_path = editorconfig_search_path.unwrap_or_else(|| {
let fs = &session.app.fs;
fs.working_directory().unwrap_or_default()
});
load_editorconfig(&session.app.fs, search_path)?
};
for diagnostic in editorconfig_diagnostics {
session.app.console.error(markup! {
{PrintDiagnostic::simple(&diagnostic)}
})
}
editorconfig.unwrap_or_default()
} else {
Default::default()
};
// this makes biome configuration take precedence over editorconfig configuration
fs_configuration.merge_with(biome_configuration);

let formatter = fs_configuration
.formatter
.get_or_insert_with(PartialFormatterConfiguration::default);
Expand Down
92 changes: 92 additions & 0 deletions crates/biome_cli/tests/cases/editorconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,95 @@ indent_style = space
result,
));
}

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

let editorconfig = Path::new(".editorconfig");
fs.insert(
editorconfig.into(),
r#"
[*]
max_line_length = 300
"#,
);

let test_file = Path::new("test.js");
let contents = r#"console.log("really long string that should cause a break if the line width remains at the default 80 characters");
"#;
fs.insert(test_file.into(), contents);

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("ci"),
("--use-editorconfig=true"),
test_file.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

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

assert_file_contents(&fs, test_file, contents);
assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"should_use_editorconfig_ci",
fs,
console,
result,
));
}

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

let editorconfig = Path::new(".editorconfig");
fs.insert(
editorconfig.into(),
r#"
[*]
max_line_length = 300
"#,
);

let biomeconfig = Path::new("biome.json");
fs.insert(
biomeconfig.into(),
r#"{
"formatter": {
"useEditorconfig": true
}
}
"#,
);

let test_file = Path::new("test.js");
let contents = r#"console.log("really long string that should cause a break if the line width remains at the default 80 characters");
"#;
fs.insert(test_file.into(), contents);

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from([("ci"), test_file.as_os_str().to_str().unwrap()].as_slice()),
);

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

assert_file_contents(&fs, test_file, contents);
assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"should_use_editorconfig_ci_enabled_from_biome_conf",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `.editorconfig`

```editorconfig
[*]
max_line_length = 300
```

## `test.js`

```js
console.log("really long string that should cause a break if the line width remains at the default 80 characters");

```

# Emitted Messages

```block
Checked 1 file in <TIME>. No fixes applied.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{
"formatter": {
"useEditorconfig": true
}
}
```

## `.editorconfig`

```editorconfig
[*]
max_line_length = 300
```

## `test.js`

```js
console.log("really long string that should cause a break if the line width remains at the default 80 characters");

```

# Emitted Messages

```block
Checked 1 file in <TIME>. No fixes applied.
```

0 comments on commit 2dd0e8b

Please sign in to comment.