Skip to content

Commit

Permalink
feat(turborepo): use biome to parse package.json (#8255)
Browse files Browse the repository at this point in the history
### Description

Uses `biome_deserialize` to parse `package.json` files. This gives us
span info, and better parse error messages.

### Testing Instructions

The existing tests for `package.json` should suffice.
  • Loading branch information
NicholasLYang authored Jun 20, 2024
1 parent 8711faa commit 29d4f41
Show file tree
Hide file tree
Showing 20 changed files with 300 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup-rust/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ runs:
- name: "Install cargo-sweep"
uses: taiki-e/install-action@v2
with:
tool: [email protected],cargo-groups
tool: [email protected],cargo-groups@0.1.9

- name: "Run cargo-sweep"
uses: ./.github/actions/cargo-sweep
55 changes: 43 additions & 12 deletions Cargo.lock

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

12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ turborepo-lib = { path = "crates/turborepo-lib", default-features = false }
turborepo-lockfiles = { path = "crates/turborepo-lockfiles" }
turborepo-repository = { path = "crates/turborepo-repository" }
turborepo-ui = { path = "crates/turborepo-ui" }
turborepo-unescape = { path = "crates/turborepo-unescape" }
turborepo-scm = { path = "crates/turborepo-scm" }
wax = { path = "crates/turborepo-wax" }
turborepo-vercel-api = { path = "crates/turborepo-vercel-api" }
Expand Down Expand Up @@ -214,11 +215,12 @@ async-trait = "0.1.64"
atty = "0.2.14"
axum = "0.6.2"
axum-server = "0.4.4"
biome_console = "0.5.7"
biome_deserialize = "0.5.7"
biome_diagnostics = "0.5.7"
biome_json_parser = "0.5.7"
biome_json_syntax = "0.5.7"
biome_console = { version = "0.5.7" }
biome_deserialize = { version = "0.6.0", features = ["serde"] }
biome_deserialize_macros = { version = "0.6.0" }
biome_diagnostics = { version = "0.5.7" }
biome_json_parser = { version = "0.5.7" }
biome_json_syntax = { version = "0.5.7" }
bytes = "1.1.0"
camino = { version = "1.1.4", features = ["serde1"] }
chrono = "0.4.23"
Expand Down
3 changes: 2 additions & 1 deletion crates/turborepo-errors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

[package]
name = "turborepo-errors"
version = "0.1.0"
Expand All @@ -9,8 +8,10 @@ license = "MIT"

[dependencies]
biome_deserialize = { workspace = true }
biome_diagnostics = { workspace = true }
miette = { workspace = true }
serde = { workspace = true, features = ["derive"] }
thiserror = { workspace = true }

[dev-dependencies]
serde_json = { workspace = true }
Expand Down
42 changes: 41 additions & 1 deletion crates/turborepo-errors/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,54 @@
use std::{
fmt::Display,
ops::{Deref, Range},
sync::Arc,
};

use biome_deserialize::{Deserializable, DeserializableValue, DeserializationDiagnostic};
use miette::{NamedSource, SourceSpan};
use miette::{Diagnostic, NamedSource, SourceSpan};
use serde::{Deserialize, Serialize};
use thiserror::Error;

pub const TURBO_SITE: &str = "https://turbo.build";

/// A little helper to convert from biome's syntax errors to miette.
#[derive(Debug, Error, Diagnostic)]
#[error("{message}")]
pub struct ParseDiagnostic {
message: String,
#[source_code]
source_code: String,
#[label]
label: Option<SourceSpan>,
}

struct BiomeMessage<'a>(&'a biome_diagnostics::Error);

impl Display for BiomeMessage<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.description(f)
}
}

impl From<biome_diagnostics::Error> for ParseDiagnostic {
fn from(diagnostic: biome_diagnostics::Error) -> Self {
let location = diagnostic.location();
let message = BiomeMessage(&diagnostic).to_string();
Self {
message,
source_code: location
.source_code
.map(|s| s.text.to_string())
.unwrap_or_default(),
label: location.span.map(|span| {
let start: usize = span.start().into();
let len: usize = span.len().into();
(start, len).into()
}),
}
}
}

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, Eq)]
#[serde(transparent)]
pub struct Spanned<T> {
Expand Down
3 changes: 2 additions & 1 deletion crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ workspace = true
atty = { workspace = true }
axum = { workspace = true }
biome_deserialize = { workspace = true }
biome_deserialize_macros = "0.5.7"
biome_deserialize_macros = { workspace = true }
biome_diagnostics = { workspace = true }
biome_json_parser = { workspace = true }
biome_json_syntax = { workspace = true }
Expand Down Expand Up @@ -128,6 +128,7 @@ turborepo-repository = { path = "../turborepo-repository" }
turborepo-scm = { workspace = true }
turborepo-telemetry = { path = "../turborepo-telemetry" }
turborepo-ui = { workspace = true }
turborepo-unescape = { workspace = true }
turborepo-vercel-api = { path = "../turborepo-vercel-api" }
twox-hash = "1.6.3"
uds_windows = "1.0.2"
Expand Down
1 change: 0 additions & 1 deletion crates/turborepo-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ mod task_graph;
mod task_hash;
mod tracing;
mod turbo_json;
mod unescape;

pub use crate::{
child::spawn_child,
Expand Down
5 changes: 2 additions & 3 deletions crates/turborepo-lib/src/run/task_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ use tracing::{debug, error, warn};
use turbopath::{AbsoluteSystemPathBuf, PathRelation};
use turborepo_cache::AsyncCache;
use turborepo_scm::SCM;
use turborepo_unescape::UnescapedString;

use super::ConfigCache;
use crate::{
config::RawTurboJson, gitignore::ensure_turbo_is_gitignored, unescape::UnescapedString,
};
use crate::{config::RawTurboJson, gitignore::ensure_turbo_is_gitignored};

// Environment variable key that will be used to enable, and set the expected
// trace location
Expand Down
4 changes: 2 additions & 2 deletions crates/turborepo-lib/src/turbo_json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tracing::debug;
use turbopath::{AbsoluteSystemPath, AnchoredSystemPath};
use turborepo_errors::Spanned;
use turborepo_repository::{package_graph::ROOT_PKG_NAME, package_json::PackageJson};
use turborepo_unescape::UnescapedString;

use crate::{
cli::OutputLogsMode,
Expand All @@ -22,7 +23,6 @@ use crate::{
task_id::{TaskId, TaskName},
},
task_graph::{TaskDefinition, TaskOutputs},
unescape::UnescapedString,
};

pub mod parser;
Expand Down Expand Up @@ -735,14 +735,14 @@ mod tests {
use test_case::test_case;
use turbopath::{AbsoluteSystemPath, AnchoredSystemPath};
use turborepo_repository::package_json::PackageJson;
use turborepo_unescape::UnescapedString;

use super::{Pipeline, RawTurboJson, Spanned, UI};
use crate::{
cli::OutputLogsMode,
run::task_id::TaskName,
task_graph::{TaskDefinition, TaskOutputs},
turbo_json::{RawTaskDefinition, TurboJson},
unescape::UnescapedString,
};

#[test_case(r"{}", TurboJson::default() ; "empty")]
Expand Down
Loading

0 comments on commit 29d4f41

Please sign in to comment.