Skip to content

Commit

Permalink
refactor: add Workspace scanner (#4682)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr authored Dec 5, 2024
1 parent 3c1c017 commit f5ba162
Show file tree
Hide file tree
Showing 32 changed files with 508 additions and 102 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ indexmap = { version = "2.6.0" }
insta = "1.41.1"
natord = "1.0.9"
oxc_resolver = "1.12.0"
papaya = "0.1.5"
papaya = "0.1.6"
proc-macro2 = "1.0.86"
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "./packages/@biomejs/biome/configuration_schema.json",
"assists": {
"assist": {
"enabled": true,
"ignore": [
"./packages/@biomejs/biome/configuration_schema.json"
Expand Down
13 changes: 12 additions & 1 deletion crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::changed::{get_changed_files, get_staged_files};
use crate::cli_options::{cli_options, CliOptions, CliReporter, ColorsArg};
use crate::execute::Stdin;
use crate::execute::{ReportMode, Stdin};
use crate::logging::LoggingKind;
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, LoggingLevel, VERSION,
Expand Down Expand Up @@ -822,6 +822,17 @@ pub(crate) trait CommandRunner: Sized {
})?;

let execution = self.get_execution(cli_options, console, workspace)?;

if execution.traversal_mode().should_scan_project() {
let result = workspace.scan_current_project_folder(())?;
if cli_options.verbose && matches!(execution.report_mode(), ReportMode::Terminal { .. })
{
console.log(markup! {
<Info>"Scanned project folder in "<Emphasis>{result.duration}</Emphasis>"."</Info>
});
}
}

Ok((execution, paths))
}

Expand Down
6 changes: 4 additions & 2 deletions crates/biome_cli/src/execute/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use biome_json_parser::{parse_json_with_cache, JsonParserOptions};
use biome_json_syntax::{JsonFileSource, JsonRoot};
use biome_migrate::{migrate_configuration, ControlFlow};
use biome_rowan::{AstNode, NodeCache};
use biome_service::workspace::{ChangeFileParams, FixAction, FormatFileParams, OpenFileParams};
use biome_service::workspace::{
ChangeFileParams, FileContent, FixAction, FormatFileParams, OpenFileParams,
};
use std::borrow::Cow;
use std::ffi::OsStr;
use std::path::PathBuf;
Expand Down Expand Up @@ -66,7 +68,7 @@ pub(crate) fn run(migrate_payload: MigratePayload) -> Result<(), CliDiagnostic>
let biome_path = BiomePath::new(configuration_file_path.as_path());
workspace.open_file(OpenFileParams {
path: biome_path.clone(),
content: biome_config_content.to_string(),
content: FileContent::FromClient(biome_config_content.to_string()),
version: 0,
document_file_source: Some(JsonFileSource::json().into()),
})?;
Expand Down
15 changes: 13 additions & 2 deletions crates/biome_cli/src/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use biome_diagnostics::{category, Category};
use biome_fs::BiomePath;
use biome_grit_patterns::GritTargetLanguage;
use biome_service::workspace::{
FeatureName, FeaturesBuilder, FixFileMode, FormatFileParams, OpenFileParams, PatternId,
FeatureName, FeaturesBuilder, FileContent, FixFileMode, FormatFileParams, OpenFileParams,
PatternId,
};
use std::ffi::OsString;
use std::fmt::{Display, Formatter};
Expand Down Expand Up @@ -228,6 +229,16 @@ impl Display for TraversalMode {
}
}

impl TraversalMode {
pub fn should_scan_project(&self) -> bool {
matches!(self, Self::CI { .. })
|| matches!(
self,
Self::Check { stdin,.. } | Self::Lint { stdin, .. } if stdin.is_none()
)
}
}

/// Tells to the execution of the traversal how the information should be reported
#[derive(Copy, Clone, Debug)]
pub enum ReportMode {
Expand Down Expand Up @@ -529,7 +540,7 @@ pub fn execute_mode(
})?;
let report_file = BiomePath::new("_report_output.json");
session.app.workspace.open_file(OpenFileParams {
content,
content: FileContent::FromClient(content),
path: report_file.clone(),
version: 0,
document_file_source: None,
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_cli/src/execute/process_file/workspace_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::execute::diagnostics::{ResultExt, ResultIoExt};
use crate::execute::process_file::SharedTraversalOptions;
use biome_diagnostics::{category, Error};
use biome_fs::{BiomePath, File, OpenOptions};
use biome_service::workspace::{FileGuard, OpenFileParams};
use biome_service::workspace::{FileContent, FileGuard, OpenFileParams};
use biome_service::{Workspace, WorkspaceError};
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -40,7 +40,7 @@ impl<'ctx, 'app> WorkspaceFile<'ctx, 'app> {
document_file_source: None,
path: biome_path,
version: 0,
content: input.clone(),
content: FileContent::FromClient(input.clone()),
},
)
.with_file_path_and_code(path.display().to_string(), category!("internalError/fs"))?;
Expand Down
8 changes: 4 additions & 4 deletions crates/biome_cli/src/execute/std_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use biome_diagnostics::PrintDiagnostic;
use biome_fs::BiomePath;
use biome_service::file_handlers::{AstroFileHandler, SvelteFileHandler, VueFileHandler};
use biome_service::workspace::{
ChangeFileParams, DropPatternParams, FeaturesBuilder, FixFileParams, FormatFileParams,
OpenFileParams, OrganizeImportsParams, SupportsFeatureParams,
ChangeFileParams, DropPatternParams, FeaturesBuilder, FileContent, FixFileParams,
FormatFileParams, OpenFileParams, OrganizeImportsParams, SupportsFeatureParams,
};
use biome_service::WorkspaceError;
use std::borrow::Cow;
Expand Down Expand Up @@ -48,7 +48,7 @@ pub(crate) fn run<'a>(
workspace.open_file(OpenFileParams {
path: biome_path.clone(),
version: 0,
content: content.into(),
content: FileContent::FromClient(content.into()),
document_file_source: None,
})?;
let printed = workspace.format_file(FormatFileParams {
Expand Down Expand Up @@ -80,7 +80,7 @@ pub(crate) fn run<'a>(
workspace.open_file(OpenFileParams {
path: biome_path.clone(),
version: 0,
content: content.into(),
content: FileContent::FromClient(content.into()),
document_file_source: None,
})?;
// apply fix file of the linter
Expand Down
20 changes: 1 addition & 19 deletions crates/biome_cli/src/execute/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ use std::{
ffi::OsString,
panic::catch_unwind,
path::PathBuf,
sync::{
atomic::{AtomicUsize, Ordering},
Once,
},
sync::atomic::{AtomicUsize, Ordering},
thread,
time::{Duration, Instant},
};
Expand All @@ -45,8 +42,6 @@ pub(crate) fn traverse(
cli_options: &CliOptions,
mut inputs: Vec<OsString>,
) -> Result<TraverseResult, CliDiagnostic> {
init_thread_pool();

if inputs.is_empty() {
match &execution.traversal_mode {
TraversalMode::Check { .. }
Expand Down Expand Up @@ -155,19 +150,6 @@ pub(crate) fn traverse(
})
}

/// This function will setup the global Rayon thread pool the first time it's called
///
/// This is currently only used to assign friendly debug names to the threads of the pool
fn init_thread_pool() {
static INIT_ONCE: Once = Once::new();
INIT_ONCE.call_once(|| {
rayon::ThreadPoolBuilder::new()
.thread_name(|index| format!("biome::worker_{index}"))
.build_global()
.expect("failed to initialize the global thread pool");
});
}

/// Initiate the filesystem traversal tasks with the provided input paths and
/// run it to completion, returning the duration of the process and the evaluated paths
fn traverse_inputs(
Expand Down
5 changes: 2 additions & 3 deletions crates/biome_cli/tests/snap_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,10 @@ fn redact_snapshot(input: &str) -> Option<Cow<'_, str>> {
let mut output = Cow::Borrowed(input);

// There are some logs that print the timing, and we can't snapshot that message
// otherwise at each run we invalid the previous snapshot.
// otherwise at each run we invalidate the previous snapshot.
//
// This is a workaround, and it might not work for all cases.
let the_match = TIME_REGEX.find(output.as_ref()).map(|f| f.start()..f.end());
if let Some(found) = the_match {
while let Some(found) = TIME_REGEX.find(&output).map(|f| f.start()..f.end()) {
output.to_mut().replace_range(found, " <TIME>.");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
snapshot_kind: text
---
## `src/file.js`

Expand All @@ -22,6 +21,10 @@ ci ━━━━━━━━━━━━━━━━━━━━━━━━━

# Emitted Messages

```block
Scanned project folder in <TIME>.
```

```block
src/folder_1/package-lock.json project VERBOSE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ check ━━━━━━━━━━━━━━━━━━━━━━━━

# Emitted Messages

```block
Scanned project folder in <TIME>.
```

```block
check.js:1:1 lint/style/useWhile FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ while (true);

# Emitted Messages

```block
Scanned project folder in <TIME>.
```

```block
Checked 1 file in <TIME>. Fixed 1 file.
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ internalError/io ━━━━━━━━━━━━━━━━━━━━━

# Emitted Messages

```block
Scanned project folder in <TIME>.
```

```block
check.txt files/missingHandler VERBOSE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ ci ━━━━━━━━━━━━━━━━━━━━━━━━━

# Emitted Messages

```block
Scanned project folder in <TIME>.
```

```block
ci.js:1:1 lint/style/useWhile FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ lint ━━━━━━━━━━━━━━━━━━━━━━━━━

# Emitted Messages

```block
Scanned project folder in <TIME>.
```

```block
check.js:1:1 lint/style/useWhile FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ internalError/io ━━━━━━━━━━━━━━━━━━━━━

# Emitted Messages

```block
Scanned project folder in <TIME>.
```

```block
check.txt files/missingHandler VERBOSE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_fs/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ impl PathInterner {
(interner, recv)
}

/// Insert the path.
/// Inserts the path.
///
/// Returns `true` if the path was not previously inserted.
pub fn intern_path(&self, path: PathBuf) -> bool {
let result = self.storage.pin().insert(path.clone());
Expand Down
5 changes: 3 additions & 2 deletions crates/biome_lsp/src/handlers/text_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::utils::apply_document_changes;
use crate::{documents::Document, session::Session};
use anyhow::Result;
use biome_service::workspace::{
ChangeFileParams, CloseFileParams, DocumentFileSource, GetFileContentParams, OpenFileParams,
ChangeFileParams, CloseFileParams, DocumentFileSource, FileContent, GetFileContentParams,
OpenFileParams,
};
use tower_lsp::lsp_types;
use tracing::{error, field};
Expand Down Expand Up @@ -31,7 +32,7 @@ pub(crate) async fn did_open(
session.workspace.open_file(OpenFileParams {
path: biome_path,
version,
content,
content: FileContent::FromClient(content),
document_file_source: Some(language_hint),
})?;

Expand Down
1 change: 1 addition & 0 deletions crates/biome_lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ impl ServerFactory {
workspace_method!(builder, is_path_ignored);
workspace_method!(builder, update_settings);
workspace_method!(builder, register_project_folder);
workspace_method!(builder, scan_current_project_folder);
workspace_method!(builder, unregister_project_folder);
workspace_method!(builder, open_file);
workspace_method!(builder, set_manifest_for_project);
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ biome_rowan = { workspace = true, features = ["serde"] }
biome_string_case = { workspace = true }
biome_text_edit = { workspace = true }
bpaf = { workspace = true }
crossbeam = { workspace = true }
enumflags2 = { workspace = true, features = ["serde"] }
getrandom = { workspace = true, features = ["js"] }
ignore = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
oxc_resolver = { workspace = true }
papaya = { workspace = true }
rayon = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true }
Expand Down
Loading

0 comments on commit f5ba162

Please sign in to comment.