-
Notifications
You must be signed in to change notification settings - Fork 5k
install: remove the node_modules links of a renamed or removed workspace #38511
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
5
commits into
main
Choose a base branch
from
farm/dd93f2b8/remove-dropped-workspace-links
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.
+264
−0
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fafc064
install: remove the node_modules links of a renamed or removed workspace
robobun 644693c
ci: retrigger
robobun a02fc5d
install: shorten the comments in remove_stale_workspace_links
robobun 1d58928
install: one-line comments in remove_stale_workspace_links
robobun 2d81856
Merge branch 'main' into farm/dd93f2b8/remove-dropped-workspace-links
dylan-conway 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
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
86 changes: 86 additions & 0 deletions
86
src/install/PackageManager/remove_stale_workspace_links.rs
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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| //! Neither linker deletes anything from `node_modules`; each only creates or | ||
| //! repoints the links the current lockfile asks for. The links of a workspace | ||
| //! whose name left the lockfile (renamed, or removed from the project) are | ||
| //! unlinked here instead, before the linker runs, so a name the new lockfile | ||
| //! still places is simply linked again. | ||
|
robobun marked this conversation as resolved.
Outdated
|
||
|
|
||
| use bstr::BStr; | ||
| use bun_paths::AutoAbsPathChecked; | ||
|
|
||
| use crate::lockfile::Lockfile; | ||
| use crate::lockfile::package::PackageColumns as _; | ||
| use crate::package_installer::alias_is_safe_install_target; | ||
| use crate::package_manager_real::PackageManager; | ||
| use crate::resolution::Tag as ResolutionTag; | ||
|
|
||
| pub(crate) fn remove_stale_workspace_links(previous: &Lockfile, current: &Lockfile) { | ||
| let previous_string_buf = previous.buffers.string_bytes.as_slice(); | ||
| let previous_packages = previous.packages.slice(); | ||
| let resolutions = previous_packages.items_resolution(); | ||
| let name_hashes = previous_packages.items_name_hash(); | ||
| let names = previous_packages.items_name(); | ||
|
|
||
| for pkg_id in 0..previous_packages.len() { | ||
| if resolutions[pkg_id].tag != ResolutionTag::Workspace | ||
| || current.workspace_paths.contains_key(&name_hashes[pkg_id]) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| let name = names[pkg_id].slice(previous_string_buf); | ||
| if !alias_is_safe_install_target(name) { | ||
| continue; | ||
| } | ||
|
|
||
| remove_links_named(current, name); | ||
| } | ||
| } | ||
|
|
||
| #[cold] | ||
| #[inline(never)] | ||
| fn remove_links_named(current: &Lockfile, name: &[u8]) { | ||
| let current_string_buf = current.buffers.string_bytes.as_slice(); | ||
|
|
||
| let mut path = AutoAbsPathChecked::init_top_level_dir(); | ||
| let top_level_len = path.len(); | ||
|
|
||
| remove_link(&mut path, b"", name); | ||
| for workspace_path in current.workspace_paths.values() { | ||
| path.set_length(top_level_len); | ||
| remove_link(&mut path, workspace_path.slice(current_string_buf), name); | ||
| } | ||
| } | ||
|
|
||
| /// Unlinks `<package_dir>/node_modules/<name>`. `readlink` doubles as the | ||
| /// symlink/junction check: a real directory or file there is not ours to remove. | ||
|
robobun marked this conversation as resolved.
Outdated
|
||
| fn remove_link(path: &mut AutoAbsPathChecked, package_dir: &[u8], name: &[u8]) { | ||
| if path.append(package_dir).is_err() | ||
| || path.append(b"node_modules").is_err() | ||
| || path.append(name).is_err() | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| let mut link_target = bun_paths::path_buffer_pool::get(); | ||
| if bun_sys::readlink(path.slice_z(), &mut link_target).is_err() { | ||
| return; | ||
| } | ||
|
|
||
| bun_output::scoped_log!( | ||
| PackageManager, | ||
| "removing stale workspace link {}", | ||
| BStr::new(path.slice()) | ||
| ); | ||
|
|
||
| #[cfg(windows)] | ||
| { | ||
| // Directory symlinks and junctions need rmdir; a file symlink needs unlink. | ||
| if bun_sys::rmdir(path.slice_z()).is_err() { | ||
| let _ = bun_sys::unlink(path.slice_z()); | ||
| } | ||
| } | ||
| #[cfg(not(windows))] | ||
| { | ||
| let _ = bun_sys::unlink(path.slice_z()); | ||
| } | ||
| } | ||
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
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.