Skip to content

Commit

Permalink
Merge branch 'main' into dylan/update-test-lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-conway authored Oct 24, 2024
2 parents 57e3f10 + 9643a92 commit 44db43c
Show file tree
Hide file tree
Showing 21 changed files with 547 additions and 65 deletions.
2 changes: 1 addition & 1 deletion LATEST
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.32
1.1.33
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "bun",
"version": "1.1.33",
"version": "1.1.34",
"workspaces": [
"./packages/bun-types"
],
Expand Down
12 changes: 6 additions & 6 deletions src/bun.js/ResolveMessage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,22 @@ pub const ResolveMessage = struct {
switch (err) {
error.ModuleNotFound => {
if (strings.eqlComptime(referrer, "bun:main")) {
return try std.fmt.allocPrint(allocator, "Module not found \"{s}\"", .{specifier});
return try std.fmt.allocPrint(allocator, "Module not found '{s}'", .{specifier});
}
if (Resolver.isPackagePath(specifier) and !strings.containsChar(specifier, '/')) {
return try std.fmt.allocPrint(allocator, "Cannot find package \"{s}\" from \"{s}\"", .{ specifier, referrer });
return try std.fmt.allocPrint(allocator, "Cannot find package '{s}' from '{s}'", .{ specifier, referrer });
} else {
return try std.fmt.allocPrint(allocator, "Cannot find module \"{s}\" from \"{s}\"", .{ specifier, referrer });
return try std.fmt.allocPrint(allocator, "Cannot find module '{s}' from '{s}'", .{ specifier, referrer });
}
},
error.InvalidDataURL => {
return try std.fmt.allocPrint(allocator, "Cannot resolve invalid data URL \"{s}\" from \"{s}\"", .{ specifier, referrer });
return try std.fmt.allocPrint(allocator, "Cannot resolve invalid data URL '{s}' from '{s}'", .{ specifier, referrer });
},
else => {
if (Resolver.isPackagePath(specifier)) {
return try std.fmt.allocPrint(allocator, "{s} while resolving package \"{s}\" from \"{s}\"", .{ @errorName(err), specifier, referrer });
return try std.fmt.allocPrint(allocator, "{s} while resolving package '{s}' from '{s}'", .{ @errorName(err), specifier, referrer });
} else {
return try std.fmt.allocPrint(allocator, "{s} while resolving \"{s}\" from \"{s}\"", .{ @errorName(err), specifier, referrer });
return try std.fmt.allocPrint(allocator, "{s} while resolving '{s}' from '{s}'", .{ @errorName(err), specifier, referrer });
}
},
}
Expand Down
9 changes: 8 additions & 1 deletion src/cli/pm_trusted_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,15 @@ pub const TrustCommand = struct {
}

const output_in_foreground = false;
const optional = false;
switch (pm.options.log_level) {
inline else => |log_level| try pm.spawnPackageLifecycleScripts(ctx, info.scripts_list, log_level, output_in_foreground),
inline else => |log_level| try pm.spawnPackageLifecycleScripts(
ctx,
info.scripts_list,
optional,
log_level,
output_in_foreground,
),
}

if (pm.options.log_level.showProgress()) {
Expand Down
87 changes: 75 additions & 12 deletions src/install/install.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// Default to a maximum of 64 simultaneous HTTP requests for bun install if no proxy is specified
// if a proxy IS specified, default to 64. We have different values because we might change this in the future.
// https://github.com/npm/cli/issues/7072
// https://pnpm.io/npmrc#network-concurrency (pnpm defaults to 16)
// https://yarnpkg.com/configuration/yarnrc#networkConcurrency (defaults to 50)
const default_max_simultaneous_requests_for_bun_install = 64;
const default_max_simultaneous_requests_for_bun_install_for_proxies = 64;

const bun = @import("root").bun;
const FeatureFlags = bun.FeatureFlags;
const string = bun.string;
Expand Down Expand Up @@ -266,6 +274,11 @@ const NetworkTask = struct {
this.package_manager.async_network_task_queue.push(this);
}

pub const Authorization = enum {
no_authorization,
allow_authorization,
};

// We must use a less restrictive Accept header value
// https://github.com/oven-sh/bun/issues/341
// https://www.jfrog.com/jira/browse/RTFACT-18398
Expand Down Expand Up @@ -467,6 +480,7 @@ const NetworkTask = struct {
allocator: std.mem.Allocator,
tarball_: *const ExtractTarball,
scope: *const Npm.Registry.Scope,
authorization: NetworkTask.Authorization,
) !void {
this.callback = .{ .extract = tarball_.* };
const tarball = &this.callback.extract;
Expand Down Expand Up @@ -496,14 +510,18 @@ const NetworkTask = struct {
this.allocator = allocator;

var header_builder = HeaderBuilder{};
var header_buf: string = "";

countAuth(&header_builder, scope);
if (authorization == .allow_authorization) {
countAuth(&header_builder, scope);
}

var header_buf: string = "";
if (header_builder.header_count > 0) {
try header_builder.allocate(allocator);

appendAuth(&header_builder, scope);
if (authorization == .allow_authorization) {
appendAuth(&header_builder, scope);
}

header_buf = header_builder.content.ptr.?[0..header_builder.content.len];
}
Expand Down Expand Up @@ -4248,6 +4266,8 @@ pub const PackageManager = struct {
dependency_id,
package,
name_and_version_hash,
// its npm.
.allow_authorization,
) orelse unreachable,
},
},
Expand Down Expand Up @@ -4305,8 +4325,8 @@ pub const PackageManager = struct {
is_required: bool,
dependency_id: DependencyID,
package: Lockfile.Package,
/// if patched then we need to do apply step after network task is done
patch_name_and_version_hash: ?u64,
authorization: NetworkTask.Authorization,
) !?*NetworkTask {
if (this.hasCreatedNetworkTask(task_id, is_required)) {
return null;
Expand Down Expand Up @@ -4350,6 +4370,7 @@ pub const PackageManager = struct {
),
},
scope,
authorization,
);

return network_task;
Expand Down Expand Up @@ -5457,6 +5478,7 @@ pub const PackageManager = struct {
.resolution = res,
},
null,
.no_authorization,
)) |network_task| {
this.enqueueNetworkTask(network_task);
}
Expand Down Expand Up @@ -5658,6 +5680,7 @@ pub const PackageManager = struct {
.resolution = res,
},
null,
.no_authorization,
)) |network_task| {
this.enqueueNetworkTask(network_task);
}
Expand Down Expand Up @@ -7232,7 +7255,7 @@ pub const PackageManager = struct {
{
const token_keys = [_]string{
"BUN_CONFIG_TOKEN",
"NPM_CONFIG_token",
"NPM_CONFIG_TOKEN",
"npm_config_token",
};
var did_set = false;
Expand Down Expand Up @@ -8741,6 +8764,19 @@ pub const PackageManager = struct {
}
}

AsyncHTTP.max_simultaneous_requests.store(brk: {
if (cli.network_concurrency) |network_concurrency| {
break :brk @max(network_concurrency, 1);
}

// If any HTTP proxy is set, use a diferent limit
if (env.has("http_proxy") or env.has("https_proxy") or env.has("HTTPS_PROXY") or env.has("HTTP_PROXY")) {
break :brk default_max_simultaneous_requests_for_bun_install_for_proxies;
}

break :brk default_max_simultaneous_requests_for_bun_install;
}, .monotonic);

HTTP.HTTPThread.init(&.{
.ca = ca,
.abs_ca_file_name = abs_ca_file_name,
Expand Down Expand Up @@ -9290,6 +9326,7 @@ pub const PackageManager = struct {
clap.parseParam("--backend <STR> Platform-specific optimizations for installing dependencies. " ++ platform_specific_backend_label) catch unreachable,
clap.parseParam("--registry <STR> Use a specific registry by default, overriding .npmrc, bunfig.toml and environment variables") catch unreachable,
clap.parseParam("--concurrent-scripts <NUM> Maximum number of concurrent jobs for lifecycle scripts (default 5)") catch unreachable,
clap.parseParam("--network-concurrency <NUM> Maximum number of concurrent network requests (default 48)") catch unreachable,
clap.parseParam("-h, --help Print this help menu") catch unreachable,
};

Expand Down Expand Up @@ -9373,7 +9410,7 @@ pub const PackageManager = struct {
token: string = "",
global: bool = false,
config: ?string = null,

network_concurrency: ?u16 = null,
backend: ?PackageInstall.Method = null,

positionals: []const string = &[_]string{},
Expand Down Expand Up @@ -9763,6 +9800,13 @@ pub const PackageManager = struct {
cli.ca_file_name = ca_file_name;
}

if (args.option("--network-concurrency")) |network_concurrency| {
cli.network_concurrency = std.fmt.parseInt(u16, network_concurrency, 10) catch {
Output.errGeneric("Expected --network-concurrency to be a number between 0 and 65535: {s}", .{network_concurrency});
Global.crash();
};
}

// commands that support --filter
if (comptime subcommand.supportsWorkspaceFiltering()) {
cli.filters = args.options("--filter");
Expand Down Expand Up @@ -12049,6 +12093,7 @@ pub const PackageManager = struct {
pending_lifecycle_scripts: std.ArrayListUnmanaged(struct {
list: Lockfile.Package.Scripts.List,
tree_id: Lockfile.Tree.Id,
optional: bool,
}) = .{},

trusted_dependencies_from_update_requests: std.AutoArrayHashMapUnmanaged(TruncatedPackageNameHash, void),
Expand Down Expand Up @@ -12213,10 +12258,17 @@ pub const PackageManager = struct {
const entry = this.pending_lifecycle_scripts.items[i];
const name = entry.list.package_name;
const tree_id = entry.tree_id;
const optional = entry.optional;
if (this.canRunScripts(tree_id)) {
_ = this.pending_lifecycle_scripts.swapRemove(i);
const output_in_foreground = false;
this.manager.spawnPackageLifecycleScripts(this.command_ctx, entry.list, log_level, output_in_foreground) catch |err| {
this.manager.spawnPackageLifecycleScripts(
this.command_ctx,
entry.list,
optional,
log_level,
output_in_foreground,
) catch |err| {
if (comptime log_level != .silent) {
const fmt = "\n<r><red>error:<r> failed to spawn life-cycle scripts for <b>{s}<r>: {s}\n";
const args = .{ name, @errorName(err) };
Expand Down Expand Up @@ -12299,8 +12351,9 @@ pub const PackageManager = struct {
PackageManager.instance.sleep();
}

const optional = entry.optional;
const output_in_foreground = false;
this.manager.spawnPackageLifecycleScripts(this.command_ctx, entry.list, log_level, output_in_foreground) catch |err| {
this.manager.spawnPackageLifecycleScripts(this.command_ctx, entry.list, optional, log_level, output_in_foreground) catch |err| {
if (comptime log_level != .silent) {
const fmt = "\n<r><red>error:<r> failed to spawn life-cycle scripts for <b>{s}<r>: {s}\n";
const args = .{ package_name, @errorName(err) };
Expand Down Expand Up @@ -12935,7 +12988,8 @@ pub const PackageManager = struct {
this.trees[this.current_tree_id].binaries.add(dependency_id) catch bun.outOfMemory();
}

const name_hash: TruncatedPackageNameHash = @truncate(this.lockfile.buffers.dependencies.items[dependency_id].name_hash);
const dep = this.lockfile.buffers.dependencies.items[dependency_id];
const name_hash: TruncatedPackageNameHash = @truncate(dep.name_hash);
const is_trusted, const is_trusted_through_update_request = brk: {
if (this.trusted_dependencies_from_update_requests.contains(name_hash)) break :brk .{ true, true };
if (this.lockfile.hasTrustedDependency(alias)) break :brk .{ true, false };
Expand All @@ -12948,6 +13002,7 @@ pub const PackageManager = struct {
log_level,
destination_dir,
package_id,
dep.behavior.optional,
resolution,
)) {
if (is_trusted_through_update_request) {
Expand Down Expand Up @@ -13071,7 +13126,8 @@ pub const PackageManager = struct {

defer if (!pkg_has_patch) this.incrementTreeInstallCount(this.current_tree_id, destination_dir, !is_pending_package_install, log_level);

const name_hash: TruncatedPackageNameHash = @truncate(this.lockfile.buffers.dependencies.items[dependency_id].name_hash);
const dep = this.lockfile.buffers.dependencies.items[dependency_id];
const name_hash: TruncatedPackageNameHash = @truncate(dep.name_hash);
const is_trusted, const is_trusted_through_update_request, const add_to_lockfile = brk: {
// trusted through a --trust dependency. need to enqueue scripts, write to package.json, and add to lockfile
if (this.trusted_dependencies_from_update_requests.contains(name_hash)) break :brk .{ true, true, true };
Expand All @@ -13089,6 +13145,7 @@ pub const PackageManager = struct {
log_level,
destination_dir,
package_id,
dep.behavior.optional,
resolution,
)) {
if (is_trusted_through_update_request) {
Expand All @@ -13114,6 +13171,7 @@ pub const PackageManager = struct {
comptime log_level: Options.LogLevel,
node_modules_folder: std.fs.Dir,
package_id: PackageID,
optional: bool,
resolution: *const Resolution,
) bool {
var scripts: Package.Scripts = this.lockfile.packages.items(.scripts)[package_id];
Expand Down Expand Up @@ -13165,6 +13223,7 @@ pub const PackageManager = struct {
this.pending_lifecycle_scripts.append(this.manager.allocator, .{
.list = scripts_list.?,
.tree_id = this.current_tree_id,
.optional = optional,
}) catch bun.outOfMemory();

return true;
Expand Down Expand Up @@ -13302,6 +13361,7 @@ pub const PackageManager = struct {
dependency_id,
this.lockfile.packages.get(package_id),
patch_name_and_version_hash,
.allow_authorization,
) catch unreachable) |task| {
task.schedule(&this.network_tarball_batch);
if (this.network_tarball_batch.len > 0) {
Expand Down Expand Up @@ -13338,6 +13398,7 @@ pub const PackageManager = struct {
dependency_id,
this.lockfile.packages.get(package_id),
patch_name_and_version_hash,
.no_authorization,
) catch unreachable) |task| {
task.schedule(&this.network_tarball_batch);
if (this.network_tarball_batch.len > 0) {
Expand Down Expand Up @@ -14593,8 +14654,9 @@ pub const PackageManager = struct {
}
// root lifecycle scripts can run now that all dependencies are installed, dependency scripts
// have finished, and lockfiles have been saved
const optional = false;
const output_in_foreground = true;
try manager.spawnPackageLifecycleScripts(ctx, scripts, log_level, output_in_foreground);
try manager.spawnPackageLifecycleScripts(ctx, scripts, optional, log_level, output_in_foreground);

while (manager.pending_lifecycle_script_tasks.load(.monotonic) > 0) {
if (PackageManager.verbose_install) {
Expand Down Expand Up @@ -14778,6 +14840,7 @@ pub const PackageManager = struct {
this: *PackageManager,
ctx: Command.Context,
list: Lockfile.Package.Scripts.List,
optional: bool,
comptime log_level: PackageManager.Options.LogLevel,
comptime foreground: bool,
) !void {
Expand Down Expand Up @@ -14827,7 +14890,7 @@ pub const PackageManager = struct {
try this_bundler.env.map.put("PATH", original_path);
PATH.deinit();

try LifecycleScriptSubprocess.spawnPackageScripts(this, list, envp, log_level, foreground);
try LifecycleScriptSubprocess.spawnPackageScripts(this, list, envp, optional, log_level, foreground);
}
};

Expand Down
Loading

0 comments on commit 44db43c

Please sign in to comment.