Bun

Bun v1.1.7


Jarred Sumner ยท May 3, 2024

Bun is an incredibly fast JavaScript runtime, bundler, transpiler, and package manager โ€” all in one.

Bun v1.1.7 is here! This release fixes 28 bugs (addressing 11 ๐Ÿ‘). Glob workspace names in bun install. Asymmetric matcher support in expect.extends() equals. bunx --version. Bugfixes to JSX transpilation, sourcemaps, cross-compilation of standalone executables, bun shell, RegExp, Worker on Windows, and Node.js compatibility improvements.

We're hiring systems engineers in San Francisco to build the future of JavaScript!

Previous releases

  • v1.1.6 fixes 10 bugs (addressing 512 ๐Ÿ‘ reactions). We've implemented UDP socket support & node:dgram. DataDog and ClickHouseDB now work. We've fixed a regression in node:http from v1.1.5. There are also Node.js compatibility improvements and bugfixes.
  • v1.1.5 fixes 64 bugs (addressing 101 ๐Ÿ‘). bun build --compile cross-compile standalone JavaScript & TypeScript executables to other platforms. import any file as text via the type: "text" import attribute. Introduces a new crash reporter. package.json doesn't error with comments or trailing commas. Fixes a bug where bun run --filter exited with 0. Fixes a bug in bun install with file: dependencies. Fixes bugs in node:fs, node:tls, node:crypto, node:readline, node:http, node:worker_threads
  • v1.1.0 Bundows. Windows support is here!

To install Bun:

curl
npm
powershell
scoop
brew
docker
curl
curl -fsSL https://bun.sh/install | bash
npm
npm install -g bun
powershell
powershell -c "irm bun.sh/install.ps1|iex"
scoop
scoop install bun
brew
brew tap oven-sh/bun
brew install bun
docker
docker pull oven/bun
docker run --rm --init --ulimit memlock=-1:-1 oven/bun

To upgrade Bun:

bun upgrade

Glob workspace names in bun install

You can now use advanced glob patterns in workspaces in package.json to specify which directories to include in a monorepo. For example:

{
  "name": "monorepo",
  "workspaces": ["{app,packages,lib}/**"]
}

Previously, we only supported /* as a pattern or exact paths in workspaces. workspaces matching is now powered by Bun.Glob, so you can use any glob pattern supported by Bun.Glob.

Thanks to @zackradisic for implementing this feature!

Asymmetric matcher support in expect.extends() equals

You can now use asymmetric matchers within when using this.equals inside expect.extends() equals. For example:

import { test, expect } from "bun:test";
expect.extend({
  toCustomEqual(actual, expected) {
    return { pass: this.equals(actual, expected) };
  },
});

test("asymmetric matchers", () => {
  expect(1).toCustomEqual(expect.anything());
  expect(1).toCustomEqual(expect.any(Number));
});

Previously, this would throw an error. Now, it works as expected.

Thanks to @anchan828 for implementing this feature!

bunx --version

When you run bunx --version, Bun now prints the version number:

bunx --version
1.1.7

Previously, bunx --version would print the help menu:

bunx --version
Usage: bunx [...flags] <package>[@version] [...flags and arguments]
Execute an npm package executable (CLI), automatically installing into a global shared cache if not installed in node_modules.

Flags:
  --bun      Force the command to run with Bun instead of Node.js

Examples:
  bunx prisma migrate
  bunx prettier foo.js
  bunx --bun vite dev foo.js

Thanks to @Electroid for fixing this!

WebSocket close code 1015

When a WebSocket client connection fails to connect due to a TLS error, the close code is now 1015 instead of 1006. This is more accurate and helps with debugging.

const ws = new WebSocket("wss://localhost:3000");

ws.on("close", ({ code, reason }) => {
  console.log(code); // 1015
  console.log(reason); // "TLS handshake failed"
});

Previously, the close code was 1006, which is a generic error code.

Unhandled errors now print the Bun version

When a top-level error is thrown or promise is rejected, Bun now prints the Bun version at the end:

1 | function hi() {
2 |   return 42;
3 | }
4 |
5 | function yo() {
6 |   throw new Error("uh oh!");
            ^
error: uh oh!
      at yo (error.js:6:9)
      at hey (error.js:11:3)
      at error.js:14:1

Bun v1.1.7 (macOS arm64)

This is useful when asking people for debugging help because it saves one question: "What version of Bun are you using?"

Better error message in Bun.build()

Fixed: React "Duplicate key" warning in development

Fixed: hang in Bun Shell seq, basename, dirname with empty output

When seq, basename, or dirname were called with empty output in Bun Shell, it would hang indefinitely due to not closing the output stream. This has been fixed in Bun v1.1.7, thanks to @RanoIP.

Fixed: Potential crash on start on Windows

An invalid memory access in the SIMD codepath within the Zig standard library function std.mem.indexOfScalarPos could lead to Bun crashing immediately during initialization on Windows. This has been fixed in Bun v1.1.7.

Additionally, we've added an optimization to code which searches for sentinel values in pointers to use the Windows wcslen and strlen functions where appropriate, instead of a slower handrolled implementation.

Thanks to @dylan-conway for fixing this!

Fixed: Potential crash after process spawn

A crash that sometimes occurred when a spawned subprocess gets garbage collected before the stdin stream gets collected has been fixed in Bun v1.1.7.

Thanks to @gvilums for fixing this!

Fixed: Crash while exiting with Worker

A crash that could occur when Bun exits while a Worker is still running has been fixed in Bun v1.1.7. This crash was caused by threadlocal destructors running for each thread during std::quick_exit.

Thanks to @dylan-conway for fixing this!

Fixed: Crash when reading directories on Windows

When reading directories on Windows, Bun was assuming that the FILE_DIRECTORY_INFORMATION struct in the NT API was 8 bytes aligned, per the Windows API documentation. However, runtime safety checks proved this to be false. We have relaxed the alignment requirement for this struct in our code and this crash should no longer occur.

Fixed: Crash when out of memory on Windows

When the mimalloc memory allocator returned NULL due to running out of memory, Bun would crash due to an assertion failure like mimalloc: allocated size is too small. This assertion failure was only meant for development and testing, not for a release build (we enable assertions by default on Windows however).

This has been fixed in Bun v1.1.7.

This could most easily be reproduced by uploading a large file to a Bun.serve() server when the system was very low on memory or simulating low memory.

Fixed: Crash when WebSocket client has connection error on Windows

A crash that could occur when a WebSocket client connection fails to connect on Windows has been fixed. We've added a test to prevent this from regressing in the future.

Fixed: UDP socket now throws on valid port number

In Bun v1.1.6 (the previous release), we introduced Bun.udpSocket for UDP socket support.

Previously, all port numbers which weren't between 1 and 65535 would be treated as 0, and a randomly assigned port would be used. Now, Bun.udpSocket throws an error if the provided port number is invalid.

Thanks to @gvilums for fixing this!

Fixed: Stacktrace with incorrect line numbers

One bug causing stacktraces to sometimes show incorrect line numbers has been fixed in Bun v1.1.7 (note: there could be other bugs causing this issue).

This bug was caused by an uninitialized integer in C++ code. We are adding clang-analyzer to Bun's CI to prevent this from happening again.

Fixed: sourcemap regression causing hang in bun build

A sourcemap regression introduced in Bun v1.1.6 caused bun build to hang indefinitely. This has been fixed in Bun v1.1.7, and we've added an integration test to prevent this from regressing in the future.

Using bun build --sourcemap=external on a "hello world" express.js app like this would hang:

const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.send("Hello World!");
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

We've added an integration test that ensures output from bun build --compile --minify can successfully run a simple express.js app.

Fixed: Cross-compilation of executables to & from Windows

Cross-compilation <> Windows had a filepath bug where we were assuming if the current platform was Windows or posix to use the file path prefix for that platform, instead of the cross-compilation target's platform. This bug was fixed, thanks to @gvilums.

Fixed: memory leak regression in unread server-side requests bodies

In Bun v1.0.30 - Bun v1.1.6, unread incoming HTTP request bodies to Bun.serve() were not being cleaned up properly. This also applied to node:http servers.

This meant that choosing not to read the incoming request's body could consume memory indefinitely. This has been fixed in Bun v1.1.7, thanks to @cirospacari, and we've added regression tests to prevent this from happening again.

Fixed: fs.fdatasync missing fd in Error

When an error was thrown in fs.fdatasync, the fd property was missing from the error object. This has been fixed in Bun v1.1.7, thanks to @nektro.

Fixed: custom inspect exception

When a custom inspect function threw an exception, Bun would return [native code]. This was confusing. We wish we could change this to throw an error, but that would be a breaking change. Instead, we've made it print [custom formatter threw an exception] instead, which is slightly better but not what we'd prefer. In a future version of Bun, we will change this to throw an error but we do not want your code to potentially randomly break in a minor version update.

ServerWebSocket micro-optimization

The in-memory size of a ServerWebSocket representation in Zig has shrunk from 32 bytes to 24 bytes. You probably won't notice the memory savings, but it might make some functions run faster due to better cache locality.

console.log with custom inspect fn gets 8% faster

Thank you to 17 contributors!