Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/install/PackageManager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ type PreallocatedNetworkTasks = HiveArrayFallback<NetworkTask, 128>;
type ResolveTaskQueue = UnboundedQueue<Task::Task<'static> /* , .next */>;

type RepositoryMap = HashMap<Task::Id, Fd /* , IdentityContext<Task::Id>, 80 */>;
/// Resolve-task id (git checkout / tarball extract) -> the package that task
/// appended during the resolve phase. A task's callback queue is drained
/// exactly once, so a dependency enqueued after that drain must resolve
/// through this map instead of queueing a callback that nothing will ever
/// process.
Comment thread
robobun marked this conversation as resolved.
Resolved
type AppendedTaskPackageMap =
HashMap<Task::Id, PackageID /* , IdentityContext<Task::Id>, 80 */>;
pub(crate) type FolderResolutionMap =
HashMap<u64, FolderResolutionEntry /* , IdentityContext<u64>, 80 */>;
pub(crate) type NpmAliasMap =
Expand Down Expand Up @@ -329,6 +336,7 @@ pub struct PackageManager {
pub manifests: PackageManifestMap,
pub(crate) folders: FolderResolutionMap,
pub(crate) git_repositories: RepositoryMap,
pub(crate) appended_task_packages: AppendedTaskPackageMap,

pub(crate) network_dedupe_map: crate::network_task::DedupeMap,
pub(crate) async_network_task_queue: AsyncNetworkTaskQueue,
Expand Down Expand Up @@ -1914,6 +1922,7 @@ pub fn init(
wr!(manifests, PackageManifestMap::default());
wr!(folders, Default::default());
wr!(git_repositories, RepositoryMap::default());
wr!(appended_task_packages, AppendedTaskPackageMap::default());
wr!(network_dedupe_map, Default::default());
wr!(async_network_task_queue, AsyncNetworkTaskQueue::default());
wr!(network_tarball_batch, thread_pool::Batch::default());
Expand Down Expand Up @@ -2346,6 +2355,7 @@ fn init_with_runtime_once(
wr!(manifests, PackageManifestMap::default());
wr!(folders, Default::default());
wr!(git_repositories, RepositoryMap::default());
wr!(appended_task_packages, AppendedTaskPackageMap::default());
wr!(network_dedupe_map, Default::default());
wr!(async_network_task_queue, AsyncNetworkTaskQueue::default());
wr!(network_tarball_batch, thread_pool::Batch::default());
Expand Down
68 changes: 54 additions & 14 deletions src/install/PackageManager/PackageManagerEnqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,7 @@ pub fn enqueue_git_for_checkout(
}

let dep = this.lockfile.buffers.dependencies[dependency_id as usize].clone();
let task = enqueue_git_clone(
this,
clone_id,
alias,
&repository,
dependency_id,
&dep,
resolution,
None,
);
let task = enqueue_git_clone(this, clone_id, alias, &repository, &dep, resolution, None);
this.task_batch.push(ThreadPool::Batch::from(task));
}
}
Expand Down Expand Up @@ -619,6 +610,36 @@ pub unsafe fn enqueue_patch_task_pre(this: &mut PackageManager, task: *mut Patch
let _ = this.pending_pre_calc_hashes.fetch_add(1, Ordering::Relaxed);
}

/// A resolve task's callback queue is drained exactly once. If `task_id`
/// already completed and appended a package, resolve `id` against it directly:
/// a context queued now would never be processed. Applies the same
/// `package_name` / `resolved` fix-up the completion drain applies.
Comment thread
robobun marked this conversation as resolved.
Resolved
fn resolve_from_appended_task(
this: &mut PackageManager,
task_id: Task::Id,
id: DependencyID,
) -> Option<PackageID> {
let &pkg_id = this.appended_task_packages.get(&task_id)?;
let pkg_name = this.lockfile.packages.items_name()[pkg_id as usize];
let pkg_res = this.lockfile.packages.items_resolution()[pkg_id as usize];
let v = &mut this.lockfile.buffers.dependencies[id as usize].version;
// The buffer row can hold a different tag than the enqueue arm when the
// dependency comes from `overrides`.
Comment thread
robobun marked this conversation as resolved.
Resolved
match v.tag {
dependency::version::Tag::Git => {
let repo = v.git_mut();
if pkg_res.tag == ResolutionTag::Git {
repo.resolved = pkg_res.git().resolved;
}
repo.package_name = pkg_name;
}
dependency::version::Tag::Github => v.github_mut().package_name = pkg_name,
dependency::version::Tag::Tarball => v.tarball_mut().package_name = pkg_name,
_ => {}
}
Some(pkg_id)
}

/// Q: "What do we do with a dependency in a package.json?"
/// A: "We enqueue it!"
pub fn enqueue_dependency_with_main_and_success_fn(
Expand Down Expand Up @@ -1218,6 +1239,14 @@ pub fn enqueue_dependency_with_main_and_success_fn(

let needs_ctx =
this.lockfile.buffers.resolutions[id as usize] == invalid_package_id;

if needs_ctx {
if let Some(pkg_id) = resolve_from_appended_task(this, checkout_id, id) {
success_fn(this, id, pkg_id);
return Ok(());
}
}

let entry = this
.task_queue
.get_or_put_context(checkout_id, ())
Expand Down Expand Up @@ -1272,8 +1301,7 @@ pub fn enqueue_dependency_with_main_and_success_fn(
return Ok(());
}

let task =
enqueue_git_clone(this, clone_id, alias, &dep, id, dependency, &res, None);
let task = enqueue_git_clone(this, clone_id, alias, &dep, dependency, &res, None);
this.task_batch.push(ThreadPool::Batch::from(task));
}
Ok(())
Expand Down Expand Up @@ -1304,6 +1332,13 @@ pub fn enqueue_dependency_with_main_and_success_fn(
);
}

if this.lockfile.buffers.resolutions[id as usize] == invalid_package_id {
if let Some(pkg_id) = resolve_from_appended_task(this, task_id, id) {
success_fn(this, id, pkg_id);
return Ok(());
}
}

let ctx = if is_root {
TaskCallbackContext::RootDependency(id)
} else {
Expand Down Expand Up @@ -1502,6 +1537,13 @@ pub fn enqueue_dependency_with_main_and_success_fn(
);
}

if this.lockfile.buffers.resolutions[id as usize] == invalid_package_id {
if let Some(pkg_id) = resolve_from_appended_task(this, task_id, id) {
success_fn(this, id, pkg_id);
return Ok(());
}
}

let ctx = if is_root {
TaskCallbackContext::RootDependency(id)
} else {
Expand Down Expand Up @@ -1651,7 +1693,6 @@ fn enqueue_git_clone(
task_id: Task::Id,
name: &[u8],
repository: &Repository,
dep_id: DependencyID,
dependency: &Dependency,
res: &Resolution,
// if patched then we need to do apply step after network task is done
Expand Down Expand Up @@ -1700,7 +1741,6 @@ fn enqueue_git_clone(
)
.expect("unreachable"),
env: crate::repository::SharedEnv::get(this.env_mut()),
dep_id,
res: *res,
}),
},
Expand Down
121 changes: 66 additions & 55 deletions src/install/PackageManager/runTasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,9 @@ pub fn run_tasks<C: RunTasksCallbacks>(
task.data_extract(),
log_level,
) {
// Record the appended package so a later-enqueued dependency
// on this same tarball can resolve; see `AppendedTaskPackageMap`.
Comment thread
robobun marked this conversation as resolved.
Resolved
manager.appended_task_packages.insert(task.id, pkg.meta.id);
'handle_pkg: {
// In the middle of an install, you could end up needing to downlaod the github tarball for a dependency
// We need to make sure we resolve the dependencies first before calling the onExtract callback
Expand Down Expand Up @@ -1251,8 +1254,6 @@ pub fn run_tasks<C: RunTasksCallbacks>(
let name = clone.name.slice();
let url = clone.url.slice();

manager.git_repositories.insert(task.id, repo_fd);

if task.status == Task::Status::Fail {
let err = task.err.unwrap_or(crate::Error::Failed);

Expand Down Expand Up @@ -1331,69 +1332,76 @@ pub fn run_tasks<C: RunTasksCallbacks>(
continue;
}

manager.git_repositories.insert(task.id, repo_fd);

if C::HAS_ON_EXTRACT && C::IS_PACKAGE_INSTALLER {
// Installing!
// this dependency might be something other than a git dependency! only need the name and
// behavior, use the resolution from the task.
let dep_id = clone.dep_id;
// reshaped for borrowck — copy the small `String` handles
// + behavior bit so
// the `&manager.lockfile` borrow doesn't extend across the
// `&mut manager` calls (`has_created_network_task`,
// `enqueue_git_checkout`) below; detach the slice backing
// through `string_buf_ptr` (matching the
// `PackageManifest`-arm `name` detach pattern above).
let (dep_name_handle, is_required) = {
let dep = &manager.lockfile.buffers.dependencies[dep_id as usize];
(dep.name, dep.behavior.is_required())
};
// SAFETY: `clone.res.tag == Git` — git-clone tasks are only
// enqueued for git resolutions; `value.git` is the active arm.
let git = *clone.res.git();
// SAFETY: `string_bytes` lives as long as `manager.lockfile`
// and is not reallocated while resolve tasks are draining.
let string_buf = unsafe {
bun_ptr::detach_lifetime(manager.lockfile.buffers.string_bytes.as_slice())
// Installing! The clone task is shared by every dependency on
// this repo URL; enqueue a checkout per waiter, not just one.
Comment thread
robobun marked this conversation as resolved.
Resolved
let Some(waiters) = manager.task_queue.remove(&task.id) else {
continue;
};
let dep_name = dep_name_handle.slice(string_buf);
let committish = git.committish.slice(string_buf);
let repo = git.repo.slice(string_buf);
for waiter in waiters.iter() {
let dep_id = match waiter {
bun_install::TaskCallbackContext::Dependency(id) => *id,
_ => continue,
};
// reshaped for borrowck — copy the small `String` handles
// so the `&manager.lockfile` borrow doesn't extend across
// the `&mut manager` calls below.
Comment thread
robobun marked this conversation as resolved.
Resolved
let (dep_name_handle, is_required) = {
let dep = &manager.lockfile.buffers.dependencies[dep_id as usize];
(dep.name, dep.behavior.is_required())
};
let pkg_id = manager.lockfile.buffers.resolutions[dep_id as usize];
if pkg_id == INVALID_PACKAGE_ID {
continue;
}
let res = manager.lockfile.packages.items_resolution()[pkg_id as usize];
if res.tag != bun_install::ResolutionTag::Git {
continue;
}
// SAFETY: `res.tag == Git` checked just above —
// `value.git` is the active union arm.
let git = *res.git();
// SAFETY: `string_bytes` lives as long as `manager.lockfile`
// and is not reallocated while resolve tasks are draining.
let string_buf = unsafe {
bun_ptr::detach_lifetime(
manager.lockfile.buffers.string_bytes.as_slice(),
)
};
let dep_name = dep_name_handle.slice(string_buf);
let repo = git.repo.slice(string_buf);
// Use the lockfile's stored SHA: `enqueue_git_for_checkout`
// keyed the install-context entry on it, and a branch
// committish's current tip may differ.
Comment thread
robobun marked this conversation as resolved.
Resolved
let resolved = git.resolved.slice(string_buf);

use crate::repository_real::RepositoryExt as _;
let resolved = crate::repository_real::Repository::find_commit(
manager.env_mut(),
manager.log_mut(),
repo_fd,
dep_name,
committish,
task.id,
)?;
let checkout_id = Task::Id::for_git_checkout(repo, resolved);

let checkout_id = Task::Id::for_git_checkout(repo, &resolved);
if manager.has_created_network_task(checkout_id, is_required) {
continue;
}
Comment thread
robobun marked this conversation as resolved.
Resolved

if manager.has_created_network_task(checkout_id, is_required) {
continue;
// reshaped for borrowck — split nested `&mut manager`.
let queued = enqueue::enqueue_git_checkout(
manager,
checkout_id,
repo_fd,
dep_id,
dep_name,
&res,
resolved,
None,
);
manager.task_batch.push(ThreadPoolBatch::from(queued));
}

// reshaped for borrowck — split nested `&mut manager`.
let queued = enqueue::enqueue_git_checkout(
manager,
checkout_id,
repo_fd,
dep_id,
dep_name,
&clone.res,
&resolved,
None,
);
manager.task_batch.push(ThreadPoolBatch::from(queued));
} else {
// Resolving!
let dependency_list_entry = manager
let dependency_list = manager
.task_queue
.get_mut(&task.id)
.remove(&task.id)
.expect("infallible: task queued");
let dependency_list = core::mem::take(dependency_list_entry);

process_dependency_list_for_ctx::<C>(
manager,
Comment thread
robobun marked this conversation as resolved.
Resolved
Expand Down Expand Up @@ -1483,6 +1491,9 @@ pub fn run_tasks<C: RunTasksCallbacks>(
task.data_git_checkout(),
log_level,
) {
// Record the appended package so a later-enqueued dependency
// on this same repo+commit can resolve; see `AppendedTaskPackageMap`.
Comment thread
robobun marked this conversation as resolved.
Resolved
manager.appended_task_packages.insert(task.id, pkg.meta.id);
'handle_pkg: {
let any_root = Cell::new(false);
let dependency_list: TaskCallbackList = {
Expand Down
25 changes: 24 additions & 1 deletion src/install/PackageManagerTask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,30 @@ impl<'a> Task<'a> {
break 'body;
}
}
} else if this.status != Status::Fail {
// Neither matcher recognized the URL (`file://`,
// `git://`, ...); clone with the URL as written
// instead of finishing with zeroed task data.
Comment thread
robobun marked this conversation as resolved.
Resolved
match Repository::download(
req.env,
&mut this.log,
// SAFETY: see `manager` decl — short-lived `&mut` at call boundary.
unsafe { &mut *manager }.get_cache_directory(),
this.id,
name,
url,
attempt,
) {
Ok(d) => d,
Err(err) => {
this.err = Some(err);
this.status = Status::Fail;
this.data = Data {
git_clone: ManuallyDrop::new(Fd::invalid()),
};
break 'body;
}
}
} else {
break 'body;
}
Expand Down Expand Up @@ -654,7 +678,6 @@ pub struct GitCloneRequest {
// `Map` owns its storage; store a
// `&'static` into the global `Repository.shared_env` instead — see `SharedEnv::get`.
pub(crate) env: &'static dot_env::Map,
pub(crate) dep_id: DependencyID,
pub(crate) res: Resolution,
}

Expand Down
Loading
Loading