diff options
author | 2023-09-12 03:35:29 -0700 | |
---|---|---|
committer | 2023-09-12 03:35:29 -0700 | |
commit | 6e4f746ace3a42c1a5c9431beba2ae599a6f15b0 (patch) | |
tree | 4d20e51072887d443cf008179ffee19307bdb90c | |
parent | b5a3bed7f2b0b511a5de3ba4a35813338b410703 (diff) | |
download | bun-6e4f746ace3a42c1a5c9431beba2ae599a6f15b0.tar.gz bun-6e4f746ace3a42c1a5c9431beba2ae599a6f15b0.tar.zst bun-6e4f746ace3a42c1a5c9431beba2ae599a6f15b0.zip |
Fix some bugs blocking Turborepo from using `bun run` (#5071)
* Clean up some error handling when loading `tsconfig.json`
* [bun run] don't parse tsconfig.json for package.json scripts
* Make this error message better
* Bump
* Don't print build errors twice
* Handle quotes in error messages a little better
* Add a couple tests
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | src/build-id | 2 | ||||
-rw-r--r-- | src/bun_js.zig | 1 | ||||
-rw-r--r-- | src/cli/run_command.zig | 8 | ||||
-rw-r--r-- | src/options.zig | 1 | ||||
-rw-r--r-- | src/resolver/resolver.zig | 17 | ||||
-rw-r--r-- | test/cli/install/bun-run.test.ts | 58 |
6 files changed, 78 insertions, 9 deletions
diff --git a/src/build-id b/src/build-id index c22708346..56a6051ca 100644 --- a/src/build-id +++ b/src/build-id @@ -1 +1 @@ -0
\ No newline at end of file +1
\ No newline at end of file diff --git a/src/bun_js.zig b/src/bun_js.zig index 0605c57a1..29ecf28b8 100644 --- a/src/bun_js.zig +++ b/src/bun_js.zig @@ -273,6 +273,7 @@ pub const Run = struct { } else { vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {}; } + vm.log.msgs.items.len = 0; Output.prettyErrorln("\n", .{}); Output.flush(); } diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig index dcb5312b1..42560947a 100644 --- a/src/cli/run_command.zig +++ b/src/cli/run_command.zig @@ -482,6 +482,10 @@ pub const RunCommand = struct { this_bundler.resolver.care_about_bin_folder = true; this_bundler.resolver.care_about_scripts = true; + + this_bundler.resolver.opts.load_tsconfig_json = false; + this_bundler.options.load_tsconfig_json = false; + this_bundler.configureLinker(); var root_dir_info = this_bundler.resolver.readDirInfo(this_bundler.fs.top_level_dir) catch |err| { @@ -491,7 +495,7 @@ pub const RunCommand = struct { } else { ctx.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {}; } - Output.prettyErrorln("Error loading directory: \"{s}\"", .{@errorName(err)}); + Output.prettyErrorln("<r><red>error<r><d>:<r> <b>{s}<r> loading directory {}", .{ @errorName(err), strings.QuotedFormatter{ .text = this_bundler.fs.top_level_dir } }); Output.flush(); return err; } orelse { @@ -500,7 +504,7 @@ pub const RunCommand = struct { } else { ctx.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {}; } - Output.prettyErrorln("Error loading current directory", .{}); + Output.prettyErrorln("error loading current directory", .{}); Output.flush(); return error.CouldntReadCurrentDirectory; }; diff --git a/src/options.zig b/src/options.zig index c4961228b..57b39aeaa 100644 --- a/src/options.zig +++ b/src/options.zig @@ -1419,6 +1419,7 @@ pub const BundleOptions = struct { transform_options: Api.TransformOptions, polyfill_node_globals: bool = false, transform_only: bool = false, + load_tsconfig_json: bool = true, rewrite_jest_for_tests: bool = false, diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index d1af1a599..fd6ba9d09 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -3840,7 +3840,7 @@ pub const Resolver = struct { } // Record if this directory has a tsconfig.json or jsconfig.json file - { + if (r.opts.load_tsconfig_json) { var tsconfig_path: ?string = null; if (r.opts.tsconfig_override == null) { if (entries.getComptimeQuery("tsconfig.json")) |lookup| { @@ -3871,10 +3871,10 @@ pub const Resolver = struct { ) catch |err| brk: { const pretty = r.prettyPath(Path.init(tsconfigpath)); - if (err == error.ENOENT) { - r.log.addErrorFmt(null, logger.Loc.Empty, r.allocator, "Cannot find tsconfig file \"{s}\"", .{pretty}) catch unreachable; - } else if (err != error.ParseErrorAlreadyLogged and err != error.IsDir) { - r.log.addErrorFmt(null, logger.Loc.Empty, r.allocator, "Cannot read file \"{s}\": {s}", .{ pretty, @errorName(err) }) catch unreachable; + if (err == error.ENOENT or err == error.FileNotFound) { + r.log.addErrorFmt(null, logger.Loc.Empty, r.allocator, "Cannot find tsconfig file {}", .{bun.strings.QuotedFormatter{ .text = pretty }}) catch {}; + } else if (err != error.ParseErrorAlreadyLogged and err != error.IsDir and err != error.EISDIR) { + r.log.addErrorFmt(null, logger.Loc.Empty, r.allocator, "Cannot read file {}: {s}", .{ bun.strings.QuotedFormatter{ .text = pretty }, @errorName(err) }) catch {}; } break :brk null; }; @@ -3886,7 +3886,12 @@ pub const Resolver = struct { var ts_dir_name = Dirname.dirname(current.abs_path); // not sure why this needs cwd but we'll just pass in the dir of the tsconfig... var abs_path = ResolvePath.joinAbsStringBuf(ts_dir_name, bufs(.tsconfig_path_abs), &[_]string{ ts_dir_name, current.extends }, .auto); - var parent_config_maybe = try r.parseTSConfig(abs_path, 0); + var parent_config_maybe = r.parseTSConfig(abs_path, 0) catch |err| { + r.log.addWarningFmt(null, logger.Loc.Empty, r.allocator, "{s} loading tsconfig.json extends {}", .{ @errorName(err), strings.QuotedFormatter{ + .text = abs_path, + } }) catch {}; + break; + }; if (parent_config_maybe) |parent_config| { try parent_configs.append(parent_config); current = parent_config; diff --git a/test/cli/install/bun-run.test.ts b/test/cli/install/bun-run.test.ts index 781e11c43..6bf910951 100644 --- a/test/cli/install/bun-run.test.ts +++ b/test/cli/install/bun-run.test.ts @@ -62,6 +62,64 @@ for (let withRun of [false, true]) { expect(exitCode).toBe(0); }); + it("invalid tsconfig.json is ignored", async () => { + await writeFile( + join(run_dir, "package.json"), + JSON.stringify({ + name: "test", + version: "0.0.0", + scripts: { + "boop": "echo 'hi'", + }, + }), + ); + + await writeFile(join(run_dir, "tsconfig.json"), "!!!bad!!!"); + + const { stdout, stderr, exitCode } = spawnSync({ + cmd: [bunExe(), "--silent", withRun ? "run" : "", "boop"].filter(Boolean), + cwd: run_dir, + env: bunEnv, + }); + + expect(stderr.toString()).toBe(""); + expect(stdout.toString()).toBe("hi\n"); + expect(exitCode).toBe(0); + }); + + it("valid tsconfig.json with invalid extends doesn't crash", async () => { + await writeFile( + join(run_dir, "package.json"), + JSON.stringify({ + name: "test", + version: "0.0.0", + scripts: {}, + }), + ); + await writeFile( + join(run_dir, "tsconfig.json"), + JSON.stringify( + { + extends: "!!!bad!!!", + }, + null, + 2, + ), + ); + + await writeFile(join(run_dir, "index.js"), "console.log('hi')"); + + const { stdout, stderr, exitCode } = spawnSync({ + cmd: [bunExe(), "--silent", withRun ? "run" : "", "./index.js"].filter(Boolean), + cwd: run_dir, + env: bunEnv, + }); + + expect(stderr.toString().trim()).toContain("FileNotFound loading tsconfig.json extends"); + expect(stdout.toString()).toBe("hi\n"); + expect(exitCode).toBe(0); + }); + it("falling back to index with no package.json", async () => { await writeFile(join(run_dir, "index.ts"), "console.log('Hello, world!');"); |