Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions docs/pm/cli/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ bun install --offline

An install is only certain to cache the packages that it places in `node_modules`. To warm the cache for an `--offline` install, run an install on the same platform that places at least the same packages. An install with `--production` or `--omit` does not cache the groups it leaves out.

`--offline` skips an optional dependency that is not cached, and anything below it that is not cached either. A package that an installed package requires is still an error.

See [lockfile](/pm/lockfile) for more on `bun.lock`.

---
Expand Down
2 changes: 1 addition & 1 deletion src/install/NetworkTask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ pub enum ForTarballError {
OutOfMemory,
#[error("InvalidURL")]
InvalidURL,
/// `--offline` and the tarball is not in the cache. Already reported (once per
/// `--offline` and the tarball is not in the cache. Reported or recorded already (once per
/// package); callers treat it like `AlreadyFailed`.
#[error("TarballFailedToDownload")]
Offline,
Expand Down
1 change: 1 addition & 0 deletions src/install/PackageInstaller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,7 @@ impl<'a> PackageInstaller<'a> {
if package_manager::enqueue_git_for_checkout(
self.manager_mut(),
dependency_id,
package_id,
alias.slice(string_buf!()),
resolution,
context,
Expand Down
5 changes: 5 additions & 0 deletions src/install/PackageManager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ pub struct PackageManager {
// `bun update <name>`: packages reachable from the workspaces in scope, see update_scope::plan_named.
pub(crate) named_update_reachable: Option<bun_collections::DynamicBitSet>,

// --offline: what the install phase missed in the cache; drained by enqueue::report_offline_misses.
pub(crate) offline_misses: Vec<enqueue::OfflineMiss>,

// bun update: patched packages a move was held back for; drained by update_transitive::print_kept_patched.
pub(crate) kept_patched: Vec<PackageID>,
pub kept_patched_text: Vec<u8>,
Expand Down Expand Up @@ -2139,6 +2142,7 @@ pub fn init(
wr!(updating_catalogs, Vec::new());
wr!(update_target_workspaces, None);
wr!(named_update_reachable, None);
wr!(offline_misses, Vec::new());
wr!(kept_patched, Vec::new());
wr!(kept_patched_text, Vec::new());
wr!(dedupe_report, None);
Expand Down Expand Up @@ -2607,6 +2611,7 @@ fn init_with_runtime_once(
wr!(updating_catalogs, Vec::new());
wr!(update_target_workspaces, None);
wr!(named_update_reachable, None);
wr!(offline_misses, Vec::new());
wr!(kept_patched, Vec::new());
wr!(kept_patched_text, Vec::new());
wr!(dedupe_report, None);
Expand Down
209 changes: 167 additions & 42 deletions src/install/PackageManager/PackageManagerEnqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::ptr::NonNull;
use core::sync::atomic::Ordering;

use crate::bun_fs::FileSystem;
use bun_collections::DynamicBitSet;
use bun_core::{Output, UnwrapOrOom, fmt as bun_fmt};
use bun_core::{StringOrTinyString, strings};
use bun_paths as Path;
Expand All @@ -20,11 +21,12 @@ use crate::dependency;
use crate::dependency::{DependencyExt as _, TagExt as _, VersionExt as _};
use crate::lockfile::PackageIndexEntry;
use crate::lockfile::package::Package;
use crate::lockfile::tree::is_filtered_dependency_or_workspace;
use crate::lockfile_real as Lockfile;
use crate::package_manager_real::{
self, FailFn, PackageManager, SuccessFn, TaskCallbackList, determine_preinstall_state,
get_cache_directory, get_preinstall_state, get_temporary_directory, run_tasks,
set_preinstall_state,
self, FailFn, PackageManager, SuccessFn, TaskCallbackList, WorkspaceFilter,
determine_preinstall_state, get_cache_directory, get_preinstall_state, get_temporary_directory,
run_tasks, set_preinstall_state,
};
use crate::package_manager_task as Task;
use crate::patch_install::EnqueueAfterState;
Expand Down Expand Up @@ -158,17 +160,15 @@ pub fn enqueue_tarball_for_download(
if this.network_task_has_failed(task_id) {
return Err(EnqueueTarballForDownloadError::AlreadyFailed);
}
if this.options.offline == crate::package_manager_real::options::OfflineMode::Offline {
let is_required = this.lockfile.buffers.dependencies[dependency_id as usize]
.behavior
.is_required();
let name = this
.lockfile
.str(&this.lockfile.packages.get(package_id as usize).name)
.to_vec();
if offline_tarball_miss(this, task_id, &name, is_required) {
return Err(EnqueueTarballForDownloadError::Offline);
}
if offline_tarball_miss(
this,
task_id,
OfflineMiss {
package_id,
dependency_id,
},
) {
return Err(EnqueueTarballForDownloadError::Offline);
}
let task_queue = this.task_queue.get_or_put(task_id)?;
if !task_queue.found_existing {
Expand Down Expand Up @@ -257,13 +257,14 @@ pub enum GitEnqueueResult {
/// a task was queued (or joined an existing one); completion arrives via the task queue
Queued,
/// `--offline` and the repository is not cached: nothing was queued (already reported
/// if required); the caller must count the package as failed/skipped itself
/// if required, or recorded for `report_offline_misses`); the caller counts it as skipped
OfflineMiss,
}

pub fn enqueue_git_for_checkout(
this: &mut PackageManager,
dependency_id: DependencyID,
package_id: PackageID,
alias: &[u8],
resolution: &Resolution,
task_context: TaskCallbackContext,
Expand All @@ -289,7 +290,11 @@ pub fn enqueue_git_for_checkout(
let is_required = this.lockfile.buffers.dependencies[dependency_id as usize]
.behavior
.is_required();
if offline_git_miss(this, clone_id, alias, is_required) {
let miss = OfflineMiss {
package_id,
dependency_id,
};
if offline_git_miss(this, clone_id, alias, is_required, Some(miss)) {
return GitEnqueueResult::OfflineMiss;
}
}
Expand Down Expand Up @@ -342,40 +347,145 @@ pub fn enqueue_git_for_checkout(

/// Under `--offline`, an install-phase request for a package that is not in the cache
/// (these helpers are only reached after the cache lookup missed): report it once if
/// required, skip if optional, and never register a task nobody will complete.
fn offline_tarball_miss(
this: &mut PackageManager,
task_id: Task::Id,
name: &[u8],
is_required: bool,
) -> bool {
/// required (in `report_offline_misses`), skip if optional, never register a task nobody completes.
fn offline_tarball_miss(this: &mut PackageManager, task_id: Task::Id, miss: OfflineMiss) -> bool {
if this.options.offline != crate::package_manager_real::options::OfflineMode::Offline {
return false;
}
if is_required && !this.network_task_has_failed(task_id) {
let is_required = this.lockfile.buffers.dependencies[miss.dependency_id as usize]
.behavior
.is_required();
let first_required_miss = is_required && !this.network_task_has_failed(task_id);
if first_required_miss {
// reserve + mark failed so later dependents take the already-failed path
let _ = this.has_created_network_task(task_id, true);
this.mark_network_task_failed(task_id);
}
if !this.options.runtime_auto_install {
this.offline_misses.push(miss);
} else if first_required_miss {
// No install phase follows, so nothing would report a recorded miss.
log_offline_miss(this, miss);
}
true
}

/// A package that the install phase of an `--offline` install did not find in the cache.
#[derive(Clone, Copy)]
pub(crate) struct OfflineMiss {
// Not `resolutions[dependency_id]`: the isolated linker can bind a peer to another package.
pub(crate) package_id: PackageID,
pub(crate) dependency_id: DependencyID,
}

fn log_offline_miss(this: &PackageManager, miss: OfflineMiss) {
let lockfile = &this.lockfile;
let package = lockfile.packages.get(miss.package_id as usize);
if package.resolution.tag == ResolutionTag::Git {
let alias = lockfile.buffers.dependencies[miss.dependency_id as usize].name;
let _ = this.log_mut().add_error_fmt(
None,
bun_ast::Loc::EMPTY,
format_args!(
"--offline: git repository for \"{}\" is not in the cache",
bstr::BStr::new(lockfile.str(&alias))
),
);
} else {
let _ = this.log_mut().add_error_fmt(
None,
bun_ast::Loc::EMPTY,
format_args!(
"--offline: \"{}\" is not in the cache",
bstr::BStr::new(name)
bstr::BStr::new(lockfile.str(&package.name))
),
);
}
true
}

/// Reports the recorded `--offline` misses that an installed package requires, and counts them.
pub(crate) fn report_offline_misses(
this: &mut PackageManager,
workspace_filters: &[WorkspaceFilter],
install_root_dependencies: bool,
packages_to_install: Option<&[PackageID]>,
) -> u32 {
let misses = core::mem::take(&mut this.offline_misses);
if misses.is_empty() {
return 0;
}
let this: &PackageManager = this;
let lockfile: &Lockfile::Lockfile = &this.lockfile;
let dependencies = lockfile.buffers.dependencies.as_slice();
let resolutions = lockfile.buffers.resolutions.as_slice();

let package_count = lockfile.packages.len();
let mut missed = DynamicBitSet::init_empty(package_count).unwrap_or_oom();
for miss in &misses {
missed.set(miss.package_id as usize);
}

// Stop at a missed package: it is not installed, so nothing is required through it.
let mut required = DynamicBitSet::init_empty(package_count).unwrap_or_oom();
let mut visited = DynamicBitSet::init_empty(package_count).unwrap_or_oom();
let package_dependencies = lockfile.packages.items_dependencies();
let package_metas = lockfile.packages.items_meta();
let mut stack: Vec<PackageID> = vec![0];
visited.set(0);
while let Some(parent_id) = stack.pop() {
let slice = package_dependencies[parent_id as usize];
for dependency_id in slice.begin()..slice.end() {
let package_id = resolutions[dependency_id as usize];
if package_id as usize >= package_count
// `is_filtered_dependency_or_workspace` tests this too, and prints it with --verbose.
|| package_metas[package_id as usize].is_disabled(this.options.cpu, this.options.os)
|| is_filtered_dependency_or_workspace(
dependency_id,
parent_id,
workspace_filters,
install_root_dependencies,
this,
lockfile,
resolutions,
)
|| (parent_id == 0
&& packages_to_install.is_some_and(|packages| !packages.contains(&package_id)))
{
continue;
}
if missed.is_set(package_id as usize) {
let behavior = dependencies[dependency_id as usize].behavior;
if !behavior.is_optional() && !behavior.is_optional_peer() {
required.set(package_id as usize);
}
} else if !visited.is_set(package_id as usize) {
visited.set(package_id as usize);
stack.push(package_id);
}
}
}

let mut reported = 0;
for &miss in &misses {
if required.is_set(miss.package_id as usize) {
required.unset(miss.package_id as usize);
reported += 1;
log_offline_miss(this, miss);
}
}
reported
}

/// Under `--offline`, a git dependency whose clone is not already in the cache cannot
/// be installed: report it (once, and only if some edge requires it) instead of
/// spawning `git`. Returns true when the clone must not be enqueued.
/// `installing`: the install phase records its miss, and reports it later.
fn offline_git_miss(
this: &mut PackageManager,
clone_id: Task::Id,
name: &[u8],
is_required: bool,
installing: Option<OfflineMiss>,
) -> bool {
if this.options.offline != crate::package_manager_real::options::OfflineMode::Offline {
return false;
Expand All @@ -395,19 +505,24 @@ fn offline_git_miss(
if cached {
return false;
}
if let Some(miss) = installing {
this.offline_misses.push(miss);
}
if is_required {
if !this.network_task_has_failed(clone_id) {
// reserve + mark failed: reported once, later dependents see the failure
let _ = this.has_created_network_task(clone_id, true);
this.mark_network_task_failed(clone_id);
let _ = this.log_mut().add_error_fmt(
None,
bun_ast::Loc::EMPTY,
format_args!(
"--offline: git repository for \"{}\" is not in the cache",
bstr::BStr::new(name)
),
);
if installing.is_none() {
let _ = this.log_mut().add_error_fmt(
None,
bun_ast::Loc::EMPTY,
format_args!(
"--offline: git repository for \"{}\" is not in the cache",
bstr::BStr::new(name)
),
);
}
}
} else {
// let a later required edge on the same repository report it
Expand Down Expand Up @@ -466,13 +581,15 @@ pub fn enqueue_package_for_download(
if this.network_task_has_failed(task_id) {
return Err(EnqueuePackageForDownloadError::AlreadyFailed);
}
Comment thread
robobun marked this conversation as resolved.
Resolved
{
let is_required = this.lockfile.buffers.dependencies[dependency_id as usize]
.behavior
.is_required();
if offline_tarball_miss(this, task_id, name, is_required) {
return Err(EnqueuePackageForDownloadError::Offline);
}
if offline_tarball_miss(
this,
task_id,
OfflineMiss {
package_id,
dependency_id,
},
) {
return Err(EnqueuePackageForDownloadError::Offline);
}
let task_queue = this.task_queue.get_or_put(task_id)?;
if !task_queue.found_existing {
Expand Down Expand Up @@ -1500,7 +1617,13 @@ pub fn enqueue_dependency_with_main_and_success_fn(
if this.has_created_network_task(clone_id, dependency.behavior.is_required()) {
return Ok(());
}
if offline_git_miss(this, clone_id, alias, dependency.behavior.is_required()) {
if offline_git_miss(
this,
clone_id,
alias,
dependency.behavior.is_required(),
None,
) {
return Ok(());
}

Expand Down Expand Up @@ -3235,6 +3358,7 @@ impl PackageManager {
pub(crate) fn enqueue_git_for_checkout(
&mut self,
dependency_id: DependencyID,
package_id: PackageID,
alias: &[u8],
resolution: &Resolution,
task_context: TaskCallbackContext,
Expand All @@ -3243,6 +3367,7 @@ impl PackageManager {
enqueue_git_for_checkout(
self,
dependency_id,
package_id,
alias,
resolution,
task_context,
Expand Down
7 changes: 7 additions & 0 deletions src/install/hoisted_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,5 +618,12 @@ pub(crate) fn install_hoisted_packages(
}
}

package_manager::enqueue::report_offline_misses(
this,
workspace_filters,
install_root_dependencies,
packages_to_install,
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Ok(summary)
}
Loading
Loading