Skip to content

Commit

Permalink
Improve error messages when you mistype commands/files/scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed May 30, 2022
1 parent 882559f commit aecc849
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1131,8 +1131,10 @@ pub const Command = struct {
}
}

var was_js_like = false;
if (options.defaultLoaders.get(extension)) |loader| {
if (loader.isJavaScriptLike()) {
was_js_like = true;
possibly_open_with_bun_js: {
const script_name_to_search = ctx.args.entry_points[0];

Expand Down Expand Up @@ -1205,6 +1207,26 @@ pub const Command = struct {
if (try RunCommand.exec(ctx, true, false)) {
return;
}

Output.prettyErrorln("<r><red>error<r>: Script not found \"<b>{s}<r>\"", .{
ctx.positionals[0],
});
Output.flush();
Global.exit(1);
}

if (was_js_like) {
Output.prettyErrorln("<r><red>error<r>: Module not found \"<b>{s}<r>\"", .{
ctx.positionals[0],
});
Output.flush();
Global.exit(1);
} else if (ctx.positionals.len > 0) {
Output.prettyErrorln("<r><red>error<r>: File not found \"<b>{s}<r>\"", .{
ctx.positionals[0],
});
Output.flush();
Global.exit(1);
}

if (FeatureFlags.dev_only) {
Expand Down
21 changes: 17 additions & 4 deletions src/cli/run_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,26 @@ pub const RunCommand = struct {
child_process.stdout_behavior = .Inherit;

const result = child_process.spawnAndWait() catch |err| {
Output.prettyErrorln("<r><red>error<r>: Failed to run <b>{s}<r> due to error <b>{s}<r>", .{ std.fs.path.basename(executable), @errorName(err) });
if (err == error.AccessDenied) {
{
var stat = std.mem.zeroes(std.c.Stat);
const rc = bun.C.stat(std.meta.assumeSentinel(executable, 0), &stat);
if (rc == 0) {
if (std.os.S.ISDIR(stat.mode)) {
Output.prettyErrorln("<r><red>error<r>: Failed to run directory \"<b>{s}<r>\"\n", .{executable});
Output.flush();
Global.exit(1);
}
}
}
}
Output.prettyErrorln("<r><red>error<r>: Failed to run \"<b>{s}<r>\" due to error <b>{s}<r>", .{ std.fs.path.basename(executable), @errorName(err) });
Output.flush();
return false;
Global.exit(1);
};

if (result.Exited > 0) {
Output.prettyErrorln("<r><red>error<r> <b>\"{s}\"<r> exited with {d} status<r>", .{ std.fs.path.basename(executable), result.Exited });
Output.prettyErrorln("<r><red>error<r> \"<b>{s}<r>\" exited with {d} status<r>", .{ std.fs.path.basename(executable), result.Exited });
Output.flush();
Global.exit(result.Exited);
}
Expand Down Expand Up @@ -947,7 +960,7 @@ pub const RunCommand = struct {
}

if (comptime log_errors) {
Output.prettyError("<r><red>error:<r> Missing script: <b>{s}<r>\n", .{script_name_to_search});
Output.prettyError("<r><red>error:<r> Missing script \"<b>{s}<r>\"\n", .{script_name_to_search});
Output.flush();
Global.exit(0);
}
Expand Down

0 comments on commit aecc849

Please sign in to comment.