Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion docs/pm/cli/publish.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bun publish ./package.tgz

### `--recursive` and `--filter`

In a monorepo, `--recursive` (`-r`) publishes the root package and every workspace package, dependencies before their dependents. `--filter` (`-F`) does the same for the matching workspace packages only.
In a monorepo, `--recursive` (`-r`) publishes the root package and every workspace package, dependencies before their dependents. `--filter` (`-F`) does the same for the matching workspace packages only. Packages that depend on each other in a cycle come after everything else they depend on, and the order inside a cycle is not specified.

```sh terminal icon="terminal"
bun publish --recursive
Expand Down
4 changes: 4 additions & 0 deletions docs/pm/filter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,7 @@ Use `--if-present` with `--workspaces` to skip packages that don't have the requ
### Dependency Order

Bun respects package dependency order when running scripts. Say you have a package `foo` that depends on another package `bar` in your workspace, and both have a `build` script. When you run `bun --filter '*' build`, `foo` only starts once `bar` is done.

Packages that depend on each other in a cycle can't wait for each other, and the order inside a cycle is not specified. Every other order stays: the packages of a cycle wait for what they depend on outside it, and a package that depends on one of them waits for all of them.

A script that never exits, such as a dev server, keeps the packages that depend on it waiting. `--parallel` starts every script at once.
3 changes: 3 additions & 0 deletions src/collections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ pub use hashbrown;
pub mod string_map;
pub use string_map::StringMap;

mod strongly_connected_components;
pub use strongly_connected_components::strongly_connected_components;

// Re-export from bun_ptr so callers can name it as `bun_collections::TaggedPtrUnion`
// (PORTING.md groups it under Collections; the impl lives in src/ptr/).
pub use bun_ptr::tagged_pointer::{TaggedPtr, TaggedPtrUnion};
Expand Down
61 changes: 61 additions & 0 deletions src/collections/strongly_connected_components.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/// Tarjan's algorithm, without recursion. Returns each node's component number, and each component's nodes in ascending
/// order. A component has a higher number than every component it has an edge to.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub fn strongly_connected_components(edges: &[Vec<u32>]) -> (Vec<u32>, Vec<Vec<usize>>) {
const NOT_VISITED: u32 = u32::MAX;
#[derive(Clone)]
struct Node {
index: u32,
lowlink: u32,
is_on_stack: bool,
}
let not_visited = Node {
index: NOT_VISITED,
lowlink: 0,
is_on_stack: false,
};
let mut nodes = vec![not_visited; edges.len()];
let mut component_of = vec![0u32; edges.len()];
let mut components: Vec<Vec<usize>> = Vec::new();
let (mut stack, mut visited) = (Vec::new(), 0u32);
// A node in progress, and how many of its edges have been followed.
let mut in_progress: Vec<(usize, usize)> = Vec::new();
for root in 0..edges.len() {
if nodes[root].index != NOT_VISITED {
continue;
}
in_progress.push((root, 0));
while let Some(&(node, followed)) = in_progress.last() {
if followed == 0 {
(nodes[node].index, nodes[node].lowlink) = (visited, visited);
nodes[node].is_on_stack = true;
visited += 1;
stack.push(node);
}
if let Some(&next) = edges[node].get(followed) {
in_progress.last_mut().unwrap().1 += 1;
let next = next as usize;
if nodes[next].index == NOT_VISITED {
in_progress.push((next, 0));
} else if nodes[next].is_on_stack {
nodes[node].lowlink = nodes[node].lowlink.min(nodes[next].index);
}
continue;
}
in_progress.pop();
if let Some(&(parent, _)) = in_progress.last() {
nodes[parent].lowlink = nodes[parent].lowlink.min(nodes[node].lowlink);
}
if nodes[node].lowlink == nodes[node].index {
let from = stack.iter().rposition(|&it| it == node).unwrap();
let mut component = stack.split_off(from);
for &member in &component {
nodes[member].is_on_stack = false;
component_of[member] = components.len() as u32;
}
component.sort_unstable();
components.push(component);
}
}
}
(component_of, components)
}
32 changes: 22 additions & 10 deletions src/install/PackageManager/workspace_manifests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
use super::PackageManager;
use super::add_remove_with_filter::{WorkspaceTarget, fetch_entry, root_package_json_path};
use super::workspace_package_json_cache::GetJSONOptions;
use super::workspace_selection::WorkspaceGraph;
use super::workspace_selection::{GivesWay, WorkspaceGraph, dependency_order};

/// Root + member package.json files parsed the way `bun install` parses them, into a throw-away lockfile.
/// Errors the parse only logs (an invalid catalog range, say) fail it, as in `bun install`.
Expand Down Expand Up @@ -482,12 +482,12 @@ impl WorkspaceManifests {
}
}

/// Graph index i == `targets[i]`; the target whose `name_hash` is `None` is the root.
pub(crate) fn relation_graph(
/// Each `(from, to, gives_way)` says that `targets[from]` depends on `targets[to]`. The target whose `name_hash` is `None` is the root.
fn relation_edges(
manager: &mut PackageManager,
targets: &[&WorkspaceTarget],
pattern: &[u8],
) -> WorkspaceGraph {
) -> Vec<(u32, u32, GivesWay)> {
let mut scratch = ScratchManifests::new();
if let Err(err) = scratch.parse_root(manager, Features::main()) {
crash_for_filter(&mut scratch.log, pattern, err);
Expand Down Expand Up @@ -519,7 +519,7 @@ pub(crate) fn relation_graph(
}
}

let mut edges: Vec<(u32, u32)> = Vec::new();
let mut edges: Vec<(u32, u32, GivesWay)> = Vec::new();
for (from, pkg) in &parsed {
for dep in pkg.dependencies.get(dbuf) {
if dep.behavior == Behavior::WORKSPACE {
Expand Down Expand Up @@ -551,11 +551,24 @@ pub(crate) fn relation_graph(
if to == *from {
continue;
}
edges.push((*from, to));
edges.push((*from, to, GivesWay::of(dep.behavior)));
}
}

WorkspaceGraph::from_edges(targets.len(), edges)
edges
}

/// Graph index i == `targets[i]`.
pub(crate) fn relation_graph(
manager: &mut PackageManager,
targets: &[&WorkspaceTarget],
pattern: &[u8],
) -> WorkspaceGraph {
let edges = relation_edges(manager, targets, pattern);
WorkspaceGraph::from_edges(
targets.len(),
edges.into_iter().map(|(from, to, _)| (from, to)),
)
}

/// Returns the package.json paths that `--filter` selects (all of them without a filter). Each one
Expand All @@ -572,13 +585,12 @@ pub fn selected_in_dependency_order(
}
};
let pattern = manager.options.filter_patterns.first().copied();
let graph = relation_graph(
let edges = relation_edges(
manager,
&targets.iter().collect::<Vec<_>>(),
pattern.unwrap_or(b"*"),
);
graph
.dependency_order()
dependency_order(targets.len(), &edges)
.into_iter()
.map(|i| targets[i as usize].package_json_path.clone())
.collect()
Expand Down
73 changes: 70 additions & 3 deletions src/install/PackageManager/workspace_selection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::VecDeque;

use bstr::BStr;
use bun_collections::{DynamicBitSet, HashMap, index_sort};
use bun_collections::{DynamicBitSet, HashMap, index_sort, strongly_connected_components};
use bun_core::{Global, Output, UnwrapOrOom as _, strings};
use bun_glob::Prepared;
use bun_paths::path_buffer_pool;
Expand Down Expand Up @@ -389,7 +389,8 @@ impl WorkspaceGraph {
}

/// Candidate indices, each after those it depends on. A cycle is broken at one of its members.
pub(crate) fn dependency_order(&self) -> Vec<u32> {
/// Also returns how many candidates were in order when it first met a cycle.
fn order_or_stall(&self) -> (Vec<u32>, Option<usize>) {
let n = self.dependencies.len();
let mut remaining: Vec<usize> = self.dependencies.iter().map(Vec::len).collect();
let mut order: Vec<u32> = Vec::with_capacity(n);
Expand All @@ -402,6 +403,7 @@ impl WorkspaceGraph {
}
}

let mut stalled_at = None;
loop {
while let Some(u) = queue.pop_front() {
order.push(u);
Expand All @@ -417,8 +419,9 @@ impl WorkspaceGraph {
}
}
if order.len() == n {
return order;
return (order, stalled_at);
}
stalled_at.get_or_insert(order.len());
// The sort is stuck on a cycle. Follow unmet dependencies until one repeats, which
// means that candidate is on the cycle.
let mut on_walk = bitset(n);
Expand Down Expand Up @@ -472,6 +475,70 @@ impl WorkspaceGraph {
}
}

/// Inside a dependency cycle, edges stop ordering its packages: the `First` ones, then the `Second` ones.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum GivesWay {
/// `dependencies`
Never,
/// `peerDependencies`
Second,
/// `devDependencies` and `optionalDependencies`
First,
}

impl GivesWay {
pub fn of(behavior: Behavior) -> GivesWay {
if behavior.is_prod() {
GivesWay::Never
} else if behavior.is_peer() {
GivesWay::Second
} else {
GivesWay::First
}
}
}

/// Drops the edges that give way inside a dependency cycle. Returns what `strongly_connected_components` does for the rest.
pub fn cycle_groups(waits_for: &mut [Vec<(u32, GivesWay)>]) -> (Vec<u32>, Vec<Vec<usize>>) {
let groups = |waits_for: &[Vec<(u32, GivesWay)>]| {
let targets = |list: &Vec<(u32, GivesWay)>| list.iter().map(|&(to, _)| to).collect();
strongly_connected_components(&waits_for.iter().map(targets).collect::<Vec<Vec<u32>>>())
};
for gives_way in [GivesWay::First, GivesWay::Second] {
let (group_of, _) = groups(waits_for);
for (from, list) in waits_for.iter_mut().enumerate() {
list.retain(|&(to, kind)| kind != gives_way || group_of[to as usize] != group_of[from]);
}
}
groups(waits_for)
}

/// Candidate indices, each after those it depends on. What `cycle_groups` leaves of a cycle stays together.
pub(crate) fn dependency_order(candidate_count: usize, edges: &[(u32, u32, GivesWay)]) -> Vec<u32> {
let all = edges.iter().map(|&(from, to, _)| (from, to));
let (order, stalled_at) = WorkspaceGraph::from_edges(candidate_count, all).order_or_stall();
if stalled_at.is_none() {
return order;
}
let mut waits_for: Vec<Vec<(u32, GivesWay)>> = vec![Vec::new(); candidate_count];
for &(from, to, gives_way) in edges {
waits_for[from as usize].push((to, gives_way));
}
let (group_of, _) = cycle_groups(&mut waits_for);
let kept = waits_for
.iter()
.enumerate()
.flat_map(|(from, list)| list.iter().map(move |&(to, _)| (from as u32, to)));
let (mut order, stalled_at) =
WorkspaceGraph::from_edges(candidate_count, kept).order_or_stall();
if let Some(from) = stalled_at {
index_sort::sort_indices(&mut order[from..], &mut |a, b| {
group_of[a as usize].cmp(&group_of[b as usize])
});
}
order
}

pub fn select_lockfile_workspaces(
lockfile: &Lockfile,
patterns: &[&[u8]],
Expand Down
5 changes: 3 additions & 2 deletions src/resolver/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,10 +857,11 @@ impl PackageJSON {
// If a name is in several groups, the last group wins, as it does in `bun install`.
let dependency_groups: &[DependencyGroup] =
if INCLUDE_DEPENDENCIES == IncludeDependencies::Main {
// `bun run --filter` orders scripts by these, so a name that `dependencies` lists is one of those.
&[
DependencyGroup::DEPENDENCIES,
DependencyGroup::DEV,
DependencyGroup::OPTIONAL,
DependencyGroup::DEV,
DependencyGroup::DEPENDENCIES,
]
} else if !strings::contains(package_json_path, NODE_MODULES_PATH.as_bytes()) {
// One of the project's own package.json files. A package.json in the cache
Expand Down
Loading
Loading