From 7d76db8752e7a28675d8c2aecb3eb7b3df83932e Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sat, 30 Nov 2024 05:05:31 -0800 Subject: [PATCH] Split these into separate functions --- src/install/install.zig | 62 +++++++++++----------------------------- src/install/lockfile.zig | 39 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 46 deletions(-) diff --git a/src/install/install.zig b/src/install/install.zig index aae420ee34..721162a38d 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -4013,6 +4013,7 @@ pub const PackageManager = struct { return this.pathForCachedNPMPath(buf, package_name, npm.version); }, + else => return "", } } @@ -4609,33 +4610,14 @@ pub const PackageManager = struct { switch (version.tag) { .npm, .dist_tag => { - resolve_from_workspace: { - if (version.tag == .npm) { - const workspace_path = if (this.lockfile.workspace_paths.count() > 0) this.lockfile.workspace_paths.get(name_hash) else null; - const workspace_version = this.lockfile.workspace_versions.get(name_hash); - const buf = this.lockfile.buffers.string_bytes.items; - if ((workspace_version != null and version.value.npm.version.satisfies(workspace_version.?, buf, buf)) or - - // https://github.com/oven-sh/bun/pull/10899#issuecomment-2099609419 - // if the workspace doesn't have a version, it can still be used if - // dependency version is wildcard - (workspace_path != null and version.value.npm.version.@"is *"())) - { - const root_package = this.lockfile.rootPackage() orelse break :resolve_from_workspace; - const root_dependencies = root_package.dependencies.get(this.lockfile.buffers.dependencies.items); - const root_resolutions = root_package.resolutions.get(this.lockfile.buffers.resolutions.items); - - for (root_dependencies, root_resolutions) |root_dep, workspace_package_id| { - if (workspace_package_id != invalid_package_id and root_dep.version.tag == .workspace and root_dep.name_hash == name_hash) { - // make sure verifyResolutions sees this resolution as a valid package id - successFn(this, dependency_id, workspace_package_id); - return .{ - .package = this.lockfile.packages.get(workspace_package_id), - .is_first_time = false, - }; - } - } - } + if (version.tag == .npm) { + if (this.lockfile.resolveWorkspaceFromNpmVersion(name_hash, &version)) |workspace_package_id| { + // make sure verifyResolutions sees this resolution as a valid package id + successFn(this, dependency_id, workspace_package_id); + return .{ + .package = this.lockfile.packages.get(workspace_package_id), + .is_first_time = false, + }; } } @@ -4652,26 +4634,14 @@ pub const PackageManager = struct { .npm => manifest.findBestVersion(version.value.npm.version, this.lockfile.buffers.string_bytes.items), else => unreachable, } orelse { - resolve_workspace_from_dist_tag: { - // choose a workspace for a dist_tag only if a version was not found - if (version.tag == .dist_tag) { - const workspace_path = if (this.lockfile.workspace_paths.count() > 0) this.lockfile.workspace_paths.get(name_hash) else null; - if (workspace_path != null) { - const root_package = this.lockfile.rootPackage() orelse break :resolve_workspace_from_dist_tag; - const root_dependencies = root_package.dependencies.get(this.lockfile.buffers.dependencies.items); - const root_resolutions = root_package.resolutions.get(this.lockfile.buffers.resolutions.items); - for (root_dependencies, root_resolutions) |root_dep, workspace_package_id| { - if (workspace_package_id != invalid_package_id and root_dep.version.tag == .workspace and root_dep.name_hash == name_hash) { - // make sure verifyResolutions sees this resolution as a valid package id - successFn(this, dependency_id, workspace_package_id); - return .{ - .package = this.lockfile.packages.get(workspace_package_id), - .is_first_time = false, - }; - } - } - } + // choose a workspace for a dist_tag only if a version was not found + if (version.tag == .dist_tag) { + if (this.lockfile.resolveWorkspaceFromNpmDistTag(name_hash)) |workspace_package_id| { + return .{ + .package = this.lockfile.packages.get(workspace_package_id), + .is_first_time = false, + }; } } diff --git a/src/install/lockfile.zig b/src/install/lockfile.zig index bf93c60932..8ee5d52d8b 100644 --- a/src/install/lockfile.zig +++ b/src/install/lockfile.zig @@ -1073,6 +1073,45 @@ pub fn cleanWithLogger( return new; } +pub fn resolveWorkspaceFromNpmVersion(this: *const Lockfile, name_hash: PackageNameHash, version: *const Dependency.Version) ?PackageID { + const workspace_path = if (this.workspace_paths.count() > 0) this.workspace_paths.get(name_hash) else null; + const workspace_version = this.workspace_versions.get(name_hash); + const buf = this.buffers.string_bytes.items; + if ((workspace_version != null and version.value.npm.version.satisfies(workspace_version.?, buf, buf)) or + // https://github.com/oven-sh/bun/pull/10899#issuecomment-2099609419 + // if the workspace doesn't have a version, it can still be used if + // dependency version is wildcard + (workspace_path != null and version.value.npm.version.@"is *"())) + { + const root_package = this.rootPackage() orelse return null; + const root_dependencies = root_package.dependencies.get(this.buffers.dependencies.items); + const root_resolutions = root_package.resolutions.get(this.buffers.resolutions.items); + + for (root_dependencies, root_resolutions) |root_dep, workspace_package_id| { + if (workspace_package_id != invalid_package_id and root_dep.version.tag == .workspace and root_dep.name_hash == name_hash) { + return workspace_package_id; + } + } + } + return null; +} + +pub fn resolveWorkspaceFromNpmDistTag(this: *const Lockfile, name_hash: PackageNameHash) ?PackageID { + const workspace_path = if (this.workspace_paths.count() > 0) this.workspace_paths.get(name_hash) else null; + if (workspace_path != null) { + const root_package = this.rootPackage() orelse return null; + const root_dependencies = root_package.dependencies.get(this.buffers.dependencies.items); + const root_resolutions = root_package.resolutions.get(this.buffers.resolutions.items); + + for (root_dependencies, root_resolutions) |root_dep, workspace_package_id| { + if (workspace_package_id != invalid_package_id and root_dep.version.tag == .workspace and root_dep.name_hash == name_hash) { + return workspace_package_id; + } + } + } + return null; +} + pub const MetaHashFormatter = struct { meta_hash: *const MetaHash,