aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Colin McDonnell <colinmcd94@gmail.com> 2023-08-15 17:42:20 -0700
committerGravatar Colin McDonnell <colinmcd94@gmail.com> 2023-08-21 17:49:58 -0700
commitedb6ed62a479adfa12defb5c52cb0693aa2a5cfe (patch)
treecbdf922989c913e53e138afb4fd86a36803b6ee3
parentf629365cb7a899229c70c6bed14a41fa62ea87f1 (diff)
downloadbun-edb6ed62a479adfa12defb5c52cb0693aa2a5cfe.tar.gz
bun-edb6ed62a479adfa12defb5c52cb0693aa2a5cfe.tar.zst
bun-edb6ed62a479adfa12defb5c52cb0693aa2a5cfe.zip
Support bun.json
-rw-r--r--src/bun.js/api/server.zig2
-rw-r--r--src/bun_js.zig9
-rw-r--r--src/cli.zig184
-rw-r--r--src/cli/run_command.zig4
-rw-r--r--src/install/install.zig2
5 files changed, 159 insertions, 42 deletions
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig
index 45c82b9fa..97c289525 100644
--- a/src/bun.js/api/server.zig
+++ b/src/bun.js/api/server.zig
@@ -5260,7 +5260,7 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp
editor.open(ctx.path, url, line, column, this.allocator) catch Output.prettyErrorln("Failed to open editor", .{});
} else {
resp.writeStatus("500 Missing Editor :(");
- resp.end("Please set your editor in bunfig.toml", false);
+ resp.end("Please set your editor in bun.json", false);
}
}
diff --git a/src/bun_js.zig b/src/bun_js.zig
index bb59d7e87..b0457febc 100644
--- a/src/bun_js.zig
+++ b/src/bun_js.zig
@@ -55,7 +55,10 @@ 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);
+ _ = bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bun.json", &ctx, .RunCommand) catch blk: {
+ break :blk bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", &ctx, .RunCommand) catch false;
+ };
}
run = .{
@@ -142,7 +145,9 @@ 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);
+ _ = bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bun.json", &ctx, .RunCommand) catch blk: {
+ break :blk bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", &ctx, .RunCommand) catch false;
+ };
}
run = .{
diff --git a/src/cli.zig b/src/cli.zig
index 0fb618afc..97aaa3713 100644
--- a/src/cli.zig
+++ b/src/cli.zig
@@ -136,7 +136,7 @@ pub const Arguments = struct {
clap.parseParam("-h, --help Display this help and exit.") catch unreachable,
clap.parseParam("-b, --bun Force a script or package to use Bun.js instead of Node.js (via symlinking node)") catch unreachable,
clap.parseParam("--cwd <STR> Absolute path to resolve files & entry points from. This just changes the process' cwd.") catch unreachable,
- clap.parseParam("-c, --config <PATH>? Config file to load bun from (e.g. -c bunfig.toml") catch unreachable,
+ clap.parseParam("-c, --config <PATH> Config file to load bun from (e.g. -c bun.json") catch unreachable,
clap.parseParam("--extension-order <STR>... Defaults to: .tsx,.ts,.jsx,.js,.json ") catch unreachable,
clap.parseParam("--jsx-factory <STR> Changes the function called when compiling JSX elements using the classic JSX runtime") catch unreachable,
clap.parseParam("--jsx-fragment <STR> Changes the function called when compiling JSX fragments") catch unreachable,
@@ -240,10 +240,10 @@ 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) !bool {
var config_file = std.fs.File{
.handle = std.os.openZ(config_path, std.os.O.RDONLY, 0) catch |err| {
- if (auto_loaded) return;
+ if (auto_loaded) return false;
Output.prettyErrorln("<r><red>error<r>: {s} opening config \"{s}\"", .{
@errorName(err),
config_path,
@@ -253,7 +253,7 @@ pub const Arguments = struct {
};
defer config_file.close();
var contents = config_file.readToEndAlloc(allocator, std.math.maxInt(usize)) catch |err| {
- if (auto_loaded) return;
+ if (auto_loaded) return false;
Output.prettyErrorln("<r><red>error<r>: {s} reading config \"{s}\"", .{
@errorName(err),
config_path,
@@ -273,9 +273,10 @@ pub const Arguments = struct {
}
ctx.log.level = logger.Log.Level.warn;
try Bunfig.parse(allocator, logger.Source.initPathString(bun.asByteSlice(config_path), contents), ctx, cmd);
+ return true;
}
- fn getHomeConfigPath(buf: *[bun.MAX_PATH_BYTES]u8) ?[:0]const u8 {
+ fn getRootBunfigPath(buf: *[bun.MAX_PATH_BYTES]u8) ?[:0]const u8 {
if (bun.getenvZ("XDG_CONFIG_HOME") orelse bun.getenvZ("HOME")) |data_dir| {
var paths = [_]string{".bunfig.toml"};
return resolve_path.joinAbsStringBufZ(data_dir, buf, &paths, .auto);
@@ -283,48 +284,98 @@ pub const Arguments = struct {
return null;
}
+
+ fn getRootBunJSONPath(buf: *[bun.MAX_PATH_BYTES]u8) ?[:0]const u8 {
+ if (bun.getenvZ("XDG_CONFIG_HOME") orelse bun.getenvZ("HOME")) |data_dir| {
+ var paths = [_]string{"bun.json"};
+ return resolve_path.joinAbsStringBufZ(data_dir, buf, &paths, .auto);
+ }
+
+ return null;
+ }
pub fn loadConfig(allocator: std.mem.Allocator, user_config_path_: ?string, ctx: *Command.Context, comptime cmd: Command.Tag) !void {
+ Output.debug("loadConfig\n", .{});
var config_buf: [bun.MAX_PATH_BYTES]u8 = undefined;
- if (comptime cmd.readGlobalConfig()) {
- if (!ctx.has_loaded_global_config) {
- ctx.has_loaded_global_config = true;
+ if (comptime cmd.readGlobalConfig()) brk: {
+ if (ctx.has_loaded_global_config) break :brk;
+
+ if (getRootBunfigPath(&config_buf)) |path| {
+ Output.debug("trying to load {s}\n", .{path});
+ const success = loadConfigPath(allocator, true, path, ctx, comptime cmd) catch false;
+ if (success) {
+ Output.debug("successfully loaded global bunfig\n", .{});
+ ctx.has_loaded_global_config = true;
+ break :brk;
+ } else {
+ Output.debug("failed to load global bunfig\n", .{});
+ }
+ }
- if (getHomeConfigPath(&config_buf)) |path| {
- try loadConfigPath(allocator, true, path, ctx, comptime cmd);
+ if (getRootBunJSONPath(&config_buf)) |path| {
+ Output.debug("trying to load {s}\n", .{path});
+ const success = loadConfigPath(allocator, true, path, ctx, comptime cmd) catch false;
+ if (success) {
+ Output.debug("successfully loaded global bun.json\n", .{});
+ ctx.has_loaded_global_config = true;
+ break :brk;
+ } else {
+ Output.debug("failed to load global bun.json\n", .{});
}
}
}
+ var config_path: [:0]u8 = undefined;
var config_path_: []const u8 = user_config_path_ orelse "";
- var auto_loaded: bool = false;
- if (config_path_.len == 0 and (user_config_path_ != null or
- Command.Tag.always_loads_config.get(cmd) or
- (cmd == .AutoCommand and
- // "bun"
- (ctx.positionals.len == 0 or
- // "bun file.js"
- ctx.positionals.len > 0 and options.defaultLoaders.has(std.fs.path.extension(ctx.positionals[0]))))))
- {
- config_path_ = "bunfig.toml";
- auto_loaded = true;
- }
-
- if (config_path_.len == 0) {
- return;
- }
- defer ctx.debug.loaded_bunfig = true;
- var config_path: [:0]u8 = undefined;
- if (config_path_[0] == '/') {
+ if (config_path_.len > 0 and config_path_[0] == '/') {
+ Output.debug("loading config from user-specified abs path\n", .{});
+ // config file is absolute path (user-specified)
+ // if (config_path_[0] == '/') {
+ Output.debug("config_path_: {s}\n", .{config_path_});
@memcpy(config_buf[0..config_path_.len], config_path_);
config_buf[config_path_.len] = 0;
config_path = config_buf[0..config_path_.len :0];
- } else {
- if (ctx.args.absolute_working_dir == null) {
- var secondbuf: [bun.MAX_PATH_BYTES]u8 = undefined;
- var cwd = std.os.getcwd(&secondbuf) catch return;
- ctx.args.absolute_working_dir = try allocator.dupe(u8, cwd);
+
+ var success = loadConfigPath(allocator, false, config_path, ctx, comptime cmd) catch false;
+ if (success) {
+ Output.debug("loaded user-specified file\n", .{});
+ defer ctx.debug.loaded_bunfig = true;
+ return;
+ } else {
+ Output.debug("failed to load user-specified file\n", .{});
}
+ }
+
+ // var config_path_: []const u8 = user_config_path_ orelse "";
+ // var config_path: [:0]u8 = undefined;
+ // var auto_loaded: bool = false;
+
+ Output.debug("cmd: {any}\n", .{cmd});
+ Output.debug("config: {any}\n", .{ctx.positionals});
+ var should_load_config = Command.Tag.always_loads_config.get(cmd) or
+ (cmd == .AutoCommand and
+ (ctx.positionals.len == 0 or ctx.positionals.len > 0 and options.defaultLoaders.has(std.fs.path.extension(ctx.positionals[0])))) or (cmd == .RunCommand and
+ (ctx.positionals.len == 2 and options.defaultLoaders.has(std.fs.path.extension(ctx.positionals[1]))));
+
+ // if (config_path_.len == 0) return;
+
+ // skip reading config
+ if (!should_load_config) {
+ Output.debug("not loading config\n", .{});
+ return;
+ }
+
+ // set absolute_working_dir
+ if (ctx.args.absolute_working_dir == null) {
+ var secondbuf: [bun.MAX_PATH_BYTES]u8 = undefined;
+ var cwd = std.os.getcwd(&secondbuf) catch return;
+ ctx.args.absolute_working_dir = try allocator.dupe(u8, cwd);
+ }
+
+ // load from user-specified path
+ Output.debug("config_path_: {s}\n", .{config_path_});
+ if (config_path_.len != 0) {
+ Output.debug("loading from path: {s}\n", .{config_path_});
var parts = [_]string{ ctx.args.absolute_working_dir.?, config_path_ };
config_path_ = resolve_path.joinAbsStringBuf(
ctx.args.absolute_working_dir.?,
@@ -334,9 +385,61 @@ pub const Arguments = struct {
);
config_buf[config_path_.len] = 0;
config_path = config_buf[0..config_path_.len :0];
+ var success = loadConfigPath(allocator, true, config_path, ctx, comptime cmd) catch false;
+ if (success) {
+ Output.debug("loaded user-specified file\n", .{});
+ defer ctx.debug.loaded_bunfig = true;
+ return;
+ } else {
+ Output.debug("failed to load user-specified file\n", .{});
+ }
+ }
+
+ var parts: [2]string = undefined;
+
+ // load bun.json
+ parts = .{ ctx.args.absolute_working_dir.?, "bun.json" };
+ config_path_ = resolve_path.joinAbsStringBuf(
+ ctx.args.absolute_working_dir.?,
+ &config_buf,
+ &parts,
+ .auto,
+ );
+ config_buf[config_path_.len] = 0;
+ config_path = config_buf[0..config_path_.len :0];
+ Output.debug("loading local bun.json\n", .{});
+ var bun_json_loaded = loadConfigPath(allocator, true, config_path, ctx, comptime cmd) catch false;
+
+ if (bun_json_loaded) {
+ Output.debug("loaded local bun.json\n", .{});
+ defer ctx.debug.loaded_bunfig = true;
+ return;
+ } else {
+ Output.debug("failed to load bun.json\n", .{});
}
- try loadConfigPath(allocator, auto_loaded, config_path, ctx, comptime cmd);
+ // load bunfig.toml
+ parts = .{ ctx.args.absolute_working_dir.?, "bunfig.toml" };
+ config_path_ = resolve_path.joinAbsStringBuf(
+ ctx.args.absolute_working_dir.?,
+ &config_buf,
+ &parts,
+ .auto,
+ );
+ config_buf[config_path_.len] = 0;
+ config_path = config_buf[0..config_path_.len :0];
+ // try to load bunfig.toml
+ Output.debug("loading local bunfig.toml\n", .{});
+ var bunfig_loaded = loadConfigPath(allocator, true, config_path, ctx, comptime cmd) catch false;
+ if (bunfig_loaded) {
+ Output.debug("loaded local bunfig.toml\n", .{});
+ defer ctx.debug.loaded_bunfig = true;
+ return;
+ } else {
+ Output.debug("failed to load bunfig\n", .{});
+ }
+
+ return;
}
pub fn loadConfigWithCmdArgs(
@@ -1491,7 +1594,13 @@ pub const Command = struct {
if (extension.len > 0) {
if (!ctx.debug.loaded_bunfig) {
- try bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", &ctx, .RunCommand);
+ // get absolute path to bunfig.toml
+ // var config_path
+
+ var success = Arguments.loadConfigPath(ctx.allocator, true, "bun.json", &ctx, .RunCommand) catch false;
+ if (!success) {
+ _ = Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", &ctx, .RunCommand) catch false;
+ }
}
if (ctx.preloads.len > 0)
@@ -1600,7 +1709,8 @@ pub const Command = struct {
var absolute_script_path = bun.getFdPath(file.handle, &script_name_buf) catch return false;
if (!ctx.debug.loaded_bunfig) {
- bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", ctx, .RunCommand) catch {};
+ var success = Arguments.loadConfigPath(ctx.allocator, true, "bun.json", ctx, .RunCommand) catch false;
+ if (!success) _ = Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", ctx, .RunCommand) catch false;
}
BunJS.Run.boot(
diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig
index ba05e1ddf..2fab80fc9 100644
--- a/src/cli/run_command.zig
+++ b/src/cli/run_command.zig
@@ -933,7 +933,9 @@ pub const RunCommand = struct {
// once we know it's a file, check if they have any preloads
if (ext.len > 0 and !has_loader) {
if (!ctx.debug.loaded_bunfig) {
- try bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", &ctx, .RunCommand);
+ _ = bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bun.json", &ctx, .RunCommand) catch brk: {
+ break :brk bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", &ctx, .RunCommand) catch false;
+ };
}
if (ctx.preloads.len == 0)
diff --git a/src/install/install.zig b/src/install/install.zig
index d444b62fc..db7a3aae3 100644
--- a/src/install/install.zig
+++ b/src/install/install.zig
@@ -5722,7 +5722,7 @@ pub const PackageManager = struct {
"Possible values: \"hardlink\" (default), \"symlink\", \"copyfile\"";
const install_params_ = [_]ParamType{
- clap.parseParam("-c, --config <STR>? Load config (bunfig.toml)") catch unreachable,
+ clap.parseParam("-c, --config <STR> Load config (bun.json)") catch unreachable,
clap.parseParam("-y, --yarn Write a yarn.lock file (yarn v1)") catch unreachable,
clap.parseParam("-p, --production Don't install devDependencies") catch unreachable,
clap.parseParam("--no-save Don't save a lockfile") catch unreachable,