Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/bun_core/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,8 @@ pub fn err_generic(fmt: &str, args: impl FmtTuple) {
#[derive(Clone, Copy)]
pub struct SysErrInfo {
pub tag_name: &'static [u8],
/// The resolved `SystemErrno` discriminant that `tag_name` was derived
/// from, so `coreutils_error_map::get` finds the label for it.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub errno: i32,
pub syscall: &'static str,
}
Expand Down
6 changes: 6 additions & 0 deletions src/js/internal-for-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,12 @@ export const sysErrorNameFromLibuv: (errno: number) => string | undefined = $new
1,
);

export const sysErrorOutputErr: (errno: number, fromLibuv: boolean) => undefined = $newRustFunction(
"sys/Error.rs",
"TestingAPIs.sysErrorOutputErr",
2,
);

export const sigactionLayout: () =>
| undefined
| {
Expand Down
1 change: 1 addition & 0 deletions src/runtime/dispatch_js2native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub use bun_sourcemap_jsc::internal_jsc::testing_to_vlq as sourcemap_internal_so

pub use bun_sys_jsc::error_jsc::TestingAPIs::sigaction_layout as sys_sys_testing_ap_is_sigaction_layout;
pub use bun_sys_jsc::error_jsc::TestingAPIs::sys_error_name_from_libuv as sys_error_testing_ap_is_sys_error_name_from_libuv;
pub use bun_sys_jsc::error_jsc::TestingAPIs::sys_error_output_err as sys_error_testing_ap_is_sys_error_output_err;
pub use bun_sys_jsc::error_jsc::TestingAPIs::translate_nt_status_to_e as sys_sys_testing_ap_is_translate_nt_status_to_e;
pub use bun_sys_jsc::error_jsc::TestingAPIs::translate_uv_error_to_e as sys_sys_testing_ap_is_translate_uv_error_to_e;

Expand Down
17 changes: 13 additions & 4 deletions src/sys/Error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const RETRY_ERRNO: Int = E::EAGAIN as Int;

const TODO_ERRNO: Int = Int::MAX - 1;

/// Printed in place of the errno tag when `errno` decodes to no `SystemErrno`.
const UNKNOWN_TAG_NAME: &[u8] = b"UNKNOWN";

pub(crate) type Int = u16;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -318,8 +321,7 @@ impl Error {

pub fn name(&self) -> &'static [u8] {
self.get_error_code_tag_name()
.map(|(n, _)| n.as_bytes())
.unwrap_or(b"UNKNOWN")
.map_or(UNKNOWN_TAG_NAME, |(n, _)| n.as_bytes())
}

pub fn to_zig_err(&self) -> SystemErrno {
Expand Down Expand Up @@ -520,9 +522,16 @@ impl bun_core::output::ErrName for Error {
Error::name(self)
}
fn as_sys_err_info(&self) -> Option<bun_core::output::SysErrInfo> {
// Resolve once and hand over the `SystemErrno` discriminant: with
// `from_libuv` the stored `errno` is a raw `UV_E*` magnitude, which the
// coreutils label table does not know.
Comment thread
robobun marked this conversation as resolved.
Resolved
let (tag_name, errno) = match self.get_error_code_tag_name() {
Some((name, e)) => (name.as_bytes(), e as i32),
None => (UNKNOWN_TAG_NAME, i32::from(self.errno)),
};
Some(bun_core::output::SysErrInfo {
tag_name: Error::name(self),
errno: i32::from(self.errno),
tag_name,
errno,
syscall: <&'static str>::from(self.syscall),
})
}
Expand Down
32 changes: 32 additions & 0 deletions src/sys_jsc/error_jsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,38 @@ pub mod TestingAPIs {
}
}

/// Prints a `bun.sys.Error` through `Output.err` so tests can read the
/// `ENOENT: No such file or directory: ... (open)` line from stderr. With
/// `fromLibuv` the errno is the negated UV code as node_fs stores it
/// (Windows-only; prints nothing and returns `undefined` elsewhere).
Comment thread
robobun marked this conversation as resolved.
Resolved
#[bun_jsc::host_fn]
pub fn sys_error_output_err(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> {
let arguments = frame.arguments();
if arguments.len() < 2 || !arguments[0].is_number() || !arguments[1].is_boolean() {
return Err(global.throw(format_args!(
"sysErrorOutputErr: expected (number, boolean) arguments"
)));
}
let Ok(errno) = u16::try_from(arguments[0].to_int32()) else {
return Err(global.throw(format_args!("sysErrorOutputErr: errno must fit in a u16")));
};
let from_libuv = arguments[1].as_boolean();
#[cfg(not(windows))]
if from_libuv {
return Ok(JSValue::UNDEFINED);
}
let err = Error {
errno,
syscall: bun_sys::Tag::open,
#[cfg(windows)]
from_libuv,
..Default::default()
};
bun_core::output::err(err, "sysErrorOutputErr", ());
bun_core::output::flush();
Ok(JSValue::UNDEFINED)
}

/// Exposes NTSTATUS -> `bun.sys.E` translation so tests can feed NTSTATUS
/// values that filter drivers and cloud-sync placeholders return in the
/// wild (STATUS_CANNOT_DELETE etc.) and verify they map to a sensible
Expand Down
41 changes: 40 additions & 1 deletion test/js/bun/sys/error-name-from-libuv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,46 @@

import { sysErrorNameFromLibuv } from "bun:internal-for-testing";
import { expect, test } from "bun:test";
import { isWindows } from "harness";
import { bunEnv, bunExe, isWindows } from "harness";

// Runs `Output.err(sys_error, "sysErrorOutputErr", ())` in a child bun. The
// line it prints is on stderr.
async function outputErr(errno: number, fromLibuv: boolean) {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", `require("bun:internal-for-testing").sysErrorOutputErr(${errno}, ${fromLibuv})`],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
return { stdout, stderr, exitCode };
}

test.concurrent("Output.err prints the errno label before the message", async () => {
expect(await outputErr(2, false)).toEqual({
stdout: "",
stderr: "ENOENT: No such file or directory: sysErrorOutputErr (open)\n",
exitCode: 0,
});
});

// The label lookup must use the translated errno. With from_libuv=true the
// stored errno is the negated UV code, which names `UV_ENOENT` in the
// SystemErrno table, and that has no coreutils label.
test.concurrent.skipIf(!isWindows)("Output.err prints the errno label for a from_libuv error", async () => {
// -UV_ENOENT
expect(await outputErr(4058, true)).toEqual({
stdout: "",
stderr: "ENOENT: No such file or directory: sysErrorOutputErr (open)\n",
exitCode: 0,
});
// -UV_EACCES
expect(await outputErr(4092, true)).toEqual({
stdout: "",
stderr: "EACCES: Permission denied: sysErrorOutputErr (open)\n",
exitCode: 0,
});
});

test.skipIf(!isWindows)("Error.name() with from_libuv=true does not overflow", () => {
// errno values as stored by node_fs.zig: @intCast(-rc) where rc is the
Expand Down
Loading