Bun

Bun v1.0.14


Ashcon Partovi ยท November 22, 2023

Bun v1.0.14 introduces Bun.Glob, a fast API for matching files and strings using glob patterns. It also fixes a race condition when extracting dependencies during bun install, improves TypeScript module resolution in node_modules, and makes error messages easier to read.

Bun is an incredibly fast JavaScript runtime, bundler, transpiler, and package manager โ€” all in one. In case you missed it, here are some of the recent changes to Bun:

  • v1.0.9 - Fixes a glibc symbol version error, illegal instruction error, a Bun.spawn bug, an edgecase with peer dependency installs, and a JSX transpiler bugfix
  • v1.0.10 - Fixes 14 bugs (addressing 102 ๐Ÿ‘ reactions), node:http gets 14% faster, stability improvements to Bun for Linux ARM64, bun install bugfix, and node:http bugfixes
  • v1.0.11 - Fixes 5 bugs, adds Bun.semver, fixes a bug in bun install and fixes bugs impacting astro and @google-cloud/storage
  • v1.0.12 - Adds bun -e for evaluating scripts, bun --env-file for loading environment variables, server.url, import.meta.env, expect.unreachable(), improved CLI help output, and more
  • v1.0.13 - Fixes 6 bugs (addressing 317 ๐Ÿ‘ reactions). 'http2' module & gRPC.js work now. Vite 5 & Rollup 4 work. Implements process.report.getReport(), improves support for ES5 'with' statements, fixes a regression in bun install, fixes a crash when printing exceptions, fixes a Bun.spawn bug, and fixes a peer dependencies bug

To install Bun:

curl
npm
brew
docker
curl
curl -fsSL https://bun.sh/install | bash
npm
npm install -g 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

Bun.Glob

Bun.Glob is a fast API for matching files and strings using glob patterns.

It's similar to popular Node.js libraries like fast-glob or micromatch, except it matches strings 3x faster.

Use glob.scan() to scan for files that match the glob, which returns an AsyncIterator that yields matching paths.

import { Glob } from "bun";

const glob = new Glob("**/*.ts");
for await (const path of glob.scan("src")) {
  console.log(path); // "src/index.ts"
}

Use glob.match() to match a string against a glob pattern.

import { Glob } from "bun";

const glob = new Glob("**/*.ts");

const match = glob.match("src/index.ts");
console.log(match); // true

Thanks to @zackradisic for implementing this! Also, thanks to @mrmlnc for fast-glob along with the contributors to micromatch & picomatch, which our tests were inspired by. Much of our Zig implementation was inspired by Devon Govett (@devongovett) and Stephen Gregoratto (@The-King-of-Toasters)'s glob implementations, which we are grateful for.

Fixed bug with missing dependencies using bun install

Bun had a race condition that could cause bun install to occasionally fail to install a dependency when many different versions of the same package were being extracted concurrently. This was caused by a bug in how Bun created the temporary filename for installing dependencies. The path used was not unique enough, which could cause one build to overwrite another build's temporary folder, sometimes.

TypeScript module resolution changes in node_modules

To mimick tsc behavior, Bun loads TypeScript files before JavaScript files. This works well in local development, but sometimes npm packages ship TypeScript source files that transpilers cannot transpile, such as:

  • TypeScript transformer plugins.
  • Using import {Foo} instead of import {type Foo}.
  • Using export {Foo} instead of export {type Foo}.

Going forward, when inside node_modules, Bun will prefer JavaScript files over TypeScript files when both are present.

This fixes errors that looked like:

SyntaxError: Indirectly exported binding name 'Foo' is not found.

Bun continues to support TypeScript files in node_modules. When both .ts and .js files are present, Bun will prefer the .js file when inside node_modules. Otherwise, Bun continues to prefer .ts files.

More readable error messages when a build fails

Bun now shows better error messages when a build fails:

  • Fixed a bug where it only highlighed the first character
  • Trimmed extraneous newlines in the error message
  • Made the styling for build errors match the styling for runtime errors

Better export & import error messages


Errors for unsupported ESM <> CommonJS features

Bun supports both require() and import in the same file, but you cannot use import and module.exports in the same file. You also cannot use exports, module, with, or top-level return.

Previously, Bun relied on a runtime error message from JavaScriptCore for this, but the error message was a little confusing.

Now, these errors are detected at build-time and show a better error message. In this example, you cannot use import and with in the same file:

import "abc";

with (Bun) {
  write("/tmp/file.txt", escapeHTML(file("/tmp/file.html").text()));
  console.log({
    contents: file("/tmp/file.txt").text(),
  });
}

Before:

1 | import "abc";
2 |
3 | with (Bun) {
   ^
SyntaxError: Unexpected string literal "abc". import call expects one or two arguments.
      at unsupported.js:3:0

After:

1 | import "abc";
           ^
error: Cannot use import statement with CommonJS-only features
    at unsupported.js:1:8 7

note: Try require("abc") instead
note: This file is CommonJS because a "with" statement is used

Class name shown in console.log

console.log() now shows the class name of the object in more cases, which matches the behavior of Node.js.

class Foo {}
class Bar {
  value = 1;
}

console.log(new Foo()); // Foo {}
console.log(new Bar()); // Bar { value: 1 }

Thanks to @JibranKalia for implementing this!

Fixed signal using process.kill()

A bug was fixed where SIGKILL and SIGSTOP were not supported by process.kill().

process.kill("SIGKILL"); // or "SIGSTOP"

If you encountered the following error, it is now fixed:

RangeError: Unknown signal name

Thanks to @antongolub for fixing this bug!

If you want to see the full changelog, you can find it here.

Thanks to 7 contributors!