From 31d5416cc7c9cd221e857b06eb97bd2393b252b7 Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Thu, 13 Feb 2025 19:15:47 +0100 Subject: [PATCH] refactor(biome_fs): make paths absolute to correctly match against globs (#5017) --- .github/workflows/pull_request.yml | 2 +- Cargo.lock | 1 + Cargo.toml | 1 + crates/biome_cli/Cargo.toml | 2 +- crates/biome_cli/src/execute/traverse.rs | 7 +++---- crates/biome_fs/Cargo.toml | 1 + crates/biome_fs/src/fs/os.rs | 14 ++++++++++++++ e2e-tests/relative-apth-ignore-file/biome.json | 4 ++++ e2e-tests/relative-apth-ignore-file/file.js | 1 + e2e-tests/relative-apth-ignore-file/test.sh | 3 +++ 10 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 e2e-tests/relative-apth-ignore-file/biome.json create mode 100644 e2e-tests/relative-apth-ignore-file/file.js create mode 100755 e2e-tests/relative-apth-ignore-file/test.sh diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index f7300fa7faca..a5fefa8fb9da 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -149,7 +149,7 @@ jobs: pnpm --filter @biomejs/js-api run test:ci e2e-tests: - name: end-to-end tests + name: End-to-end tests runs-on: ubuntu-latest steps: - name: Checkout PR branch diff --git a/Cargo.lock b/Cargo.lock index 4c4861e15e11..8eb77c475738 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -531,6 +531,7 @@ dependencies = [ "oxc_resolver", "papaya", "parking_lot", + "path-absolutize", "rayon", "rustc-hash 2.1.0", "schemars", diff --git a/Cargo.toml b/Cargo.toml index b4f3969abbb4..2cdbaa8150d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -196,6 +196,7 @@ insta = "1.42.1" natord = "1.0.9" oxc_resolver = "4.0" papaya = "0.1.8" +path-absolutize = { version = "3.1.1", optional = false, features = ["use_unix_paths_on_wasm"] } proc-macro-error2 = { version = "2.0.1", default-features = false } proc-macro2 = "1.0.86" quickcheck = "1.0.3" diff --git a/crates/biome_cli/Cargo.toml b/crates/biome_cli/Cargo.toml index 27c224cd7585..dd54d486e257 100644 --- a/crates/biome_cli/Cargo.toml +++ b/crates/biome_cli/Cargo.toml @@ -44,7 +44,7 @@ bpaf = { workspace = true, features = ["bright-color"] } camino = { workspace = true } crossbeam = { workspace = true } dashmap = { workspace = true } -path-absolutize = { version = "3.1.1", optional = false, features = ["use_unix_paths_on_wasm"] } +path-absolutize = { workspace = true } quick-junit = "0.5.1" rayon = { workspace = true } regex = { workspace = true } diff --git a/crates/biome_cli/src/execute/traverse.rs b/crates/biome_cli/src/execute/traverse.rs index 7dd8647506b8..e518413ec075 100644 --- a/crates/biome_cli/src/execute/traverse.rs +++ b/crates/biome_cli/src/execute/traverse.rs @@ -25,7 +25,6 @@ use std::{ env::current_dir, ffi::OsString, panic::catch_unwind, - path::PathBuf, sync::atomic::{AtomicUsize, Ordering}, thread, time::{Duration, Instant}, @@ -101,7 +100,7 @@ pub(crate) fn traverse( // contains are properly closed once the traversal finishes let (elapsed, evaluated_paths) = traverse_inputs( fs, - inputs, + &inputs, &TraversalOptions { fs, workspace, @@ -159,7 +158,7 @@ pub(crate) fn traverse( /// run it to completion, returning the duration of the process and the evaluated paths fn traverse_inputs( fs: &dyn FileSystem, - inputs: Vec, + inputs: &[OsString], ctx: &TraversalOptions, ) -> (Duration, BTreeSet) { let start = Instant::now(); @@ -167,7 +166,7 @@ fn traverse_inputs( for input in inputs { scope.evaluate( ctx, - Utf8PathBuf::from_path_buf(PathBuf::from(input)).expect("Valid UTF-8 path"), + Utf8PathBuf::from_path_buf(input.into()).expect("Valid UTF-8 path"), ); } })); diff --git a/crates/biome_fs/Cargo.toml b/crates/biome_fs/Cargo.toml index 68b17b818e1c..24964602f9bc 100644 --- a/crates/biome_fs/Cargo.toml +++ b/crates/biome_fs/Cargo.toml @@ -21,6 +21,7 @@ enumflags2 = { workspace = true, features = ["serde"] } oxc_resolver = { workspace = true } papaya = { workspace = true } parking_lot = { version = "0.12.3", features = ["arc_lock"] } +path-absolutize = { workspace = true } rayon = { workspace = true } rustc-hash = { workspace = true } schemars = { workspace = true, optional = true } diff --git a/crates/biome_fs/src/fs/os.rs b/crates/biome_fs/src/fs/os.rs index 0df2147b67e4..8cae38e5819b 100644 --- a/crates/biome_fs/src/fs/os.rs +++ b/crates/biome_fs/src/fs/os.rs @@ -8,6 +8,7 @@ use crate::{ use biome_diagnostics::{DiagnosticExt, Error, IoError, Severity}; use camino::{Utf8DirEntry, Utf8Path, Utf8PathBuf}; use oxc_resolver::{FsResolution, ResolveError, ResolveOptions, Resolver}; +use path_absolutize::Absolutize; use rayon::{scope, Scope}; use std::env::temp_dir; use std::fs::FileType; @@ -235,6 +236,17 @@ impl<'scope> OsTraversalScope<'scope> { impl<'scope> TraversalScope<'scope> for OsTraversalScope<'scope> { fn evaluate(&self, ctx: &'scope dyn TraversalContext, path: Utf8PathBuf) { + // Path must be absolute in order to properly normalize them before matching against globs. + // + // FIXME: This code should be moved to the `traverse_inputs` function in `biome_cli/src/traverse.rs`. + // Unfortunately moving this code to the `traverse_inputs` function makes many tests fail. + // The issue is coming from some bugs in our test infra. + // See https://github.com/biomejs/biome/pull/5017 + let path = match std::path::Path::new(&path).absolutize() { + Ok(std::borrow::Cow::Owned(absolutized)) => Utf8PathBuf::from_path_buf(absolutized) + .expect("Absolute path must be correctly parsed"), + _ => path, + }; let file_type = match path.metadata() { Ok(meta) => meta.file_type(), Err(err) => { @@ -511,6 +523,8 @@ impl TemporaryFs { /// Creates a file under the working directory pub fn create_file(&mut self, name: &str, content: &str) { let path = self.working_directory.join(name); + std::fs::create_dir_all(path.parent().expect("parent dir exists.")) + .expect("Temporary directory to exist and being writable"); std::fs::write(path.as_std_path(), content) .expect("Temporary directory to exist and being writable"); self.files.push((path, content.to_string())); diff --git a/e2e-tests/relative-apth-ignore-file/biome.json b/e2e-tests/relative-apth-ignore-file/biome.json new file mode 100644 index 000000000000..69139ce9b40a --- /dev/null +++ b/e2e-tests/relative-apth-ignore-file/biome.json @@ -0,0 +1,4 @@ +{ + "files": { "includes": ["**", "!file.js"] }, + "linter": { "rules": { "suspicious": { "noDebugger": "error" } } } +} \ No newline at end of file diff --git a/e2e-tests/relative-apth-ignore-file/file.js b/e2e-tests/relative-apth-ignore-file/file.js new file mode 100644 index 000000000000..9f5fb105ad0c --- /dev/null +++ b/e2e-tests/relative-apth-ignore-file/file.js @@ -0,0 +1 @@ +debugger; \ No newline at end of file diff --git a/e2e-tests/relative-apth-ignore-file/test.sh b/e2e-tests/relative-apth-ignore-file/test.sh new file mode 100755 index 000000000000..75a053b3ed91 --- /dev/null +++ b/e2e-tests/relative-apth-ignore-file/test.sh @@ -0,0 +1,3 @@ +set -eu + +cargo run --bin biome -- lint .