Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/six-corners-lay.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
---
"@biomejs/biome": minor
"@biomejs/wasm-bundler": minor
"@biomejs/wasm-nodejs": minor
"@biomejs/wasm-web": minor
---

Added new functions to the `@biomejs/wasm-*` packages:
- `fileExists`: returns whether the input file exists in the workspace.
- `isPathIgnored`: returns whether the input path is ignored.
- `updateModuleGraph`: updates the internal module graph of the input path.
- `getModuleGraph`: it returns a serialized version of the internal module graph.
- `scanProjectFolder`: scans the files and directories in the project to build the internal module graph.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ unicode-width = "0.1.12"
ureq = "3.0.12"
url = "2.5.4"
walkdir = "2.5.0"
web-time = "1.1.0"

[profile.dev.package.biome_wasm]
debug = true
Expand Down
1 change: 1 addition & 0 deletions crates/biome_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ serde_json = { workspace = true, features = ["raw_value"] }
smallvec = { workspace = true, features = ["serde"] }
tokio = { workspace = true, features = ["sync"] }
tracing = { workspace = true, features = ["attributes", "log"] }
web-time = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
Expand Down
45 changes: 38 additions & 7 deletions crates/biome_service/src/workspace/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{FeaturesBuilder, IgnoreKind, PathIsIgnoredParams};
use crate::diagnostics::Panic;
use crate::projects::ProjectKey;
use crate::workspace::DocumentFileSource;
use crate::{WatcherInstruction, Workspace, WorkspaceError};
use crate::{Workspace, WorkspaceError};
use biome_diagnostics::serde::Diagnostic;
use biome_diagnostics::{Diagnostic as _, DiagnosticExt, Error, Severity};
use biome_fs::{BiomePath, PathInterner, PathKind, TraversalContext, TraversalScope};
Expand All @@ -22,8 +22,7 @@ use crossbeam::channel::{Receiver, Sender, unbounded};
use std::collections::BTreeSet;
use std::panic::catch_unwind;
use std::sync::RwLock;
use std::thread;
use std::time::{Duration, Instant};
use std::time::Duration;
use tracing::instrument;

pub(crate) struct ScanOptions {
Expand Down Expand Up @@ -75,8 +74,9 @@ impl WorkspaceServer {

let collector = DiagnosticsCollector::new(verbose);

let (duration, diagnostics, configuration_files) = thread::scope(|scope| {
let handler = thread::Builder::new()
#[cfg(not(target_family = "wasm"))]
let (duration, diagnostics, configuration_files) = std::thread::scope(|scope| {
let handler = std::thread::Builder::new()
.name("biome::scanner".to_string())
.spawn_scoped(scope, || collector.run(diagnostics_receiver))
.expect("failed to spawn scanner thread");
Expand All @@ -103,7 +103,10 @@ impl WorkspaceServer {
for folder in folders_to_watch {
let _ = self
.watcher_tx
.try_send(WatcherInstruction::WatchFolder(folder, scan_kind.clone()));
.try_send(crate::WatcherInstruction::WatchFolder(
folder,
scan_kind.clone(),
));
}

// Wait for the collector thread to finish.
Expand All @@ -112,6 +115,32 @@ impl WorkspaceServer {
(duration, diagnostics, configuration_files)
});

// On WASM platforms, run the scanner without spawning a thread.
// The watcher is also not available.
#[cfg(target_family = "wasm")]
let (duration, diagnostics, configuration_files) = {
let ScanFolderResult {
duration,
configuration_files,
..
} = scan_folder(
folder,
ScanContext {
workspace: self,
project_key,
interner,
diagnostics_sender,
evaluated_paths: Default::default(),
scan_kind: scan_kind.clone(),
watch,
},
);

let diagnostics = collector.run(diagnostics_receiver);

(duration, diagnostics, configuration_files)
};

Ok(ScanResult {
diagnostics,
duration,
Expand Down Expand Up @@ -195,6 +224,7 @@ struct ScanFolderResult {
pub configuration_files: Vec<BiomePath>,

/// List of directories that need to be watched.
#[cfg_attr(target_family = "wasm", allow(dead_code))]
pub folders_to_watch: Vec<Utf8PathBuf>,
}

Expand All @@ -203,7 +233,8 @@ struct ScanFolderResult {
/// Returns the duration of the process and the evaluated paths.
#[instrument(level = "debug", skip(ctx))]
fn scan_folder(folder: &Utf8Path, ctx: ScanContext) -> ScanFolderResult {
let start = Instant::now();
let start = web_time::Instant::now();
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

web_time::Instant is a drop-in replacement of std::time::Instant backed by performance.now() with wasm-bindgen.


let fs = ctx.workspace.fs();
let ctx_ref = &ctx;
fs.traversal(Box::new(move |scope: &dyn TraversalScope| {
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_service/src/workspace_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,12 @@ macro_rules! workspace_method {
}

/// Returns a list of signature for all the methods in the [Workspace] trait
pub fn methods() -> [WorkspaceMethod; 29] {
pub fn methods() -> [WorkspaceMethod; 30] {
[
workspace_method!(file_features),
workspace_method!(update_settings),
workspace_method!(open_project),
workspace_method!(scan_project_folder),
workspace_method!(open_file),
workspace_method!(change_file),
workspace_method!(close_file),
Expand Down
18 changes: 16 additions & 2 deletions crates/biome_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use biome_service::workspace::{
FormatOnTypeParams, FormatRangeParams, GetControlFlowGraphParams, GetFileContentParams,
GetFormatterIRParams, GetModuleGraphParams, GetRegisteredTypesParams, GetSemanticModelParams,
GetSyntaxTreeParams, GetTypeInfoParams, OpenProjectParams, PathIsIgnoredParams,
PullActionsParams, PullDiagnosticsParams, RenameParams, UpdateModuleGraphParams,
UpdateSettingsParams,
PullActionsParams, PullDiagnosticsParams, RenameParams, ScanProjectFolderParams,
UpdateModuleGraphParams, UpdateSettingsParams,
};
use biome_service::workspace::{OpenFileParams, SupportsFeatureParams};
use camino::{Utf8Path, Utf8PathBuf};
Expand Down Expand Up @@ -113,6 +113,20 @@ impl Workspace {
.map_err(into_error)
}

#[wasm_bindgen(js_name = scanProjectFolder)]
pub fn scan_project_folder(
&self,
params: IScanProjectFolderParams,
) -> Result<IScanProjectFolderResult, Error> {
let params: ScanProjectFolderParams =
serde_wasm_bindgen::from_value(params.into()).map_err(into_error)?;
let result = self.inner.scan_project_folder(params).map_err(into_error)?;

to_value(&result)
.map(IScanProjectFolderResult::from)
.map_err(into_error)
}

#[wasm_bindgen(js_name = openFile)]
pub fn open_file(&self, params: IOpenFileParams) -> Result<(), Error> {
let params: OpenFileParams =
Expand Down
47 changes: 47 additions & 0 deletions packages/@biomejs/backend-jsonrpc/src/workspace.ts

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

Loading