Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(core): refactor cargo crates #30130

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
341 changes: 275 additions & 66 deletions Cargo.lock

Large diffs are not rendered by default.

88 changes: 83 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,87 @@

[workspace]
resolver = '2'
members = [
'packages/nx',
]
resolver = "2"
members = ["crates/*", "packages/nx"]

[workspace.package]
edition = "2021"
license = "MIT"
version = "0.1.0"

[workspace.dependencies]
nx_glob = { path = "crates/nx_glob" }
nx_walker = { path = "crates/nx_walker" }
nx_db = { path = "crates/nx_db" }
nx_hasher = { path = "crates/nx_hasher" }
nx_tasks = { path = "crates/nx_tasks" }
nx_workspace = { path = "crates/nx_workspace" }
nx_project_graph = { path = "crates/nx_project_graph" }
nx_cache = { path = "crates/nx_cache" }
nx_terminal = { path = "crates/nx_terminal" }
nx_watch = { path = "crates/nx_watch" }
nx_utils = { path = "crates/nx_utils" }
nx_logger = { path = "crates/nx_logger" }
nx_core = { path = "crates/nx_core" }
nx_pty = { path = "crates/nx_pty" }

anyhow = "1.0.95"
colored = "2"
crossbeam-channel = '0.5'
dashmap = { version = "5.5.3", features = ["rayon"] }
dunce = "1"
fs_extra = "1.3.0"
hashbrown = { version = "0.14.5", features = ["rayon", "rkyv"] }
ignore = '0.4'
itertools = "0.10.5"
globset = "0.4.10"
once_cell = "1.18.0"
parking_lot = { version = "0.12.1", features = ["send_guard"] }
napi = { version = '2.16.16', default-features = false, features = [
'anyhow',
'napi4',
'tokio_rt',
] }
napi-derive = '2.16.13'
nom = '7.1.3'
regex = "1.9.1"
rayon = "1.7.0"
rkyv = { version = "0.7", features = ["validation"] }
thiserror = "1.0.40"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
walkdir = '2.3.3'
xxhash-rust = { version = '0.8.5', features = ['xxh3', 'xxh64'] }
swc_common = "0.31.16"
swc_ecma_parser = { version = "0.137.1", features = ["typescript"] }
swc_ecma_visit = "0.93.0"
swc_ecma_ast = "0.107.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

#windows dependencies
winapi = { version = "0.3", features = ["fileapi"] }

#unix dependencies
mio = "0.8"

#non-wasm dependencies
portable-pty = { git = "https://github.com/cammisuli/wezterm", rev = "b538ee29e1e89eeb4832fb35ae095564dce34c29" }
crossterm = "0.27.0"
ignore-files = "2.1.0"
fs4 = "0.12.0"
rusqlite = { version = "0.32.1", features = ["bundled", "array", "vtab"] }
watchexec = "3.0.1"
watchexec-events = "2.0.1"
watchexec-filterer-ignore = "3.0.0"
watchexec-signals = "2.1.0"
machine-uid = "0.5.2"

# build dependencies
napi-build = '2.1.4'
assert_fs = "1.0.10"
# This is only used for unit tests
swc_ecma_dep_graph = "0.109.1"
tempfile = "3.13.0"
tokio = "1.38.0"

[profile.release]
lto = true
23 changes: 23 additions & 0 deletions crates/nx_cache/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "nx_cache"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }

[dependencies]
anyhow = { workspace = true }
tracing = { workspace = true }
nx_hasher = { workspace = true }
nx_walker = { workspace = true }
nx_glob = { workspace = true }
nx_db = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
parking_lot = { workspace = true }
rayon = { workspace = true }
rkyv = { workspace = true }

[dev-dependencies]
assert_fs = { workspace = true }
tempfile = { workspace = true }
4 changes: 4 additions & 0 deletions crates/nx_cache/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// Placeholder function for cache operations
pub fn cache_task_result(_task_id: &str, _result: &[u8]) -> anyhow::Result<()> {
Ok(())
}
9 changes: 9 additions & 0 deletions crates/nx_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "nx_core"
edition.workspace = true
license.workspace = true
version.workspace = true

[dependencies]
napi = { workspace = true}
napi-derive = { workspace = true}
2 changes: 2 additions & 0 deletions crates/nx_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod types;

3 changes: 3 additions & 0 deletions crates/nx_core/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod inputs;
pub mod nx_json;
pub mod project_graph;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use napi::bindgen_prelude::Either7;
use napi::Either;
use napi_derive::napi;

#[napi(object)]
pub struct InputsInput {
Expand Down Expand Up @@ -34,7 +35,7 @@ pub struct DepsOutputsInput {
pub transitive: Option<bool>,
}

pub(crate) type JsInputs = Either7<
pub type JsInputs = Either7<
InputsInput,
String,
FileSetInput,
Expand Down Expand Up @@ -88,7 +89,7 @@ impl<'a> From<&'a JsInputs> for Input<'a> {
}

#[derive(Debug)]
pub(crate) enum Input<'a> {
pub enum Input<'a> {
Inputs {
input: &'a str,
dependencies: bool,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::native::types::JsInputs;
use std::collections::HashMap;
use napi_derive::napi;
use crate::types::inputs::JsInputs;

#[napi(object)]
/// Stripped version of the NxJson interface for use in rust
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::native::types::JsInputs;
use crate::types::inputs::JsInputs;
use napi_derive::napi;
use std::collections::HashMap;

#[napi(object)]
Expand Down
20 changes: 20 additions & 0 deletions crates/nx_db/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "nx_db"
version.workspace = true
edition.workspace = true
license.workspace = true

[dependencies]
napi = { workspace = true }
napi-derive = { workspace = true }
nx_logger = { workspace = true }
nx_utils = { workspace = true }
anyhow = { workspace = true }
tracing = { workspace = true }
fs4 = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rusqlite = { workspace = true }

[dev-dependencies]
tempfile = { workspace = true }
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;

use rusqlite::{Connection, DatabaseName, Error, OptionalExtension, Params, Row, Statement, ToSql};
use std::thread;
use std::time::Duration;
Expand Down Expand Up @@ -53,7 +52,7 @@ macro_rules! retry_db_operation_when_busy {
}

impl NxDbConnection {
pub fn new(connection: Connection) -> Self {
pub(crate) fn new(connection: Connection) -> Self {
Self {
conn: Some(connection),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use crate::native::db::connection::NxDbConnection;
use crate::connection::NxDbConnection;
use rusqlite::{Connection, OpenFlags};
use std::fs::{remove_file, File};
use std::path::{Path, PathBuf};
use tracing::{debug, trace};

pub(super) struct LockFile {
pub(crate) struct LockFile {
file: File,
path: PathBuf,
}

pub(super) fn unlock_file(lock_file: &LockFile) {
pub(crate) fn unlock_file(lock_file: &LockFile) {
if lock_file.path.exists() {
fs4::fs_std::FileExt::unlock(&lock_file.file)
.and_then(|_| remove_file(&lock_file.path))
.ok();
}
}

pub(super) fn create_lock_file(db_path: &Path) -> anyhow::Result<LockFile> {
pub(crate) fn create_lock_file(db_path: &Path) -> anyhow::Result<LockFile> {
let lock_file_path = db_path.with_extension("lock");
let lock_file = File::create(&lock_file_path)
.map_err(|e| anyhow::anyhow!("Unable to create db lock file: {:?}", e))?;
Expand All @@ -32,7 +32,7 @@ pub(super) fn create_lock_file(db_path: &Path) -> anyhow::Result<LockFile> {
})
}

pub(super) fn initialize_db(nx_version: String, db_path: &Path) -> anyhow::Result<NxDbConnection> {
pub(crate) fn initialize_db(nx_version: String, db_path: &Path) -> anyhow::Result<NxDbConnection> {
match open_database_connection(db_path) {
Ok(mut c) => {
trace!(
Expand Down Expand Up @@ -135,8 +135,6 @@ fn configure_database(connection: &NxDbConnection) -> anyhow::Result<()> {

#[cfg(test)]
mod tests {
use crate::native::logger::enable_logger;

use super::*;

#[test]
Expand Down Expand Up @@ -181,10 +179,9 @@ mod tests {

#[test]
fn initialize_db_recreates_incompatible_db() -> anyhow::Result<()> {
enable_logger();
let temp_dir = tempfile::tempdir()?;
let db_path = temp_dir.path().join("test.db");
//

// Create initial db
let _ = initialize_db("1.0.0".to_string(), &db_path)?;

Expand Down
27 changes: 13 additions & 14 deletions packages/nx/src/native/db/mod.rs → crates/nx_db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
pub mod connection;
mod initialize;
pub mod napi;

use crate::native::logger::enable_logger;
use crate::native::machine_id::get_machine_id;
use crate::native::{db::connection::NxDbConnection, hasher::hash};
use napi::bindgen_prelude::External;
use std::fs::create_dir_all;
use std::path::PathBuf;
use std::{mem, process};
use tracing::{trace, trace_span};

#[napi]
pub fn connect_to_nx_db(
use connection::NxDbConnection;

/// Connect to the Nx database
pub fn open_db_connection(
cache_dir: String,
nx_version: String,
db_name: Option<String>,
) -> anyhow::Result<External<NxDbConnection>> {
enable_logger();
machine_id: String,
) -> anyhow::Result<NxDbConnection> {
let cache_dir_buf = PathBuf::from(cache_dir);
let mut db_file_name = db_name.unwrap_or_else(get_machine_id);
let mut db_file_name = db_name.unwrap_or(machine_id);

if db_file_name.is_empty() {
trace!("Invalid db file name, using fallback name");
db_file_name = hash(b"machine");
db_file_name = "machine".to_string();
}

let db_path = cache_dir_buf.join(format!("{}.db", db_file_name));
Expand All @@ -37,12 +36,12 @@ pub fn connect_to_nx_db(

initialize::unlock_file(&lock_file);

Ok(External::new(c))
Ok(c)
})
}

#[napi]
pub fn close_db_connection(mut connection: External<NxDbConnection>) -> anyhow::Result<()> {
let conn = mem::take(connection.as_mut());
/// Close the database connection
pub fn close_db_connection(mut connection: NxDbConnection) -> anyhow::Result<()> {
let conn = mem::take(&mut connection);
conn.close()
}
28 changes: 28 additions & 0 deletions crates/nx_db/src/napi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::NxDbConnection;
use napi::bindgen_prelude::External;
use napi_derive::*;
use nx_logger::enable_logger;
use std::fs::create_dir_all;
use std::path::PathBuf;

use nx_utils::machine_id::get_machine_id;

#[napi]
pub fn connect_to_nx_db(
cache_dir: String,
nx_version: String,
db_name: Option<String>,
) -> anyhow::Result<External<NxDbConnection>> {
enable_logger();
let cache_dir_buf = PathBuf::from(&cache_dir);
create_dir_all(&cache_dir_buf)?;

let machine_id = get_machine_id();
let connection = crate::open_db_connection(cache_dir, nx_version, db_name, machine_id)?;
Ok(External::new(connection))
}

#[napi]
pub fn close_db_connection(mut connection: External<NxDbConnection>) -> anyhow::Result<()> {
crate::close_db_connection(std::mem::take(connection.as_mut()))
}
1 change: 1 addition & 0 deletions crates/nx_glob/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
12 changes: 12 additions & 0 deletions crates/nx_glob/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "nx_glob"
edition.workspace = true
license.workspace = true
version.workspace = true

[dependencies]
globset = { workspace = true }
tracing = { workspace = true }
anyhow = { workspace = true }
itertools = { workspace = true }
nom = { workspace = true }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::native::glob::glob_group::GlobGroup;
use crate::glob_group::GlobGroup;
use nom::branch::alt;
use nom::bytes::complete::{is_not, tag, take_till, take_until, take_while};
use nom::combinator::{eof, map, map_parser};
Expand Down Expand Up @@ -198,8 +198,8 @@ pub fn parse_glob(input: &str) -> anyhow::Result<(bool, Vec<Vec<GlobGroup>>)> {

#[cfg(test)]
mod test {
use crate::native::glob::glob_group::GlobGroup;
use crate::native::glob::glob_parser::{parse_glob, special_char_with_no_group};
use crate::glob_group::GlobGroup;
use crate::glob_parser::{parse_glob, special_char_with_no_group};

#[test]
fn invalid_groups() {
Expand Down
Loading
Loading