Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 0 deletions src/cli/link_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ fn link(ctx: Command.Context) !void {
.bin = package.bin,
.node_modules_path = &node_modules_path,
.global_bin_path = manager.options.bin_path,
.target_node_modules_path = &node_modules_path,
.target_package_name = strings.StringOrTinyString.init(name),

// .destination_dir_subpath = destination_dir_subpath,
.package_name = strings.StringOrTinyString.init(name),
Expand Down
2 changes: 2 additions & 0 deletions src/cli/unlink_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ fn unlink(ctx: Command.Context) !void {
defer node_modules_path.deinit();

var bin_linker = Bin.Linker{
.target_node_modules_path = &node_modules_path,
.target_package_name = strings.StringOrTinyString.init(name),
.bin = package.bin,
.node_modules_path = &node_modules_path,
.global_bin_path = manager.options.bin_path,
Expand Down
7 changes: 7 additions & 0 deletions src/env_var.zig
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ pub const feature_flag = struct {
pub const BUN_BE_BUN = newFeatureFlag("BUN_BE_BUN", .{});
pub const BUN_DEBUG_NO_DUMP = newFeatureFlag("BUN_DEBUG_NO_DUMP", .{});
pub const BUN_DESTRUCT_VM_ON_EXIT = newFeatureFlag("BUN_DESTRUCT_VM_ON_EXIT", .{});

/// Disable "nativeDependencies"
pub const BUN_FEATURE_FLAG_DISABLE_NATIVE_DEPENDENCY_LINKER = newFeatureFlag("BUN_FEATURE_FLAG_DISABLE_NATIVE_DEPENDENCY_LINKER", .{});

/// Disable "ignoreScripts" in package.json
pub const BUN_FEATURE_FLAG_DISABLE_IGNORE_SCRIPTS = newFeatureFlag("BUN_FEATURE_FLAG_DISABLE_IGNORE_SCRIPTS", .{});

pub const BUN_FEATURE_FLAG_DISABLE_ADDRCONFIG = newFeatureFlag("BUN_FEATURE_FLAG_DISABLE_ADDRCONFIG", .{});
pub const BUN_FEATURE_FLAG_DISABLE_ASYNC_TRANSPILER = newFeatureFlag("BUN_FEATURE_FLAG_DISABLE_ASYNC_TRANSPILER", .{});
pub const BUN_FEATURE_FLAG_DISABLE_DNS_CACHE = newFeatureFlag("BUN_FEATURE_FLAG_DISABLE_DNS_CACHE", .{});
Expand Down
176 changes: 118 additions & 58 deletions src/install/PackageInstaller.zig
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ pub const PackageInstaller = struct {
var node_modules_path: bun.AbsPath(.{}) = .from(this.node_modules.path.items);
defer node_modules_path.deinit();

const pkgs = lockfile.packages.slice();
const pkg_name_hashes = pkgs.items(.name_hash);
const pkg_metas = pkgs.items(.meta);
const pkg_resolutions_lists = pkgs.items(.resolutions);
const pkg_resolutions_buffer = lockfile.buffers.resolutions.items;
const pkg_names = pkgs.items(.name);

while (tree.binaries.removeOrNull()) |dep_id| {
bun.assertWithLocation(dep_id < lockfile.buffers.dependencies.items.len, @src());
const package_id = lockfile.buffers.resolutions.items[dep_id];
Expand All @@ -268,20 +275,33 @@ pub const PackageInstaller = struct {
bun.assertWithLocation(bin.tag != .none, @src());

const alias = lockfile.buffers.dependencies.items[dep_id].name.slice(string_buf);

var bin_linker: Bin.Linker = .{
.bin = bin,
.global_bin_path = this.options.bin_path,
.package_name = strings.StringOrTinyString.init(alias),
.string_buf = string_buf,
.extern_string_buf = lockfile.buffers.extern_strings.items,
.seen = &this.seen_bin_links,
.node_modules_path = &node_modules_path,
.abs_target_buf = link_target_buf,
.abs_dest_buf = link_dest_buf,
.rel_buf = link_rel_buf,
};

const package_name_ = strings.StringOrTinyString.init(alias);
var target_package_name = package_name_;
var can_retry_without_native_binlink_optimization = false;

if (lockfile.postinstall_optimizer.isNativeBinlinkEnabled()) {
// Check for native binlink optimization
const name_hash = pkg_name_hashes[package_id];
if (lockfile.postinstall_optimizer.get(name_hash)) |optimizer| {
switch (optimizer) {
.native_binlink => {
const target_cpu = this.manager.options.cpu;
const target_os = this.manager.options.os;
if (PostinstallOptimizer.getNativeBinlinkReplacementPackageID(
pkg_resolutions_lists[package_id].get(pkg_resolutions_buffer),
pkg_metas,
target_cpu,
target_os,
)) |replacement_pkg_id| {
const replacement_name = pkg_names[replacement_pkg_id].slice(string_buf);
target_package_name = strings.StringOrTinyString.init(replacement_name);
can_retry_without_native_binlink_optimization = true;
}
},
.ignore => {},
}
}
}
// globally linked packages shouls always belong to the root
// tree (0).
const global = if (!this.manager.options.global)
Expand All @@ -298,21 +318,47 @@ pub const PackageInstaller = struct {
break :global false;
};

bin_linker.link(global);
while (true) {
var bin_linker: Bin.Linker = .{
.bin = bin,
.global_bin_path = this.options.bin_path,
.package_name = package_name_,
.target_package_name = target_package_name,
.string_buf = string_buf,
.extern_string_buf = lockfile.buffers.extern_strings.items,
.seen = &this.seen_bin_links,
.node_modules_path = &node_modules_path,
.target_node_modules_path = &node_modules_path,
.abs_target_buf = link_target_buf,
.abs_dest_buf = link_dest_buf,
.rel_buf = link_rel_buf,
};

bin_linker.link(global);

if (bin_linker.err) |err| {
if (log_level != .silent) {
this.manager.log.addErrorFmtOpts(
this.manager.allocator,
"Failed to link <b>{s}<r>: {s}",
.{ alias, @errorName(err) },
.{},
) catch |e| bun.handleOom(e);
}
if (bin_linker.err) |err| {
if (can_retry_without_native_binlink_optimization) {
can_retry_without_native_binlink_optimization = false;
bin_linker.err = null;
target_package_name = package_name_;
continue;
}

if (log_level != .silent) {
this.manager.log.addErrorFmtOpts(
this.manager.allocator,
"Failed to link <b>{s}<r>: {s}",
.{ alias, @errorName(err) },
.{},
) catch |e| bun.handleOom(e);
}

if (this.options.enable.fail_early) {
this.manager.crash();
if (this.options.enable.fail_early) {
this.manager.crash();
}
}

break;
}
}
}
Expand Down Expand Up @@ -357,6 +403,7 @@ pub const PackageInstaller = struct {
if (this.canRunScripts(tree_id)) {
_ = this.pending_lifecycle_scripts.swapRemove(i);
const output_in_foreground = false;

this.manager.spawnPackageLifecycleScripts(
this.command_ctx,
entry.list,
Expand Down Expand Up @@ -1100,22 +1147,28 @@ pub const PackageInstaller = struct {
defer folder_path.deinit();
folder_path.append(alias.slice(this.lockfile.buffers.string_bytes.items));

if (this.enqueueLifecycleScripts(
alias.slice(this.lockfile.buffers.string_bytes.items),
log_level,
&folder_path,
package_id,
dep.behavior.optional,
resolution,
)) {
if (is_trusted_through_update_request) {
this.manager.trusted_deps_to_add_to_package_json.append(
this.manager.allocator,
bun.handleOom(this.manager.allocator.dupe(u8, alias.slice(this.lockfile.buffers.string_bytes.items))),
) catch |err| bun.handleOom(err);

if (this.lockfile.trusted_dependencies == null) this.lockfile.trusted_dependencies = .{};
this.lockfile.trusted_dependencies.?.put(this.manager.allocator, truncated_dep_name_hash, {}) catch |err| bun.handleOom(err);
enqueueLifecycleScripts: {
if (this.lockfile.postinstall_optimizer.shouldIgnoreLifecycleScripts(pkg_name_hash)) {
break :enqueueLifecycleScripts;
}

if (this.enqueueLifecycleScripts(
alias.slice(this.lockfile.buffers.string_bytes.items),
log_level,
&folder_path,
package_id,
dep.behavior.optional,
resolution,
)) {
if (is_trusted_through_update_request) {
this.manager.trusted_deps_to_add_to_package_json.append(
this.manager.allocator,
bun.handleOom(this.manager.allocator.dupe(u8, alias.slice(this.lockfile.buffers.string_bytes.items))),
) catch |err| bun.handleOom(err);

if (this.lockfile.trusted_dependencies == null) this.lockfile.trusted_dependencies = .{};
this.lockfile.trusted_dependencies.?.put(this.manager.allocator, truncated_dep_name_hash, {}) catch |err| bun.handleOom(err);
}
}
}
}
Expand Down Expand Up @@ -1272,24 +1325,30 @@ pub const PackageInstaller = struct {
defer folder_path.deinit();
folder_path.append(alias.slice(this.lockfile.buffers.string_bytes.items));

if (this.enqueueLifecycleScripts(
alias.slice(this.lockfile.buffers.string_bytes.items),
log_level,
&folder_path,
package_id,
dep.behavior.optional,
resolution,
)) {
if (is_trusted_through_update_request) {
this.manager.trusted_deps_to_add_to_package_json.append(
this.manager.allocator,
bun.handleOom(this.manager.allocator.dupe(u8, alias.slice(this.lockfile.buffers.string_bytes.items))),
) catch |err| bun.handleOom(err);
enqueueLifecycleScripts: {
if (this.lockfile.postinstall_optimizer.shouldIgnoreLifecycleScripts(pkg_name_hash)) {
break :enqueueLifecycleScripts;
}

if (add_to_lockfile) {
if (this.lockfile.trusted_dependencies == null) this.lockfile.trusted_dependencies = .{};
this.lockfile.trusted_dependencies.?.put(this.manager.allocator, truncated_dep_name_hash, {}) catch |err| bun.handleOom(err);
if (this.enqueueLifecycleScripts(
alias.slice(this.lockfile.buffers.string_bytes.items),
log_level,
&folder_path,
package_id,
dep.behavior.optional,
resolution,
)) {
if (is_trusted_through_update_request) {
this.manager.trusted_deps_to_add_to_package_json.append(
this.manager.allocator,
bun.handleOom(this.manager.allocator.dupe(u8, alias.slice(this.lockfile.buffers.string_bytes.items))),
) catch |err| bun.handleOom(err);
}

if (add_to_lockfile) {
if (this.lockfile.trusted_dependencies == null) this.lockfile.trusted_dependencies = .{};
this.lockfile.trusted_dependencies.?.put(this.manager.allocator, truncated_dep_name_hash, {}) catch |err| bun.handleOom(err);
}
}
}
}
Expand Down Expand Up @@ -1424,6 +1483,7 @@ const PackageID = install.PackageID;
const PackageInstall = install.PackageInstall;
const PackageNameHash = install.PackageNameHash;
const PatchTask = install.PatchTask;
const PostinstallOptimizer = install.PostinstallOptimizer;
const Resolution = install.Resolution;
const Task = install.Task;
const TaskCallbackContext = install.TaskCallbackContext;
Expand Down
12 changes: 10 additions & 2 deletions src/install/bin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ pub const Bin = extern struct {
pub const Linker = struct {
bin: Bin,

/// Usually will be the same as `node_modules_path`.
/// Used to support native bin linking.
target_node_modules_path: *bun.AbsPath(.{}),

/// Usually will be the same as `package_name`.
/// Used to support native bin linking.
target_package_name: strings.StringOrTinyString,

// Hash map of seen destination paths for this `node_modules/.bin` folder. PackageInstaller will reset it before
// linking each tree.
seen: ?*bun.StringHashMap(void),
Expand Down Expand Up @@ -838,7 +846,7 @@ pub const Bin = extern struct {

/// uses `this.abs_target_buf`
pub fn buildTargetPackageDir(this: *const Linker) []const u8 {
const dest_dir_without_trailing_slash = strings.withoutTrailingSlash(this.node_modules_path.slice());
const dest_dir_without_trailing_slash = strings.withoutTrailingSlash(this.target_node_modules_path.slice());

var remain = this.abs_target_buf;

Expand All @@ -847,7 +855,7 @@ pub const Bin = extern struct {
remain[0] = std.fs.path.sep;
remain = remain[1..];

const package_name = this.package_name.slice();
const package_name = this.target_package_name.slice();
@memcpy(remain[0..package_name.len], package_name);
remain = remain[package_name.len..];
remain[0] = std.fs.path.sep;
Expand Down
1 change: 1 addition & 0 deletions src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ pub const Resolution = @import("./resolution.zig").Resolution;
pub const Store = @import("./isolated_install/Store.zig").Store;
pub const FileCopier = @import("./isolated_install/FileCopier.zig").FileCopier;
pub const PnpmMatcher = @import("./PnpmMatcher.zig");
pub const PostinstallOptimizer = @import("./postinstall_optimizer.zig").PostinstallOptimizer;

pub const ArrayIdentityContext = @import("../identity_context.zig").ArrayIdentityContext;
pub const IdentityContext = @import("../identity_context.zig").IdentityContext;
Expand Down
Loading