Skip to content
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

Make Command.Context a pointer #10237

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 6 additions & 8 deletions src/bun_js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ pub const Run = struct {
arena: Arena,
any_unhandled: bool = false,

pub fn bootStandalone(ctx_: Command.Context, entry_path: string, graph: bun.StandaloneModuleGraph) !void {
var ctx = ctx_;
pub fn bootStandalone(ctx: Command.Context, entry_path: string, graph: bun.StandaloneModuleGraph) !void {
JSC.markBinding(@src());
bun.JSC.initialize();

Expand All @@ -55,7 +54,7 @@ pub const Run = struct {
var arena = try Arena.init();

if (!ctx.debug.loaded_bunfig) {
try bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", &ctx, .RunCommand);
try bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", ctx, .RunCommand);
}

run = .{
Expand Down Expand Up @@ -123,7 +122,7 @@ pub const Run = struct {
vm.global.vm().holdAPILock(&run, callback);
}

fn bootBunShell(ctx: *const Command.Context, entry_path: []const u8) !bun.shell.ExitCode {
fn bootBunShell(ctx: Command.Context, entry_path: []const u8) !bun.shell.ExitCode {
@setCold(true);

// this is a hack: make dummy bundler so we can use its `.runEnvLoader()` function to populate environment variables probably should split out the functionality
Expand All @@ -139,16 +138,15 @@ pub const Run = struct {
return bun.shell.Interpreter.initAndRunFromFile(ctx, mini, entry_path);
}

pub fn boot(ctx_: Command.Context, entry_path: string) !void {
var ctx = ctx_;
pub fn boot(ctx: Command.Context, entry_path: string) !void {
JSC.markBinding(@src());

if (!ctx.debug.loaded_bunfig) {
try bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", &ctx, .RunCommand);
try bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", ctx, .RunCommand);
}

if (strings.endsWithComptime(entry_path, ".sh")) {
const exit_code = try bootBunShell(&ctx, entry_path);
const exit_code = try bootBunShell(ctx, entry_path);
Global.exitWide(exit_code);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/bunfig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub const Bunfig = struct {
log: *logger.Log,
allocator: std.mem.Allocator,
bunfig: *Api.TransformOptions,
ctx: *Command.Context,
ctx: Command.Context,

fn addError(this: *Parser, loc: logger.Loc, comptime text: string) !void {
this.log.addError(this.source, loc, text) catch unreachable;
Expand Down Expand Up @@ -777,7 +777,7 @@ pub const Bunfig = struct {
}
};

pub fn parse(allocator: std.mem.Allocator, source: logger.Source, ctx: *Command.Context, comptime cmd: Command.Tag) !void {
pub fn parse(allocator: std.mem.Allocator, source: logger.Source, ctx: Command.Context, comptime cmd: Command.Tag) !void {
const log_count = ctx.log.errors + ctx.log.warnings;

const expr = if (strings.eqlComptime(source.path.name.ext[1..], "toml")) TOML.parse(&source, ctx.log, allocator) catch |err| {
Expand Down
95 changes: 51 additions & 44 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ pub const Arguments = struct {
Global.exit(0);
}

pub fn loadConfigPath(allocator: std.mem.Allocator, auto_loaded: bool, config_path: [:0]const u8, ctx: *Command.Context, comptime cmd: Command.Tag) !void {
pub fn loadConfigPath(allocator: std.mem.Allocator, auto_loaded: bool, config_path: [:0]const u8, ctx: Command.Context, comptime cmd: Command.Tag) !void {
var config_file = switch (bun.sys.openA(config_path, std.os.O.RDONLY, 0)) {
.result => |fd| fd.asFile(),
.err => |err| {
Expand Down Expand Up @@ -306,7 +306,7 @@ pub const Arguments = struct {

return null;
}
pub fn loadConfig(allocator: std.mem.Allocator, user_config_path_: ?string, ctx: *Command.Context, comptime cmd: Command.Tag) !void {
pub fn loadConfig(allocator: std.mem.Allocator, user_config_path_: ?string, ctx: Command.Context, comptime cmd: Command.Tag) !void {
var config_buf: [bun.MAX_PATH_BYTES]u8 = undefined;
if (comptime cmd.readGlobalConfig()) {
if (!ctx.has_loaded_global_config) {
Expand Down Expand Up @@ -368,12 +368,12 @@ pub const Arguments = struct {
comptime cmd: Command.Tag,
allocator: std.mem.Allocator,
args: clap.Args(clap.Help, cmd.params()),
ctx: *Command.Context,
ctx: Command.Context,
) !void {
return try loadConfig(allocator, args.option("--config"), ctx, comptime cmd);
}

pub fn parse(allocator: std.mem.Allocator, ctx: *Command.Context, comptime cmd: Command.Tag) !Api.TransformOptions {
pub fn parse(allocator: std.mem.Allocator, ctx: Command.Context, comptime cmd: Command.Tag) !Api.TransformOptions {
var diag = clap.Diagnostic{};
const params_to_parse = comptime cmd.params();

Expand Down Expand Up @@ -1136,7 +1136,18 @@ pub const Command = struct {
} = .{},
};

pub const Context = struct {
var global_cli_ctx: Context = undefined;

var context_data: ContextData = ContextData{
.args = std.mem.zeroes(Api.TransformOptions),
.log = undefined,
.start_time = 0,
.allocator = undefined,
};

pub const init = ContextData.create;

pub const ContextData = struct {
start_time: i128,
args: Api.TransformOptions,
log: *logger.Log,
Expand Down Expand Up @@ -1173,34 +1184,28 @@ pub const Command = struct {
minify_identifiers: bool = false,
};

const _ctx = Command.Context{
.args = std.mem.zeroes(Api.TransformOptions),
.log = undefined,
.start_time = 0,
.allocator = undefined,
};

pub fn create(allocator: std.mem.Allocator, log: *logger.Log, comptime command: Command.Tag) anyerror!Context {
Cli.cmd = command;
var ctx = _ctx;
ctx.log = log;
ctx.start_time = start_time;
ctx.allocator = allocator;
global_cli_ctx = &context_data;
global_cli_ctx.log = log;
global_cli_ctx.start_time = start_time;
global_cli_ctx.allocator = allocator;

if (comptime Command.Tag.uses_global_options.get(command)) {
ctx.args = try Arguments.parse(allocator, &ctx, command);
global_cli_ctx.args = try Arguments.parse(allocator, global_cli_ctx, command);
}

if (comptime Environment.isWindows) {
if (ctx.debug.hot_reload == .watch and !bun.isWatcherChild()) {
if (global_cli_ctx.debug.hot_reload == .watch and !bun.isWatcherChild()) {
// this is noreturn
bun.becomeWatcherManager(allocator);
}
}

return ctx;
return global_cli_ctx;
}
};
pub const Context = *ContextData;

// std.process.args allocates!
const ArgsIterator = struct {
Expand Down Expand Up @@ -1371,12 +1376,14 @@ pub const Command = struct {

// bun build --compile entry point
if (try bun.StandaloneModuleGraph.fromExecutable(bun.default_allocator)) |graph| {
var ctx = Command.Context{
context_data = .{
.args = std.mem.zeroes(Api.TransformOptions),
.log = log,
.start_time = start_time,
.allocator = bun.default_allocator,
};
global_cli_ctx = &context_data;
var ctx = global_cli_ctx;

ctx.args.target = Api.Target.bun;
if (bun.argv().len > 1) {
Expand Down Expand Up @@ -1404,7 +1411,7 @@ pub const Command = struct {
.InitCommand => return try InitCommand.exec(allocator, bun.argv()),
.BuildCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .BuildCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .BuildCommand);
const ctx = try Command.init(allocator, log, .BuildCommand);
try BuildCommand.exec(ctx);
},
.InstallCompletionsCommand => {
Expand All @@ -1414,36 +1421,36 @@ pub const Command = struct {
},
.InstallCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .InstallCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .InstallCommand);
const ctx = try Command.init(allocator, log, .InstallCommand);

try InstallCommand.exec(ctx);
return;
},
.AddCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .AddCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .AddCommand);
const ctx = try Command.init(allocator, log, .AddCommand);

try AddCommand.exec(ctx);
return;
},
.UpdateCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .UpdateCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .UpdateCommand);
const ctx = try Command.init(allocator, log, .UpdateCommand);

try UpdateCommand.exec(ctx);
return;
},
.BunxCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .BunxCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .BunxCommand);
const ctx = try Command.init(allocator, log, .BunxCommand);

try BunxCommand.exec(ctx, bun.argv()[if (is_bunx_exe) 0 else 1..]);
return;
},
.ReplCommand => {
// TODO: Put this in native code.
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .BunxCommand) unreachable;
var ctx = try Command.Context.create(allocator, log, .BunxCommand);
var ctx = try Command.init(allocator, log, .BunxCommand);
ctx.debug.run_in_bun = true; // force the same version of bun used. fixes bun-debug for example
var args = bun.argv()[0..];
args[1] = "bun-repl";
Expand All @@ -1452,42 +1459,42 @@ pub const Command = struct {
},
.RemoveCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .RemoveCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .RemoveCommand);
const ctx = try Command.init(allocator, log, .RemoveCommand);

try RemoveCommand.exec(ctx);
return;
},
.LinkCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .LinkCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .LinkCommand);
const ctx = try Command.init(allocator, log, .LinkCommand);

try LinkCommand.exec(ctx);
return;
},
.UnlinkCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .UnlinkCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .UnlinkCommand);
const ctx = try Command.init(allocator, log, .UnlinkCommand);

try UnlinkCommand.exec(ctx);
return;
},
.PackageManagerCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .PackageManagerCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .PackageManagerCommand);
const ctx = try Command.init(allocator, log, .PackageManagerCommand);

try PackageManagerCommand.exec(ctx);
return;
},
.TestCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .TestCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .TestCommand);
const ctx = try Command.init(allocator, log, .TestCommand);

try TestCommand.exec(ctx);
return;
},
.GetCompletionsCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .GetCompletionsCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .GetCompletionsCommand);
const ctx = try Command.init(allocator, log, .GetCompletionsCommand);
var filter = ctx.positionals;

for (filter, 0..) |item, i| {
Expand Down Expand Up @@ -1583,7 +1590,7 @@ pub const Command = struct {
});

// Create command wraps bunx
const ctx = try Command.Context.create(allocator, log, .CreateCommand);
const ctx = try Command.init(allocator, log, .CreateCommand);

var args = try std.process.argsAlloc(allocator);

Expand Down Expand Up @@ -1699,7 +1706,7 @@ pub const Command = struct {
},
.RunCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .RunCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .RunCommand);
const ctx = try Command.init(allocator, log, .RunCommand);

if (ctx.filters.len > 0) {
FilterRun.runScriptsWithFilter(ctx) catch |err| {
Expand All @@ -1718,20 +1725,20 @@ pub const Command = struct {
},
.RunAsNodeCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .RunAsNodeCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .RunAsNodeCommand);
const ctx = try Command.init(allocator, log, .RunAsNodeCommand);
bun.assert(pretend_to_be_node);
try RunCommand.execAsIfNode(ctx);
},
.UpgradeCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .UpgradeCommand) unreachable;
const ctx = try Command.Context.create(allocator, log, .UpgradeCommand);
const ctx = try Command.init(allocator, log, .UpgradeCommand);
try UpgradeCommand.exec(ctx);
return;
},
.AutoCommand => {
if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .AutoCommand) unreachable;

var ctx = Command.Context.create(allocator, log, .AutoCommand) catch |e| {
const ctx = Command.init(allocator, log, .AutoCommand) catch |e| {
switch (e) {
error.MissingEntryPoint => {
HelpCommand.execWithReason(allocator, .explicit);
Expand Down Expand Up @@ -1805,7 +1812,7 @@ pub const Command = struct {
}

if (!ctx.debug.loaded_bunfig) {
try bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", &ctx, .RunCommand);
try bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", ctx, .RunCommand);
}

if (ctx.preloads.len > 0)
Expand All @@ -1820,15 +1827,15 @@ pub const Command = struct {
if (default_loader) |loader| {
if (loader.canBeRunByBun()) {
was_js_like = true;
if (maybeOpenWithBunJS(&ctx)) {
if (maybeOpenWithBunJS(ctx)) {
return;
}
did_check = true;
}
}

if (force_using_bun and !did_check) {
if (maybeOpenWithBunJS(&ctx)) {
if (maybeOpenWithBunJS(ctx)) {
return;
}
}
Expand Down Expand Up @@ -1873,16 +1880,16 @@ pub const Command = struct {
try HelpCommand.exec(allocator);
},
.ExecCommand => {
var ctx = try Command.Context.create(allocator, log, .RunCommand);
const ctx = try Command.init(allocator, log, .RunCommand);

if (ctx.positionals.len > 1) {
try ExecCommand.exec(&ctx);
try ExecCommand.exec(ctx);
} else Tag.printHelp(.ExecCommand, true);
},
}
}

fn maybeOpenWithBunJS(ctx: *Command.Context) bool {
fn maybeOpenWithBunJS(ctx: Command.Context) bool {
if (ctx.args.entry_points.len == 0)
return false;

Expand Down Expand Up @@ -1959,7 +1966,7 @@ pub const Command = struct {
}

BunJS.Run.boot(
ctx.*,
ctx,
absolute_script_path.?,
) catch |err| {
if (Output.enable_ansi_colors) {
Expand Down
7 changes: 3 additions & 4 deletions src/cli/build_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ var estimated_input_lines_of_code_: usize = undefined;

pub const BuildCommand = struct {
pub fn exec(
ctx_: Command.Context,
ctx: Command.Context,
) !void {
Global.configureAllocator(.{ .long_running = true });
var ctx = ctx_;
const allocator = ctx.allocator;
var log = ctx.log;
estimated_input_lines_of_code_ = 0;
Expand All @@ -63,8 +62,8 @@ pub const BuildCommand = struct {
}
var outfile = ctx.bundler_options.outfile;

this_bundler.options.public_path = ctx_.bundler_options.public_path;
this_bundler.resolver.opts.public_path = ctx_.bundler_options.public_path;
this_bundler.options.public_path = ctx.bundler_options.public_path;
this_bundler.resolver.opts.public_path = ctx.bundler_options.public_path;

this_bundler.options.entry_naming = ctx.bundler_options.entry_naming;
this_bundler.options.chunk_naming = ctx.bundler_options.chunk_naming;
Expand Down
Loading
Loading