Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 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: 1 addition & 1 deletion docs/pm/cli/update.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Updated packages appear in the install summary as `↑ name old → new`, with `
- A transitive dependency that shares the copy your own `package.json` entry resolves to (in the root or in a workspace) stays on that copy and moves with it. With `@types/node: ~20` in your `package.json`, the `@types/node: *` that `@types/ws` declares keeps using your `~20` copy instead of nesting the newest major under `@types/ws`. Bun updates a transitive range on its own when it rejects the version your entry moves to, or when you removed the entry.
- Bun does not add a version that another one covers. Two dependencies declaring `foo@^1` and `foo@*` share one `1.x` copy, as on `bun install`. Versions already in `bun.lock` and your own `package.json` entries are never moved to save a copy.
- Versions in `patchedDependencies` stay put as long as their range allows. Bun reports them as `kept name@version (patched, v1.2.3 available)`. `--latest` and [`bun audit fix`](/pm/cli/audit#bun-audit-fix) do move them; re-create the patch with [`bun patch`](/pm/cli/patch) afterwards.
- If a registry request for a transitive package fails, that package keeps its locked version and Bun prints a warning. A failed request for a direct dependency is an error.
- If a registry request for a transitive package fails, that package keeps its locked version and Bun prints a warning. So does an entry of your `optionalDependencies` that is a version range, when that range allows the locked version. A failed request for a required entry, or for an entry that a name, a pattern, `--dev`, `--prod` or `--depth 0` selects, is an error.

## `--interactive`

Expand Down
30 changes: 30 additions & 0 deletions src/install/PackageManager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,28 @@ impl Default for ManifestFailure {
}
}

/// An optional row that got no answer from the registry and took a package of bun.lock (`enqueue::keep_locked`); `bun update` has not checked that package.
pub(crate) struct KeptLocked {
/// Both by their ids before `clean_lockfile`.
pub(crate) row: DependencyID,
pub(crate) package: PackageID,
/// The manifest request that failed. `None`: `--offline` sent none.
pub(crate) task: Option<Task::Id>,
/// Finds the package.json entry of the row after `clean_lockfile`: its workspace, as `PackageManager::workspace_name_hash` names it, and its key. `None`: a row of an installed package.
pub(crate) entry: Option<(Option<PackageNameHash>, PackageNameHash)>,
}

impl KeptLocked {
/// Whether one of `kept` is the entry `key` in the package.json of `workspace`.
pub(crate) fn has_entry(
kept: &[KeptLocked],
workspace: Option<PackageNameHash>,
key: PackageNameHash,
) -> bool {
kept.iter().any(|kept| kept.entry == Some((workspace, key)))
}
}

/// How far along the messages of a `ManifestFailure` are in being reported.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum FailureReport {
Expand Down Expand Up @@ -615,6 +637,11 @@ pub struct PackageManager {
// 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>,
pub(crate) kept_locked: Vec<KeptLocked>,
// bun update: the packages that a "was not checked for updates" warning names, by their ids before `clean_lockfile`.
pub(crate) warned_unchecked: Vec<PackageID>,
// The root and workspace rows as bun.lock has them, taken before the differ binds them anew.
pub(crate) direct_before: crate::update_transitive::DirectDependencies,

// `Some` while `bun install` resolves. A "no such package" or "no such version" message about a
// dependency that dedupe may still drop is kept here until `log_held` decides whether to log
Expand Down Expand Up @@ -1726,6 +1753,9 @@ fn allocate_package_manager(
wr!(named_update_reachable, None);
wr!(kept_patched, Vec::new());
wr!(kept_patched_text, Vec::new());
wr!(kept_locked, Vec::new());
wr!(warned_unchecked, Vec::new());
wr!(direct_before, Default::default());
wr!(held_messages, None);
wr!(manifest_failures, HashMap::default());
wr!(look_ahead, LookAhead::default());
Expand Down
37 changes: 28 additions & 9 deletions src/install/PackageManager/PackageJSONEditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ use crate::bun_fs::FileSystem;
use bun_install::dependency::{self, DependencyExt as _, TagExt as _};
use bun_install::lockfile::CatalogMap;
use bun_install::lockfile::package::PackageColumns as _;
use bun_install::{Dependency, INVALID_PACKAGE_ID, Lockfile, resolution};
use bun_install::{Dependency, DependencyID, INVALID_PACKAGE_ID, Lockfile, resolution};
use bun_install_types::{DependencyGroup, PackageNameHash};

use super::package_manager_options::{Do, Enable};
use super::workspace_package_json_cache::{GetJSONOptions, GetResult};
use super::{CatalogUpdateInfo, PackageManager, PackageUpdateInfo, Subcommand, UpdateRequest};
use super::{
CatalogUpdateInfo, KeptLocked, PackageManager, PackageUpdateInfo, Subcommand, UpdateRequest,
};

type ExprDisabler = bun_ast::expr::Disabler;

Expand Down Expand Up @@ -326,6 +328,7 @@ pub(crate) fn edit_update_no_args(
) -> Result<bool, bun_alloc::AllocError> {
edit_update_entries(
&manager.lockfile,
&manager.kept_locked,
&manager.ast_arena,
&mut manager.updating_packages,
manager.workspace_name_hash,
Expand All @@ -350,6 +353,7 @@ fn edit_update_aliases_of_requests(
};
edit_update_entries(
&manager.lockfile,
&manager.kept_locked,
&manager.ast_arena,
&mut manager.updating_packages,
manager.workspace_name_hash,
Expand Down Expand Up @@ -381,6 +385,7 @@ pub(crate) fn string_pair(property: &G::Property) -> Option<(&[u8], &[u8])> {
/// Split borrows, for callers holding a `&mut` to another `PackageManager` field.
pub(crate) fn edit_update_entries(
lockfile: &crate::Lockfile,
kept_locked: &[KeptLocked],
arena: &bun_alloc::Arena,
updating_packages: &mut StringArrayHashMap<PackageUpdateInfo>,
workspace_name_hash: Option<PackageNameHash>,
Expand Down Expand Up @@ -462,17 +467,21 @@ pub(crate) fn edit_update_entries(
continue;
}
entry.written_back = true;
let Some((workspace_dep, resolution)) = (workspace_deps.iter())
.zip(workspace_resolution_ids)
.filter(|&(_, &package_id)| package_id != INVALID_PACKAGE_ID)
.map(|(row, &package_id)| (row, &resolutions[package_id as usize]))
.find(|(workspace_dep, resolution)| {
resolution.tag == resolution::Tag::Npm
let Some((workspace_dep, package_id)) = (workspace_deps.iter())
.zip(workspace_resolution_ids.iter().copied())
.find(|&(workspace_dep, package_id)| {
package_id != INVALID_PACKAGE_ID
&& resolutions[package_id as usize].tag == resolution::Tag::Npm
&& strings::eql_long(workspace_dep.name.slice(string_buf), key_str, true)
})
else {
continue;
};
let resolution = &resolutions[package_id as usize];
// The registry gave no answer for it: the entry stays as written.
if KeptLocked::has_entry(kept_locked, workspace_name_hash, workspace_dep.name_hash) {
continue;
}

// an exact pin is not moved by a plain `bun update`
if !update_to_latest
Expand Down Expand Up @@ -753,6 +762,7 @@ pub(crate) fn edit_catalogs_after_update(
let index = CatalogInfoIndex::init(&infos)?;
resolve_catalog_literals(
&manager.lockfile,
&manager.kept_locked,
&mut infos,
&index,
manager.options.do_.contains(Do::UPDATE_TO_LATEST),
Expand Down Expand Up @@ -827,6 +837,7 @@ impl CatalogInfoIndex {

fn resolve_catalog_literals(
lockfile: &Lockfile,
kept_locked: &[KeptLocked],
infos: &mut [CatalogUpdateInfo],
by_name: &CatalogInfoIndex,
update_to_latest: bool,
Expand All @@ -839,11 +850,12 @@ fn resolve_catalog_literals(
lockfile.buffers.dependencies.len(),
lockfile.buffers.resolutions.len()
);
for (dep, &package_id) in lockfile
for (row, (dep, &package_id)) in lockfile
.buffers
.dependencies
.iter()
.zip(lockfile.buffers.resolutions.iter())
.enumerate()
{
if dep.version.tag != dependency::Tag::Catalog || package_id == INVALID_PACKAGE_ID {
continue;
Expand All @@ -861,6 +873,13 @@ fn resolve_catalog_literals(
if resolution.tag != resolution::Tag::Npm {
continue;
}
// The registry gave no answer for it: its entry stays as written.
if !kept_locked.is_empty()
&& let Some((_, workspace)) = lockfile.declaring_workspace(row as DependencyID)
&& KeptLocked::has_entry(kept_locked, workspace, dep.name_hash)
{
continue;
}

if !update_to_latest {
// plain `bun update` does not move an exact pin (matches direct-dep behavior)
Expand Down
Loading
Loading