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/WebKit
Submodule WebKit updated 1 files
+0 −2 Dockerfile
8 changes: 3 additions & 5 deletions src/bun.js/api/bun/subprocess.zig
Expand Up @@ -1688,7 +1688,6 @@ pub const Subprocess = struct {
}

if (args != .zero and args.isObject()) {

// This must run before the stdio parsing happens
if (args.getTruthy(globalThis, "ipc")) |val| {
if (val.isCell() and val.isCallable(globalThis.vm())) {
Expand Down Expand Up @@ -1848,9 +1847,8 @@ pub const Subprocess = struct {
}

if (!override_env and env_array.items.len == 0) {
env_array.items = jsc_vm.bundler.env.map.createNullDelimitedEnvMap(allocator) catch |err|
env_array = jsc_vm.bundler.env.map.createEnvArrayList(allocator) catch |err|
return globalThis.handleError(err, "in Bun.spawn");
env_array.capacity = env_array.items.len;
}

inline for (0..stdio.len) |fd_index| {
Expand Down Expand Up @@ -1949,8 +1947,8 @@ pub const Subprocess = struct {

var spawned = switch (bun.spawn.spawnProcess(
&spawn_options,
@ptrCast(argv.items.ptr),
@ptrCast(env_array.items.ptr),
@ptrCast(argv.items[0 .. argv.items.len - 1 :null].ptr),
@ptrCast(env_array.items[0 .. env_array.items.len - 1 :null].ptr),
) catch |err| {
process_allocator.destroy(subprocess);
spawn_options.deinit();
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/event_loop.zig
Expand Up @@ -2172,7 +2172,7 @@ pub const EventLoopHandle = union(enum) {
this.loop().unref();
}

pub inline fn createNullDelimitedEnvMap(this: @This(), alloc: Allocator) ![:null]?[*:0]u8 {
pub inline fn createNullDelimitedEnvMap(this: @This(), alloc: Allocator) !bun.DotEnv.Map.NullDelimitedEnvMap {
return switch (this) {
.js => this.js.virtual_machine.bundler.env.map.createNullDelimitedEnvMap(alloc),
.mini => this.mini.env.?.map.createNullDelimitedEnvMap(alloc),
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/javascript.zig
Expand Up @@ -769,8 +769,8 @@ 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| {
this.initIPCInstance(kv.value);
} 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
6 changes: 3 additions & 3 deletions src/bun.js/module_loader.zig
Expand Up @@ -183,7 +183,7 @@ fn dumpSourceString(specifier: string, written: []const u8) void {
},
};
const dir = std.fs.cwd().makeOpenPath(base_name, .{}) catch |e| {
Output.debug("Failed to dump source string: {}", .{e});
Output.debugWarn("Failed to dump source string: {}", .{e});
return;
};
BunDebugHolder.dir = dir;
Expand All @@ -196,12 +196,12 @@ fn dumpSourceString(specifier: string, written: []const u8) void {
.windows => bun.path.windowsFilesystemRoot(dir_path).len,
};
var parent = dir.makeOpenPath(dir_path[root_len..], .{}) catch |e| {
Output.debug("Failed to dump source string: makeOpenPath({s}[{d}..]) {}", .{ dir_path, root_len, e });
Output.debugWarn("Failed to dump source string: makeOpenPath({s}[{d}..]) {}", .{ dir_path, root_len, e });
return;
};
defer parent.close();
parent.writeFile(std.fs.path.basename(specifier), written) catch |e| {
Output.debug("Failed to dump source string: writeFile {}", .{e});
Output.debugWarn("Failed to dump source string: writeFile {}", .{e});
return;
};
} else {
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
5 changes: 4 additions & 1 deletion src/cli/bunx_command.zig
Expand Up @@ -602,10 +602,13 @@ pub const BunxCommand = struct {
debug("installing package: {s}", .{bun.fmt.fmtSlice(argv_to_use, " ")});
this_bundler.env.map.put("BUN_INTERNAL_BUNX_INSTALL", "true") catch bun.outOfMemory();

const envp = try this_bundler.env.map.createNullDelimitedEnvMap(bun.default_allocator);
defer envp.deinit(bun.default_allocator);

const spawn_result = switch ((bun.spawnSync(&.{
.argv = argv_to_use,

.envp = try this_bundler.env.map.createNullDelimitedEnvMap(bun.default_allocator),
.envp = envp.envp,

.cwd = bunx_cache_dir,
.stderr = .inherit,
Expand Down
45 changes: 14 additions & 31 deletions src/cli/run_command.zig
Expand Up @@ -347,13 +347,14 @@ pub const RunCommand = struct {
Output.flush();
}

const envp = try env.map.createNullDelimitedEnvMap(bun.default_allocator);
defer envp.deinit(bun.default_allocator);

const spawn_result = switch ((bun.spawnSync(&.{
.argv = &argv,
.argv0 = shell_bin.ptr,

// TODO: remember to free this when we add --filter or --concurrent
// in the meantime we don't need to free it.
.envp = try env.map.createNullDelimitedEnvMap(bun.default_allocator),
.envp = envp.envp,

.cwd = cwd,
.stderr = .inherit,
Expand Down Expand Up @@ -513,14 +514,15 @@ pub const RunCommand = struct {
argv = try array_list.toOwnedSlice();
}

const envp = try env.map.createNullDelimitedEnvMap(bun.default_allocator);
defer envp.deinit(bun.default_allocator);

const silent = ctx.debug.silent;
const spawn_result = bun.spawnSync(&.{
.argv = argv,
.argv0 = executableZ,

// TODO: remember to free this when we add --filter or --concurrent
// in the meantime we don't need to free it.
.envp = try env.map.createNullDelimitedEnvMap(bun.default_allocator),
.envp = envp.envp,

.cwd = cwd,
.stderr = .inherit,
Expand Down Expand Up @@ -850,7 +852,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 +878,10 @@ 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;
}
}

// 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 {};
if (this_bundler.env.isProduction())
paperdave marked this conversation as resolved.
Show resolved Hide resolved
this_bundler.options.production = true;

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 {};
}
}
this_bundler.runEnvLoader(true) catch {};
}

this_bundler.env.map.putDefault("npm_config_local_prefix", this_bundler.fs.top_level_dir) catch unreachable;
Expand Down Expand Up @@ -1055,11 +1041,8 @@ pub const RunCommand = struct {
{
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())
this_bundler.options.production = true;
}

const ResultList = bun.StringArrayHashMap(void);
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