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

tidy: clean up code for env loader #9642

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/bun.js/javascript.zig
Expand Up @@ -770,7 +770,7 @@ pub const VirtualMachine = struct {
if (map.map.fetchSwapRemove("BUN_INTERNAL_IPC_FD")) |kv| {
if (Environment.isWindows) {
this.initIPCInstance(kv.value.value);
} else if (std.fmt.parseInt(i32, kv.value.value, 10) catch null) |fd| {
} else if (std.fmt.parseInt(i32, kv.value, 10) catch null) |fd| {
this.initIPCInstance(bun.toFD(fd));
} else {
Output.printErrorln("Failed to parse BUN_INTERNAL_IPC_FD", .{});
Expand Down
9 changes: 3 additions & 6 deletions src/bun_js.zig
Expand Up @@ -133,7 +133,7 @@ pub const Run = struct {
try @import("./bun.js/config.zig").configureTransformOptionsForBunVM(ctx.allocator, ctx.args),
null,
);
try bundle.runEnvLoader();
try bundle.runEnvLoader(false);
const mini = JSC.MiniEventLoop.initGlobal(bundle.env);
mini.top_level_dir = ctx.args.absolute_working_dir orelse "";
return try bun.shell.Interpreter.initAndRunFromFile(mini, entry_path);
Expand All @@ -147,7 +147,7 @@ pub const Run = struct {
try bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", &ctx, .RunCommand);
}

if (strings.endsWithComptime(entry_path, comptime if (Environment.isWindows) ".sh" else ".bun.sh")) {
if (strings.endsWithComptime(entry_path, ".sh")) {
const exit_code = try bootBunShell(&ctx, entry_path);
Global.exitWide(exit_code);
return;
Expand Down Expand Up @@ -226,10 +226,7 @@ pub const Run = struct {
const node_env_entry = try b.env.map.getOrPutWithoutValue("NODE_ENV");
if (!node_env_entry.found_existing) {
node_env_entry.key_ptr.* = try b.env.allocator.dupe(u8, node_env_entry.key_ptr.*);
node_env_entry.value_ptr.* = .{
.value = try b.env.allocator.dupe(u8, "development"),
.conditional = false,
};
node_env_entry.value_ptr.* = try b.env.allocator.dupe(u8, "development");
}

b.configureRouter(false) catch {
Expand Down
10 changes: 5 additions & 5 deletions src/bundler.zig
Expand Up @@ -520,7 +520,7 @@ pub const Bundler = struct {
bundler.configureLinkerWithAutoJSX(true);
}

pub fn runEnvLoader(this: *Bundler) !void {
pub fn runEnvLoader(this: *Bundler, is_script_runner: bool) !void {
switch (this.options.env.behavior) {
.prefix, .load_all, .load_all_without_inlining => {
// Step 1. Load the project root.
Expand All @@ -541,11 +541,11 @@ pub const Bundler = struct {
}

if (this.options.isTest() or this.env.isTest()) {
try this.env.load(dir, this.options.env.files, .@"test");
try this.env.load(dir, this.options.env.files, .@"test", is_script_runner);
} else if (this.options.production) {
try this.env.load(dir, this.options.env.files, .production);
try this.env.load(dir, this.options.env.files, .production, is_script_runner);
} else {
try this.env.load(dir, this.options.env.files, .development);
try this.env.load(dir, this.options.env.files, .development, is_script_runner);
}
},
.disable => {
Expand Down Expand Up @@ -584,7 +584,7 @@ pub const Bundler = struct {
this.options.env.prefix = "BUN_";
}

try this.runEnvLoader();
try this.runEnvLoader(false);

this.options.jsx.setProduction(this.env.isProduction());

Expand Down
32 changes: 13 additions & 19 deletions src/cli/run_command.zig
Expand Up @@ -850,7 +850,7 @@ pub const RunCommand = struct {

this_bundler.configureLinker();

var root_dir_info = this_bundler.resolver.readDirInfo(this_bundler.fs.top_level_dir) catch |err| {
const root_dir_info = this_bundler.resolver.readDirInfo(this_bundler.fs.top_level_dir) catch |err| {
if (!log_errors) return error.CouldntReadCurrentDirectory;
if (Output.enable_ansi_colors) {
ctx.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
Expand All @@ -876,26 +876,20 @@ pub const RunCommand = struct {
if (env == null) {
this_bundler.env.loadProcess();

if (this_bundler.env.get("NODE_ENV")) |node_env| {
if (strings.eqlComptime(node_env, "production")) {
this_bundler.options.production = true;
}
}
if (this_bundler.env.isProduction())
paperdave marked this conversation as resolved.
Show resolved Hide resolved
this_bundler.options.production = true;

// TODO: evaluate if we can skip running this in nested calls to bun run
// The reason why it's unclear:
// - Some scripts may do NODE_ENV=production bun run foo
// This would cause potentially a different .env file to be loaded
this_bundler.runEnvLoader() catch {};
this_bundler.runEnvLoader(true) catch {};

if (root_dir_info.getEntries(0)) |dir| {
// Run .env again if it exists in a parent dir
if (this_bundler.options.production) {
this_bundler.env.load(dir, this_bundler.options.env.files, .production) catch {};
} else {
this_bundler.env.load(dir, this_bundler.options.env.files, .development) catch {};
}
}
// sus
// if (root_dir_info.getEntries(0)) |dir| {
// // Run .env again if it exists in a parent dir
// if (this_bundler.options.production) {
// this_bundler.env.load(dir, this_bundler.options.env.files, .production, true) catch {};
// } else {
// this_bundler.env.load(dir, this_bundler.options.env.files, .development, true) catch {};
// }
// }
}

this_bundler.env.map.putDefault("npm_config_local_prefix", this_bundler.fs.top_level_dir) catch unreachable;
Expand Down
5 changes: 1 addition & 4 deletions src/cli/test_command.zig
Expand Up @@ -663,10 +663,7 @@ pub const TestCommand = struct {
const node_env_entry = try env_loader.map.getOrPutWithoutValue("NODE_ENV");
if (!node_env_entry.found_existing) {
node_env_entry.key_ptr.* = try env_loader.allocator.dupe(u8, node_env_entry.key_ptr.*);
node_env_entry.value_ptr.* = .{
.value = try env_loader.allocator.dupe(u8, "test"),
.conditional = false,
};
node_env_entry.value_ptr.* = try env_loader.allocator.dupe(u8, "test");
}

try vm.bundler.configureDefines();
Expand Down