Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions src/runtime/shell/builtin/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,13 @@ impl Cd {
interp: &Interpreter,
cmd: NodeId,
_: usize,
_err: Option<bun_sys::SystemError>,
err: Option<bun_sys::SystemError>,
) -> Yield {
Self::state_mut(interp, cmd).state = State::Done;
Builtin::done(interp, cmd, 1)
Builtin::done(
interp,
cmd,
err.map_or(1, |e| e.get_errno() as crate::shell::ExitCode),
)
}
}
2 changes: 1 addition & 1 deletion src/runtime/shell/builtin/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Echo {
Builtin::done(
interp,
cmd,
err.map(|e| e.errno as crate::shell::ExitCode).unwrap_or(0),
err.map_or(0, |e| e.get_errno() as crate::shell::ExitCode),
)
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/shell/builtin/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ impl Export {
err: Option<bun_sys::SystemError>,
) -> Yield {
Self::state_mut(interp, cmd).state = State::Done;
Builtin::done(interp, cmd, err.map_or(0, |_| 1))
Builtin::done(
interp,
cmd,
err.map_or(0, |e| e.get_errno() as crate::shell::ExitCode),
Comment thread
robobun marked this conversation as resolved.
)
}
}
2 changes: 1 addition & 1 deletion src/runtime/shell/builtin/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ impl Mv {
_ => unreachable!(),
};
// The failing rename's errno becomes the shell exit code.
let exit_code = e.errno as ExitCode;
let exit_code = e.get_errno() as ExitCode;
let buf = Builtin::task_error_to_string(interp, cmd, Kind::Mv, &e).to_vec();
Self::write_failing_error(interp, cmd, &buf, exit_code).run(interp);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/shell/builtin/which.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl Which {
e: Option<bun_sys::SystemError>,
) -> Yield {
if let Some(err) = e {
return Builtin::done(interp, cmd, err.errno as crate::shell::ExitCode);
return Builtin::done(interp, cmd, err.get_errno() as crate::shell::ExitCode);
}
match Self::state_mut(interp, cmd).state {
State::OneArg => Builtin::done(interp, cmd, 1),
Expand Down
25 changes: 24 additions & 1 deletion test/js/bun/shell/bunshell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ import { $ } from "bun";
import { afterAll, beforeAll, describe, expect, it, test } from "bun:test";
import { chmodSync, mkdirSync } from "fs";
import { mkdir, rm, stat } from "fs/promises";
import { bunExe, isPosix, isWindows, rss, runWithErrorPromise, tempDir, tempDirWithFiles, tmpdirSync } from "harness";
import {
bunExe,
isLinux,
isPosix,
isWindows,
rss,
runWithErrorPromise,
tempDir,
tempDirWithFiles,
tmpdirSync,
} from "harness";
import { join, sep } from "path";
import { createTestBuilder, sortedShellOutput } from "./util";
const TestBuilder = createTestBuilder(import.meta.path);
Expand Down Expand Up @@ -89,6 +99,19 @@ describe("bunshell", () => {
.runAsTest(cmdstr)
: "",
);

// Every write to /dev/full fails with ENOSPC. The builtin reports that
// errno as its exit code, like the builtins that already do (cat, rm).
test.skipIf(!isLinux)("builtins exit with the errno of a failed output write", async () => {
const ENOSPC = 28;
const exitCodes = {
echo: (await $`echo hi > /dev/full`.nothrow()).exitCode,
which: (await $`which sh > /dev/full`.nothrow()).exitCode,
export: (await $`export FOO=bar; export > /dev/full`.nothrow()).exitCode,
cd: (await $`cd /nonexistent-dir 2> /dev/full`.nothrow()).exitCode,
};
expect(exitCodes).toEqual({ echo: ENOSPC, which: ENOSPC, export: ENOSPC, cd: ENOSPC });
});
});

describe("concurrency", () => {
Expand Down
Loading