Repository navigation
allow importing of files with top level awaits in bundler - #14218
Conversation
small-angel
left a comment
There was a problem hiding this comment.
i have tiny nitpicks. overall this looks correct porting from esbuild. thank you for the regression test.
| // Stop now if there were errors | ||
| if (this.log.hasErrors()) { | ||
| return &[_]Chunk{}; | ||
| return error.BuildFailed; |
There was a problem hiding this comment.
im still so surprised this hasnt caused any problems. or maybe it has.
| // async. This happens when you call "import()" on this module and code | ||
| // splitting is off. | ||
| if (Index.isValid(Index.init(result_tla_check.parent))) { | ||
| c.graph.meta.items(.flags)[source_index].is_async_or_has_async_dependency = true; |
There was a problem hiding this comment.
we don't need to call .items here
we can hoist it out to the parent
There was a problem hiding this comment.
.items continues to be called
| const parent_source_index = other_source_index; | ||
|
|
||
| if (parent_result.top_level_await_keyword.len > 0) { | ||
| tla_pretty_path = c.parse_graph.input_files.items(.source)[other_source_index].path.pretty; |
There was a problem hiding this comment.
don't need to call .items here either
| else | ||
| std.fmt.allocPrint(c.allocator, "This require call is not allowed because the transitive dependency \"{s}\" contains a top-level await", .{tla_pretty_path}) catch bun.outOfMemory(); | ||
|
|
||
| const source: Logger.Source = c.parse_graph.input_files.items(.source)[source_index]; |
There was a problem hiding this comment.
this does a stack copy and then only references by pointer
let's avoid the stack copy entirely via &
|
|
||
| // Build up a chain of notes for all of the imports | ||
| while (true) { | ||
| const parent_result = c.parse_graph.ast.get(other_source_index); |
There was a problem hiding this comment.
Do we need to call .get() here? This loads data from many different arrays.
There was a problem hiding this comment.
only top_level_await_keywords and tla_check are referenced?
| } | ||
|
|
||
| pub fn validateTLA(c: *LinkerContext, source_index: Index.Int) js_ast.TlaCheck { | ||
| var result_tla_check: *js_ast.TlaCheck = &c.parse_graph.ast.items(.tla_check).ptr[source_index]; |
There was a problem hiding this comment.
This .ptr access avoids safety checks. Can we avoid doing that?
There was a problem hiding this comment.
was using it while debugging and missed it when reviewing. fixed
|
|
||
| pub fn validateTLA(c: *LinkerContext, source_index: Index.Int) js_ast.TlaCheck { | ||
| var result_tla_check: *js_ast.TlaCheck = &c.parse_graph.ast.items(.tla_check).ptr[source_index]; | ||
| const result = c.parse_graph.ast.get(source_index); |
There was a problem hiding this comment.
Please don't get the entire AST for only a handful of necessary fields
| // does it have a JS AST? | ||
| if (!(id < import_records_list.len)) continue; | ||
|
|
||
| _ = this.validateTLA(id); |
There was a problem hiding this comment.
Since most projects do not use any top-level await, maybe we can add a boolean in ParseGraph or LinkerGraph that tracks whether any modules at all used top-level await, and then only run this nested for loop over all the import records of all the files when any file has top-level await? We can set that boolean as we receive results from the parser threads to avoid any extra passes
| stdout: "0\n1", | ||
| }, | ||
| }); | ||
| itBundled("default/TopLevelAwaitImport", { |
There was a problem hiding this comment.
Needs more tests.
This only tests one level deep.
Please add tests for:
- require
- dynamic import
- import statement
These tests need to involve multiple files and permutations of those.
There was a problem hiding this comment.
what about require in-between for each of those?
| return hasher.digest(); | ||
| } | ||
|
|
||
| pub fn validateTLA(c: *LinkerContext, source_index: Index.Int) js_ast.TlaCheck { |
There was a problem hiding this comment.
I suggest passing the slices in to the function, so that we avoid doing any extra work
| else | ||
| std.fmt.allocPrint(c.allocator, "This require call is not allowed because the transitive dependency \"{s}\" contains a top-level await", .{tla_pretty_path}) catch bun.outOfMemory(); | ||
|
|
||
| const source: Logger.Source = input_files[source_index]; |
There was a problem hiding this comment.
still making a stack copy here.
|
|
||
| // Require of a top-level await chain is forbidden | ||
| if (record.kind == .require) { | ||
| var notes = std.ArrayList(Logger.Data).init(c.allocator); |
There was a problem hiding this comment.
This leaks memory. notes is cloned in addRangeErrorWithNotes (not the contents within the notes, but the slice is)
There was a problem hiding this comment.
this is fine; c.allocator is an arena so it all gets handled
- optimize usages of ast.items - don't make stack copy of source - pass slices to validateTLA - don't leak memory on notes
| // does it have a JS AST? | ||
| if (!(id < import_records_list.len)) continue; | ||
|
|
||
| const tla_keywords = this.parse_graph.ast.items(.top_level_await_keyword); |
There was a problem hiding this comment.
We are now calling .items for every reachable file. This is an improvement over calling .items for every reachable file for every import record for every import record's record, but we can still move this one level up. It might even already be there.
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
|
|
||
| if (result_tla_check.depth == 0) { | ||
| result_tla_check.depth = 1; | ||
| if (result_top_level_await_keyword.len > 0) { |
There was a problem hiding this comment.
| if (result_top_level_await_keyword.len > 0) { | |
| if (tla_keywords[source_index].len > 0) { |
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
What does this PR do?
fixes #12508 - we now implement validateTLA from esbuild and properly flag
is_async_or_has_async_dependency.How did you verify your code works?