diff options
author | 2022-03-02 03:06:59 -0800 | |
---|---|---|
committer | 2022-03-02 03:06:59 -0800 | |
commit | 4b36efd50ad27f7b390ca38b2f968e1dfcdbe4f1 (patch) | |
tree | 34f140ae17c9ba6a45adb9fc896d113baba64ddb | |
parent | c3b96c90d3972da4d5a8fd306d96e97352f593d0 (diff) | |
download | bun-4b36efd50ad27f7b390ca38b2f968e1dfcdbe4f1.tar.gz bun-4b36efd50ad27f7b390ca38b2f968e1dfcdbe4f1.tar.zst bun-4b36efd50ad27f7b390ca38b2f968e1dfcdbe4f1.zip |
[bun run] Set more environment variables
-rw-r--r-- | src/cli/run_command.zig | 31 | ||||
-rw-r--r-- | src/env_loader.zig | 24 |
2 files changed, 55 insertions, 0 deletions
diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig index 15040f69c..8a8451cea 100644 --- a/src/cli/run_command.zig +++ b/src/cli/run_command.zig @@ -775,6 +775,35 @@ pub const RunCommand = struct { PATH = new_path.items; } + this_bundler.env.loadNodeJSConfig(this_bundler.fs) catch {}; + this_bundler.env.map.putDefault("npm_config_local_prefix", this_bundler.fs.top_level_dir) catch unreachable; + if (this_bundler.env.get("BUN_INSTALL")) |bun_install| { + this_bundler.env.map.putDefault("npm_config_prefix", bun_install) catch unreachable; + } + + // we have no way of knowing what version they're expecting without running the node executable + // running the node executable is too slow + // so we will just hardcode it to LTS + this_bundler.env.map.putDefault( + "npm_config_user_agent", + // the use of npm/? is copying yarn + // e.g. + // > "yarn/1.22.4 npm/? node/v12.16.3 darwin x64", + "bun/" ++ Global.package_json_version ++ "npm/? node/v16.14.0 " ++ Global.os_name ++ " " ++ Global.arch_name, + ) catch unreachable; + + if (this_bundler.env.get("npm_execpath") == null) { + // we don't care if this fails + if (std.fs.selfExePathAlloc(ctx.allocator)) |self_exe_path| { + this_bundler.env.map.putDefault("npm_execpath", self_exe_path) catch unreachable; + if (strings.lastIndexOf(self_exe_path, std.fs.path.sep_str ++ "bin" ++ std.fs.path.sep_str)) |bin_dir_i| { + this_bundler.env.map.putDefault("npm_config_prefix", std.fs.path.dirname(self_exe_path[0..bin_dir_i]) orelse "/") catch unreachable; + } else { + this_bundler.env.map.putDefault("npm_config_prefix", std.fs.path.dirname(self_exe_path) orelse "/") catch unreachable; + } + } else |_| {} + } + var did_print = false; if (root_dir_info.enclosing_package_json) |package_json| { if (package_json.name.len > 0) { @@ -783,6 +812,8 @@ pub const RunCommand = struct { } } + this_bundler.env.map.putDefault("npm_package_json", package_json.source.path.text) catch unreachable; + if (package_json.version.len > 0) { if (this_bundler.env.map.get(NpmArgs.package_version) == null) { this_bundler.env.map.put(NpmArgs.package_version, package_json.version) catch unreachable; diff --git a/src/env_loader.zig b/src/env_loader.zig index 737e3d92b..b75d47cec 100644 --- a/src/env_loader.zig +++ b/src/env_loader.zig @@ -15,6 +15,7 @@ const CodepointIterator = @import("./string_immutable.zig").CodepointIterator; const Analytics = @import("./analytics/analytics_thread.zig"); const Fs = @import("./fs.zig"); const Api = @import("./api/schema.zig").Api; +const which = @import("./which.zig").which; const Variable = struct { key: string, value: string, @@ -396,6 +397,29 @@ pub const Loader = struct { const empty_string_value: string = "\"\""; + pub fn getNodePath(this: *Loader, fs: *Fs.FileSystem, buf: *Fs.PathBuffer) ?[:0]const u8 { + if (this.get("NODE") orelse this.get("npm_node_execpath")) |node| { + @memcpy(buf, node.ptr, node.len); + buf[node.len] = 0; + return buf[0..node.len :0]; + } + + if (which(buf, this.map.get("PATH") orelse return null, fs.top_level_dir, "node")) |node| { + return node; + } + + return null; + } + + pub fn loadNodeJSConfig(this: *Loader, fs: *Fs.FileSystem) !void { + var buf: Fs.PathBuffer = undefined; + + var node = this.getNodePath(fs, &buf) orelse return; + var cloned = try fs.dirname_store.append([]const u8, std.mem.span(node)); + try this.map.put("NODE", cloned); + try this.map.put("npm_node_execpath", cloned); + } + pub fn get(this: *const Loader, key: string) ?string { var _key = key; if (_key.len > 0 and _key[0] == '$') { |