Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/warm-houses-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Fixed [#6783](https://github.com/biomejs/biome/issues/6783): now, when a path is provided via `--stdin-file-path`, Biome checks whether the file exists on disk. If the path doesn't exist (virtual path), ignore checks (`files.includes` and VCS ignore rules) are skipped.
15 changes: 15 additions & 0 deletions crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ pub enum BiomeCommand {
/// Also, if you have overrides configured and/or nested configurations,
/// the path may determine the settings being applied.
///
/// The provided path may also affect whether the input is treated as
/// ignored. If the path doesn't exist on disk (virtual path), Biome
/// won't require it to be part of the project file set, and ignore
/// checks (`files.includes` and VCS ignore rules) are skipped.
///
/// Example:
/// ```shell
/// echo 'let a;' | biome check --stdin-file-path=file.js --write
Expand Down Expand Up @@ -287,6 +292,11 @@ pub enum BiomeCommand {
///
/// The file doesn't need to exist on disk, what matters is the extension of the file. Based on the extension, Biome knows how to lint the code.
///
/// The provided path may also affect whether the input is treated as
/// ignored. If the path doesn't exist on disk (virtual path), Biome
/// won't require it to be part of the project file set, and ignore
/// checks (`files.includes` and VCS ignore rules) are skipped.
///
/// Example:
/// ```shell
/// echo 'let a;' | biome lint --stdin-file-path=file.js --write
Expand Down Expand Up @@ -345,6 +355,11 @@ pub enum BiomeCommand {
///
/// The file doesn't need to exist on disk, what matters is the extension of the file. Based on the extension, Biome knows how to format the code.
///
/// The provided path may also affect whether the input is treated as
/// ignored. If the path doesn't exist on disk (virtual path), Biome
/// won't require it to be part of the project file set, and ignore
/// checks (`files.includes` and VCS ignore rules) are skipped.
///
/// Example:
/// ```shell
/// echo 'let a;' | biome format --stdin-file-path=file.js --write
Expand Down
1 change: 1 addition & 0 deletions crates/biome_cli/src/execute/process_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub(crate) fn process_file(ctx: &TraversalOptions, biome_path: &BiomePath) -> Fi
project_key: ctx.project_key,
path: biome_path.clone(),
features: ctx.execution.to_feature(),
skip_ignore_check: false,
})
.with_file_path_and_code_and_tags(
biome_path.to_string(),
Expand Down
4 changes: 4 additions & 0 deletions crates/biome_cli/src/execute/std_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ pub(crate) fn run<'a>(
return Ok(());
}

let skip_ignore_check = !workspace.fs().path_exists(biome_path.as_path());

if mode.is_format() {
let FileFeaturesResult {
features_supported: file_features,
} = workspace.file_features(SupportsFeatureParams {
project_key,
path: biome_path.clone(),
features: FeaturesBuilder::new().with_formatter().build(),
skip_ignore_check,
})?;

if file_features.is_ignored() {
Expand Down Expand Up @@ -126,6 +129,7 @@ pub(crate) fn run<'a>(
.with_assist()
.with_formatter()
.build(),
skip_ignore_check,
})?;

if file_features.is_ignored() {
Expand Down
1 change: 1 addition & 0 deletions crates/biome_cli/src/execute/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ impl TraversalContext for TraversalOptions<'_, '_> {
project_key: self.project_key,
path: biome_path.clone(),
features: self.execution.to_feature(),
skip_ignore_check: false,
});

let can_read = DocumentFileSource::can_read(biome_path);
Expand Down
47 changes: 45 additions & 2 deletions crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::configs::{
CONFIG_DISABLED_FORMATTER, CONFIG_FILE_SIZE_LIMIT, CONFIG_FORMAT, CONFIG_FORMAT_JSONC,
CONFIG_ISSUE_3175_1, CONFIG_ISSUE_3175_2,
CONFIG_DISABLED_FORMATTER, CONFIG_FILE_SIZE_LIMIT, CONFIG_FILES_INCLUDES_EXCLUDES_STDIN_PATH,
CONFIG_FORMAT, CONFIG_FORMAT_JSONC, CONFIG_ISSUE_3175_1, CONFIG_ISSUE_3175_2,
};
use crate::snap_test::{SnapshotPayload, assert_file_contents, markup_to_string};
use crate::{
Expand Down Expand Up @@ -1210,6 +1210,49 @@ fn format_stdin_successfully() {
));
}

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

let config_path = Utf8Path::new("biome.json");
fs.insert(
config_path.into(),
CONFIG_FILES_INCLUDES_EXCLUDES_STDIN_PATH.as_bytes(),
);

console
.in_buffer
.push("function f() {return{}}".to_string());

let (fs, result) = run_cli(
fs,
&mut console,
Args::from(["format", "--stdin-file-path", "a.tsx"].as_slice()),
);

assert!(result.is_ok(), "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, "function f() {\n\treturn {};\n}\n");

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

#[test]
fn format_stdin_with_errors() {
let fs = MemoryFileSystem::default();
Expand Down
13 changes: 13 additions & 0 deletions crates/biome_cli/tests/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ pub const CONFIG_DISABLED_FORMATTER_JSONC: &str = r#"{
}
"#;

pub const CONFIG_FILES_INCLUDES_EXCLUDES_STDIN_PATH: &str = r#"{
"files": {
"includes": [
"apps/**/*.{ts,tsx}",
"packages/**/*.{ts,tsx}"
]
},
"formatter": {
"enabled": true
}
}
"#;

pub const CONFIG_ALL_FIELDS: &str = r#"{
"formatter": {
"enabled": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ Available options:
code.
Also, if you have overrides configured and/or nested configurations,
the path may determine the settings being applied.
The provided path may also affect whether the input is treated as
ignored. If the path doesn't exist on disk (virtual path), Biome won't
require it to be part of the project file set, and ignore checks
(`files.includes` and VCS ignore rules) are skipped.
Example: ```shell echo 'let a;' | biome check
--stdin-file-path=file.js --write ```
--staged When set to true, only the files that have been staged (the ones
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ Available options:
The file doesn't need to exist on disk, what matters is the extension
of the file. Based on the extension, Biome knows how to format the
code.
The provided path may also affect whether the input is treated as
ignored. If the path doesn't exist on disk (virtual path), Biome won't
require it to be part of the project file set, and ignore checks
(`files.includes` and VCS ignore rules) are skipped.
Example: ```shell echo 'let a;' | biome format
--stdin-file-path=file.js --write ```
--write Writes formatted files to a file system.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: redactor(content)
---
## `biome.json`

```json
{
"files": {
"includes": ["apps/**/*.{ts,tsx}", "packages/**/*.{ts,tsx}"]
},
"formatter": {
"enabled": true
}
}
```

# Input messages

```block
function f() {return{}}
```

# Emitted Messages

```block
function f() {
return {};
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ Available options:
print the output to `stdout`.
The file doesn't need to exist on disk, what matters is the extension
of the file. Based on the extension, Biome knows how to lint the code.
The provided path may also affect whether the input is treated as
ignored. If the path doesn't exist on disk (virtual path), Biome won't
require it to be part of the project file set, and ignore checks
(`files.includes` and VCS ignore rules) are skipped.
Example: ```shell echo 'let a;' | biome lint --stdin-file-path=file.js
--write ```
--staged When set to true, only the files that have been staged (the ones
Expand Down
1 change: 1 addition & 0 deletions crates/biome_formatter_test/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl<'a> SpecTestFile<'a> {
project_key,
path: input_file.clone(),
features: FeaturesBuilder::new().with_formatter().build(),
skip_ignore_check: false,
})
.unwrap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ invalid.js:9:23 lint/nursery/noUndeclaredEnvVars ━━━━━━━━━━
10 │
11 │ // Bracket notation with string literals should also be checked

i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.


```

Expand All @@ -104,6 +106,8 @@ invalid.js:12:20 lint/nursery/noUndeclaredEnvVars ━━━━━━━━━━
13 │ const bracketMeta = import.meta.env["BRACKET_META_VAR"];
14 │

i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.


```

Expand All @@ -119,6 +123,8 @@ invalid.js:13:21 lint/nursery/noUndeclaredEnvVars ━━━━━━━━━━
14 │
15 │ // Bun.env should also be checked

i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.


```

Expand All @@ -133,6 +139,8 @@ invalid.js:16:16 lint/nursery/noUndeclaredEnvVars ━━━━━━━━━━
17 │ const bunBracketVar = Bun.env["BUN_BRACKET_VAR"];
18 │

i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.


```

Expand All @@ -148,6 +156,8 @@ invalid.js:17:23 lint/nursery/noUndeclaredEnvVars ━━━━━━━━━━
18 │
19 │ // Deno.env.get should also be checked

i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.


```

Expand All @@ -162,6 +172,8 @@ invalid.js:20:17 lint/nursery/noUndeclaredEnvVars ━━━━━━━━━━
21 │ const denoVar2 = Deno.env.get("ANOTHER_DENO_VAR");
22 │

i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.


```

Expand Down
2 changes: 2 additions & 0 deletions crates/biome_lsp/src/handlers/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub(crate) fn code_actions(
project_key: doc.project_key,
path: path.clone(),
features,
skip_ignore_check: false,
})?;

if !file_features.supports_lint() && !file_features.supports_assist() {
Expand Down Expand Up @@ -315,6 +316,7 @@ fn fix_all(
.with_linter()
.with_assist()
.build(),
skip_ignore_check: false,
})?;
let should_format = file_features.supports_format();

Expand Down
3 changes: 3 additions & 0 deletions crates/biome_lsp/src/handlers/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) fn format(
project_key: doc.project_key,
path: path.clone(),
features,
skip_ignore_check: false,
})?;
if !file_features.supports_format() {
return notify_user(file_features, path);
Expand Down Expand Up @@ -107,6 +108,7 @@ pub(crate) fn format_range(
project_key: doc.project_key,
path: path.clone(),
features,
skip_ignore_check: false,
})?;
if !file_features.supports_format() {
return notify_user(file_features, path);
Expand Down Expand Up @@ -199,6 +201,7 @@ pub(crate) fn format_on_type(
project_key: doc.project_key,
path: path.clone(),
features,
skip_ignore_check: false,
})?;
if !file_features.supports_format() {
return notify_user(file_features, path);
Expand Down
1 change: 1 addition & 0 deletions crates/biome_lsp/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ impl Session {
project_key: doc.project_key,
features: FeaturesBuilder::new().with_linter().with_assist().build(),
path: biome_path.clone(),
skip_ignore_check: false,
})?;

if !file_features.supports_lint() && !file_features.supports_assist() {
Expand Down
Loading