-
Notifications
You must be signed in to change notification settings - Fork 5k
install: fix npm dependencies that share a workspace member's name #37248
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
Open
robobun
wants to merge
12
commits into
main
Choose a base branch
from
farm/bd2f1c53/workspace-npm-dep-collision
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d865b7b
install: fix npm dependencies that share a workspace member's name
robobun cc46b2f
[autofix.ci] apply automated fixes
autofix-ci[bot] 5cbe12c
Trim comments
robobun 5b761b0
install: keep displaced-member lockfiles loadable under config flips …
robobun e7e42c8
Tighten comments
robobun cae2677
[autofix.ci] apply automated fixes
autofix-ci[bot] ceac07f
Re-run checks
robobun 11240cd
Drain stdout in the duplicate-keys test; decouple the wildcard guard …
robobun b82cf86
Resolve a displaced member's dependencies by walking its placement, n…
robobun bda08e9
Add coverage for a displaced member's dependency hoisted to an interm…
robobun 0ce78f6
Harden displaced-member resolution per review
robobun a2a3a2f
Fix clippy lints; add scoped displaced-member coverage
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1604,6 +1604,10 @@ | |
| ) -> Result<(), ParseError> { | ||
| lockfile.init_empty(); | ||
|
|
||
| let link_workspace_packages = manager | ||
| .as_deref() | ||
| .is_none_or(|m| m.options.link_workspace_packages); | ||
|
|
||
| let Some(lockfile_version_expr) = root.get(b"lockfileVersion") else { | ||
| log.add_error(Some(source), root.loc, b"Missing lockfile version"); | ||
| return Err(ParseError::InvalidLockfileVersion); | ||
|
|
@@ -2091,6 +2095,7 @@ | |
| None | ||
| }; | ||
|
|
||
| let root_pkgs_expr = root.get(b"packages"); | ||
| let (off, len) = parse_append_dependencies::<false, true>( | ||
| lockfile, | ||
| &root_pkg_exr, | ||
|
|
@@ -2100,6 +2105,8 @@ | |
| None, | ||
| None, | ||
| Some(&workspaces_obj), | ||
| root_pkgs_expr.as_ref(), | ||
| link_workspace_packages, | ||
| )?; | ||
|
|
||
| let mut root_pkg = Package::default(); | ||
|
|
@@ -2124,8 +2131,13 @@ | |
| let workspace_pkgs_off: u32 = 1; | ||
| let mut workspace_pkgs_len: u32 = 0; | ||
|
|
||
| // Workspace-name duplicate detection, decoupled from the conditional | ||
| // `pkg_map` claim below so it fires even for displaced members. | ||
|
robobun marked this conversation as resolved.
|
||
| let mut seen_workspace_names: PkgPathSet = PkgPathSet::init(); | ||
|
|
||
| if lockfile_version != Version::V0 { | ||
| // these are the `workspaceOnly` packages | ||
| let pkgs_expr_for_claims = root.get(b"packages"); | ||
| // snapshot the workspace-path handles up front so the loop | ||
| // body can take `&mut *lockfile` (`parse_append_dependencies`, | ||
| // `append_package_dedupe`) without conflicting with the | ||
|
|
@@ -2156,6 +2168,16 @@ | |
| .expect("infallible: is_string checked"); | ||
| let name_hash = StringBuilder::string_hash(name); | ||
|
|
||
| if seen_workspace_names.contains(name) { | ||
| log.add_error_fmt( | ||
| source, | ||
| row.key_loc, | ||
| format_args!("Duplicate workspace name: '{}'", bstr::BStr::new(name)), | ||
| ); | ||
| return Err(ParseError::InvalidWorkspaceObject); | ||
| } | ||
| seen_workspace_names.put(name, ()); | ||
|
|
||
| pkg.name = sbuf!(lockfile).append_with_hash(name, name_hash)?; | ||
| pkg.name_hash = name_hash; | ||
|
|
||
|
|
@@ -2168,6 +2190,8 @@ | |
| None, | ||
| None, | ||
| None, | ||
| None, | ||
| link_workspace_packages, | ||
| )?; | ||
|
|
||
| pkg.dependencies = DependencySlice::new(off, len); | ||
|
|
@@ -2185,21 +2209,24 @@ | |
| } | ||
|
|
||
| // there should be no duplicates | ||
| let pkgs_len_before = lockfile.packages.len(); | ||
| let pkg_id = lockfile.append_package_dedupe(&mut pkg)?; | ||
|
|
||
| let entry = pkg_map.get_or_put(name)?; | ||
| if entry.found_existing { | ||
| log.add_error_fmt( | ||
| source, | ||
| row.key_loc, | ||
| format_args!("Duplicate workspace name: '{}'", bstr::BStr::new(name)), | ||
| ); | ||
| return Err(ParseError::InvalidWorkspaceObject); | ||
| // A member displaced by a root dependency only appears | ||
| // nested; pre-claiming its name would falsely collide with | ||
| // the root `packages` key the other package owns. | ||
|
robobun marked this conversation as resolved.
|
||
| if member_owns_packages_key(pkgs_expr_for_claims.as_ref(), name, path) { | ||
| let entry = pkg_map.get_or_put(name)?; | ||
| debug_assert!(!entry.found_existing, "caught by seen_workspace_names"); | ||
| *entry.value_ptr = pkg_id; | ||
| } | ||
|
|
||
| *entry.value_ptr = pkg_id; | ||
|
|
||
| workspace_pkgs_len += 1; | ||
| // `workspace_pkgs_len` sizes the 1..1+len package-id range | ||
| // resolved below; it must count appended packages, not loop | ||
| // iterations. | ||
|
robobun marked this conversation as resolved.
|
||
| if lockfile.packages.len() > pkgs_len_before { | ||
| workspace_pkgs_len += 1; | ||
| } | ||
| continue 'workspaces; | ||
| } | ||
| } | ||
|
|
@@ -2263,6 +2290,11 @@ | |
| bundled_pkgs.put(pkg_path, ()); | ||
| } | ||
|
|
||
| // Workspace packages claimed under a `packages` key other than their | ||
| // name (nested under a dependent, or aliased). Their own dependency | ||
| // keys are written under that placement, e.g. "beta/member/dep". | ||
|
robobun marked this conversation as resolved.
|
||
| let mut member_tree_keys: Vec<(PackageID, &[u8])> = Vec::new(); | ||
|
|
||
| 'next_pkg_key: for row in object_rows(&pkgs_expr) { | ||
| let key_loc = row.key_loc; | ||
| let pkg_path = row.key.slice(); | ||
|
|
@@ -2454,6 +2486,7 @@ | |
| // "another-pkg1": "workspaces:packages/pkg1", | ||
| // }, | ||
| *entry.value_ptr = workspace_pkg_id; | ||
| member_tree_keys.push((workspace_pkg_id, pkg_path)); | ||
| continue 'next_pkg_key; | ||
| } | ||
| } | ||
|
|
@@ -2521,6 +2554,8 @@ | |
| Some(pkg_path), | ||
| Some(&bundled_pkgs), | ||
| None, | ||
| None, | ||
| link_workspace_packages, | ||
| )?; | ||
|
|
||
| pkg.dependencies = DependencySlice::new(off, len); | ||
|
|
@@ -2847,12 +2882,45 @@ | |
|
|
||
| seen_deps.clear_retaining_capacity(); | ||
|
|
||
| // `"<name>/<dep>"` keys belong to whichever package owns the | ||
| // root `<name>` entry; for a displaced member that is the | ||
| // npm package that replaced it. | ||
|
robobun marked this conversation as resolved.
|
||
| let owns_name_node = pkg_map.get(workspace_name).copied() == Some(pkg_id); | ||
|
|
||
| let deps = pkg_deps[pkg_id as usize]; | ||
| for _dep_id in deps.begin()..deps.end() { | ||
| let dep_id: DependencyID = _dep_id; | ||
| let dep = &mut dependencies[dep_id as usize]; | ||
| let dep_name = dep.name.slice(string_buf); | ||
|
|
||
| // A displaced member's node lives under its recorded | ||
| // `packages` key (e.g. "beta/member"). Walk that | ||
| // placement up like hoisting would: "beta/member/dep", | ||
| // then "beta/dep"; the bare "dep" probe below covers the | ||
| // root level. | ||
|
robobun marked this conversation as resolved.
|
||
| let nested_res_id = member_tree_keys.iter().find_map(|&(id, key)| { | ||
| if id != pkg_id { | ||
| return None; | ||
| } | ||
| let mut prefix = key; | ||
| loop { | ||
| let needed = prefix.len() + 1 + dep_name.len(); | ||
| let buf_slice = &mut path_buf[..]; | ||
| if needed <= buf_slice.len() { | ||
| buf_slice[..prefix.len()].copy_from_slice(prefix); | ||
| buf_slice[prefix.len()] = b'/'; | ||
| buf_slice[prefix.len() + 1..needed].copy_from_slice(dep_name); | ||
| if let Some(&found) = pkg_map.get(&buf_slice[..needed]) { | ||
| return Some(found); | ||
| } | ||
| } | ||
| match strings::last_index_of_char(prefix, b'/') { | ||
| Some(i) => prefix = &prefix[..i as usize], | ||
|
Check failure on line 2918 in src/install/lockfile/bun.lock.rs
|
||
|
claude[bot] marked this conversation as resolved.
Outdated
|
||
| None => return None, | ||
| } | ||
| } | ||
| }); | ||
|
claude[bot] marked this conversation as resolved.
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| let workspace_node_modules = { | ||
| let buf_slice = &mut path_buf[..]; | ||
| let needed = workspace_name.len() + 1 + dep_name.len(); | ||
|
|
@@ -2885,11 +2953,13 @@ | |
| } else { | ||
| None | ||
| }; | ||
| let Some(res_id) = peer_res_id.or_else(|| { | ||
| pkg_map | ||
| .get(workspace_node_modules) | ||
| .or_else(|| pkg_map.get(dep_name)) | ||
| .copied() | ||
| let Some(res_id) = peer_res_id.or(nested_res_id).or_else(|| { | ||
|
robobun marked this conversation as resolved.
|
||
| let by_name = if owns_name_node { | ||
| pkg_map.get(workspace_node_modules) | ||
| } else { | ||
| None | ||
| }; | ||
| by_name.or_else(|| pkg_map.get(dep_name)).copied() | ||
| }) else { | ||
| if dep.behavior.contains(Behavior::OPTIONAL) { | ||
| continue; | ||
|
|
@@ -3153,6 +3223,33 @@ | |
| } | ||
| } | ||
|
|
||
| /// A workspace member normally owns the root `packages` key matching its | ||
| /// name. When a root dependency replaced the member's workspace dependency | ||
| /// (`Package::parse_dependency`), that key holds the other package and the | ||
| /// member only appears nested, so its name must not pre-claim the key. | ||
|
robobun marked this conversation as resolved.
|
||
| fn member_owns_packages_key(pkgs_expr: Option<&Expr>, name: &[u8], path: &[u8]) -> bool { | ||
| let Some(pkgs) = pkgs_expr else { | ||
| return true; | ||
| }; | ||
| let Some(value) = pkgs.get(name) else { | ||
| return true; | ||
| }; | ||
| if !value.is_array() { | ||
| return true; | ||
| } | ||
| let Some(first) = array_items(&value).first().and_then(|item| item.as_str()) else { | ||
| return true; | ||
| }; | ||
| // a member's own entry is written as "<name>@workspace:<path>" | ||
| let Some(rest) = first.get(name.len() + 1..) else { | ||
| return false; | ||
| }; | ||
| strings::has_prefix(first, name) | ||
| && first[name.len()] == b'@' | ||
| && strings::has_prefix(rest, b"workspace:") | ||
| && strings::eql(&rest[b"workspace:".len()..], path) | ||
| } | ||
|
|
||
| fn dependency_resolution_failure( | ||
| dep: &Dependency, | ||
| pkg_path: Option<&[u8]>, | ||
|
|
@@ -3213,6 +3310,9 @@ | |
| pkg_path: Option<&[u8]>, | ||
| bundled_pkgs: Option<&PkgPathSet>, | ||
| workspaces_obj: Option<&Expr>, | ||
| // Only meaningful when `IS_ROOT`. | ||
| pkgs_expr: Option<&Expr>, | ||
| link_workspace_packages: bool, | ||
| ) -> Result<(u32, u32), ParseError> { | ||
| // Clearing on entry is equivalent to clearing on every exit path for all | ||
| // callers (none read the buf between calls) and also covers early-error exits. | ||
|
|
@@ -3362,6 +3462,37 @@ | |
| .expect("infallible: is_string checked"); | ||
| let name_hash = StringBuilder::string_hash(name); | ||
|
|
||
| // The key-ownership check honors the shape the lockfile was | ||
| // written in (a linkWorkspacePackages flip must re-resolve, | ||
| // not bind a workspace edge to the npm package); the range | ||
| // scan mirrors `Package::parse_dependency` for aliased | ||
| // dependencies, whose root key is the alias. | ||
|
robobun marked this conversation as resolved.
|
||
| let overridden = { | ||
| let bytes = lockfile.buffers.string_bytes.as_slice(); | ||
| !member_owns_packages_key(pkgs_expr, name, path) || { | ||
| let member_version = lockfile.workspace_versions.get(&name_hash).copied(); | ||
| lockfile.buffers.dependencies.as_slice()[off..] | ||
| .iter() | ||
| .any(|dep| { | ||
| if dep.version.tag != DependencyVersionTag::Npm { | ||
| return false; | ||
| } | ||
| let npm = dep.version.npm(); | ||
| if StringBuilder::string_hash(npm.name.slice(bytes)) != name_hash { | ||
| return false; | ||
| } | ||
| let satisfies = match member_version { | ||
| Some(version) => npm.version.satisfies(version, bytes, bytes), | ||
| None => npm.version.is_star(), | ||
| }; | ||
| !(link_workspace_packages && satisfies) | ||
| }) | ||
| } | ||
| }; | ||
| if overridden { | ||
| continue 'workspaces; | ||
| } | ||
|
|
||
| let dep = Dependency { | ||
| name: sbuf!(lockfile).append_with_hash(name, name_hash)?, | ||
| name_hash, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.