Skip to content

Commit

Permalink
fix(cli): don't emit diagnostics in stdin mode, and exit with error code
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Aug 27, 2024
1 parent 88f1d5e commit 3ad714e
Show file tree
Hide file tree
Showing 27 changed files with 206 additions and 753 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

Contributed by @Conaclos

- The CLI now returns an error code when calling a command in `stdin` mode, and the contents of the files aren't fixed. For example, the following example will result in an error code of `1` because the `lint` command triggers some lint rules:

```shell
echo "let x = 1" | biome lint --stdin-file-path=stdin.js
```

Contributed by @ematipico

#### Bug fixes

- `biome lint --write` now takes `--only` and `--skip` into account ([#3470](https://github.com/biomejs/biome/issues/3470)). Contributed by @Conaclos
Expand All @@ -106,6 +114,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

- Fix JSX expressions for `noAriaHiddenOnFocusable` ([#3708](https://github.com/biomejs/biome/pull/3708)) . Contributed by @anthonyshew

- Fix [#3633](https://github.com/biomejs/biome/issues/3633), where diagnostics where incorrectly printed if the code has errors. Contributed by @ematipico

### Configuration

- Add support for loading configuration from `.editorconfig` files ([#1724](https://github.com/biomejs/biome/issues/1724)).
Expand Down
14 changes: 14 additions & 0 deletions crates/biome_cli/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub enum CliDiagnostic {
MigrateError(MigrationDiagnostic),
/// Emitted during the reporting phase
Report(ReportDiagnostic),
/// Emitted when there's an error emitted when using stdin mode
Stdin(StdinDiagnostic),
}

#[derive(Debug, Diagnostic)]
Expand Down Expand Up @@ -415,6 +417,10 @@ impl CliDiagnostic {
})
}

pub fn stdin() -> Self {
Self::Stdin(StdinDiagnostic::default())
}

/// Emitted when the server is not running
pub fn server_not_running() -> Self {
Self::ServerNotRunning(ServerNotRunning)
Expand Down Expand Up @@ -489,6 +495,14 @@ impl DeprecatedConfigurationFile {
}
}

#[derive(Debug, Default, Diagnostic)]
#[diagnostic(
severity = Error,
category = "stdin",
message = "The contents aren't fixed. Use the `--fix` flag to fix them."
)]
pub struct StdinDiagnostic {}

#[cfg(test)]
mod test {
use crate::CliDiagnostic;
Expand Down
55 changes: 6 additions & 49 deletions crates/biome_cli/src/execute/std_in.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
//! In here, there are the operations that run via standard input
//!
use crate::execute::diagnostics::{ContentDiffAdvice, FormatDiffDiagnostic};
use crate::execute::Execution;
use crate::{CliDiagnostic, CliSession, TraversalMode};
use biome_analyze::RuleCategoriesBuilder;
use biome_console::{markup, ConsoleExt};
use biome_diagnostics::Diagnostic;
use biome_diagnostics::PrintDiagnostic;
use biome_diagnostics::{Diagnostic, DiagnosticExt, Error};
use biome_fs::BiomePath;
use biome_service::file_handlers::{AstroFileHandler, SvelteFileHandler, VueFileHandler};
use biome_service::workspace::{
ChangeFileParams, DropPatternParams, FeaturesBuilder, FixFileParams, FormatFileParams,
OpenFileParams, OrganizeImportsParams, PullDiagnosticsParams, SupportsFeatureParams,
OpenFileParams, OrganizeImportsParams, SupportsFeatureParams,
};
use biome_service::WorkspaceError;
use std::borrow::Cow;
Expand Down Expand Up @@ -71,11 +70,11 @@ pub(crate) fn run<'a>(
{content}
});
console.error(markup! {
<Warn>"The content was not formatted because the formatter is currently disabled."</Warn>
})
<Warn>"The content was not formatted because the formatter is currently disabled."</Warn>
});
return Err(CliDiagnostic::stdin());
}
} else if mode.is_check() || mode.is_lint() {
let mut diagnostics: Vec<Error> = Vec::new();
let mut new_content = Cow::Borrowed(content);

workspace.open_file(OpenFileParams {
Expand Down Expand Up @@ -168,33 +167,6 @@ pub(crate) fn run<'a>(
}
}

if !mode.is_check_apply_unsafe() {
let result = workspace.pull_diagnostics(PullDiagnosticsParams {
categories: RuleCategoriesBuilder::default()
.with_lint()
.with_syntax()
.build(),
path: biome_path.clone(),
max_diagnostics: mode.max_diagnostics.into(),
only,
skip,
})?;
let content = match biome_path.extension_as_str() {
Some("astro") => AstroFileHandler::input(&new_content),
Some("vue") => VueFileHandler::input(&new_content),
Some("svelte") => SvelteFileHandler::input(&new_content),
_ => &new_content,
};
diagnostics.extend(
result
.diagnostics
.into_iter()
.map(Error::from)
.map(|diag| diag.with_file_source_code(content.to_string()))
.collect::<Vec<_>>(),
);
}

if file_features.supports_format() && mode.is_check() {
let printed = workspace.format_file(FormatFileParams {
path: biome_path.clone(),
Expand All @@ -210,15 +182,6 @@ pub(crate) fn run<'a>(
if output != new_content {
new_content = Cow::Owned(output);
}
} else {
let diagnostic = FormatDiffDiagnostic {
file_name: biome_path.display().to_string(),
diff: ContentDiffAdvice {
new: output,
old: content.to_string(),
},
};
diagnostics.push(Error::from(diagnostic));
}
}

Expand All @@ -227,20 +190,14 @@ pub(crate) fn run<'a>(
console.append(markup! {
{original_content}
});
return Err(CliDiagnostic::stdin());
}
Cow::Owned(ref new_content) => {
console.append(markup! {
{new_content}
});
}
}
if !diagnostics.is_empty() {
for diag in diagnostics {
console.error(markup! {
{if verbose { PrintDiagnostic::verbose(&diag) } else { PrintDiagnostic::simple(&diag) }}
})
}
}
} else if let TraversalMode::Search { pattern, .. } = mode.traversal_mode() {
// Make sure patterns are always cleaned up at the end of execution.
let _ = session.app.workspace.drop_pattern(DropPatternParams {
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_cli/tests/cases/handle_astro_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ fn lint_stdin_successfully() {
Args::from(["lint", "--stdin-file-path", "file.astro"].as_slice()),
);

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

let message = console
.out_buffer
Expand Down Expand Up @@ -590,7 +590,7 @@ fn check_stdin_successfully() {
Args::from(["check", "--stdin-file-path", "file.astro"].as_slice()),
);

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

let message = console
.out_buffer
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_cli/tests/cases/handle_svelte_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ fn check_stdin_successfully() {
Args::from(["check", "--stdin-file-path", "file.svelte"].as_slice()),
);

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

let message = console
.out_buffer
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_cli/tests/cases/handle_vue_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ fn lint_stdin_successfully() {
Args::from(["lint", "--stdin-file-path", "file.svelte"].as_slice()),
);

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

let message = console
.out_buffer
Expand Down Expand Up @@ -786,7 +786,7 @@ fn check_stdin_successfully() {
Args::from(["check", "--stdin-file-path", "file.vue"].as_slice()),
);

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

let message = console
.out_buffer
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_cli/tests/cases/protected_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn not_process_file_from_stdin_lint() {
Args::from([("lint"), ("--stdin-file-path=package.json")].as_slice()),
);

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

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
Expand Down
46 changes: 45 additions & 1 deletion crates/biome_cli/tests/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2325,7 +2325,7 @@ fn check_stdin_returns_text_if_content_is_not_changed() {
),
);

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

let message = console
.out_buffer
Expand All @@ -2346,6 +2346,50 @@ fn check_stdin_returns_text_if_content_is_not_changed() {
result,
));
}

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

console.in_buffer.push("let b = 2;".to_string());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("check"),
"--organize-imports-enabled=true",
("--stdin-file-path"),
("mock.js"),
]
.as_slice(),
),
);

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

let message = console
.out_buffer
.first()
.expect("Console should have written a message");

let content = markup_to_string(markup! {
{message.content}
});

assert_eq!(content, "let b = 2;");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"check_stdin_returns_content_when_not_write",
fs,
console,
result,
));
}

#[test]
fn should_apply_correct_file_source() {
let mut fs = MemoryFileSystem::default();
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ fn does_not_format_if_disabled() {
Args::from([("format"), ("--stdin-file-path"), ("mock.js")].as_slice()),
);

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

let message = console
.out_buffer
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_cli/tests/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,7 @@ const = ""; "#
Args::from([("lint"), "--write", ("--stdin-file-path"), ("mock.ts")].as_slice()),
);

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

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
Expand Down
Loading

0 comments on commit 3ad714e

Please sign in to comment.