-
Notifications
You must be signed in to change notification settings - Fork 5k
bun test: report a test file or preload the loader cannot resolve instead of exiting; keep the JUnit report valid UTF-8 #38273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
494054a
de29575
d6f3bc6
ebc21f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| use core::ptr::NonNull; | ||
|
|
||
| use crate::{JSGlobalObject, JSInternalPromise, JsError, JsResult}; | ||
| use bun_core::String as BunString; | ||
|
|
||
|
|
@@ -28,33 +30,60 @@ impl JSModuleLoader { | |
| /// Raw-pointer variant of `load_and_evaluate_module`. Returns the FFI | ||
| /// `*mut JSInternalPromise` directly so callers that need to store or pass | ||
| /// a mutable cell pointer don't launder provenance through `&T -> *mut T`. | ||
| /// | ||
| /// Every load failure comes back as a rejected promise (see | ||
| /// [`Self::reject_with_thrown_exception`]); `None` only while the VM is | ||
| /// being terminated, with the termination exception left pending. | ||
| pub fn load_and_evaluate_module_ptr( | ||
| global_object: *mut JSGlobalObject, | ||
| module_name: Option<&BunString>, | ||
| ) -> Option<core::ptr::NonNull<JSInternalPromise>> { | ||
| ) -> Option<NonNull<JSInternalPromise>> { | ||
| // `JSGlobalObject` is an opaque ZST handle; `opaque_ref` is the | ||
| // centralised zero-byte deref proof (panics on null). | ||
| core::ptr::NonNull::new(JSC__JSModuleLoader__loadAndEvaluateModule( | ||
| JSGlobalObject::opaque_ref(global_object), | ||
| let global = JSGlobalObject::opaque_ref(global_object); | ||
| NonNull::new(JSC__JSModuleLoader__loadAndEvaluateModule( | ||
| global, | ||
| module_name, | ||
| )) | ||
| .or_else(|| Self::reject_with_thrown_exception(global)) | ||
| } | ||
|
|
||
| /// Raw-pointer variant of `Self::import`. Returns the FFI | ||
| /// `*mut JSInternalPromise` directly so callers that need to store or pass | ||
| /// a mutable cell pointer (e.g. `VirtualMachine::pending_internal_promise`) | ||
| /// don't launder provenance through `&T -> *mut T`. Mirrors | ||
| /// [`Self::load_and_evaluate_module_ptr`]. | ||
| /// [`Self::load_and_evaluate_module_ptr`], including how failures are | ||
| /// reported: `Err` only while the VM is being terminated. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| pub fn import_ptr( | ||
| global_object: *mut JSGlobalObject, | ||
| module_name: &BunString, | ||
| ) -> JsResult<core::ptr::NonNull<JSInternalPromise>> { | ||
| ) -> JsResult<NonNull<JSInternalPromise>> { | ||
| // `JSGlobalObject` is an opaque ZST handle; `opaque_ref` is the | ||
| // centralised zero-byte deref proof (panics on null). | ||
| core::ptr::NonNull::new(JSModuleLoader__import( | ||
| JSGlobalObject::opaque_ref(global_object), | ||
| module_name, | ||
| )) | ||
| .ok_or(JsError::Thrown) | ||
| let global = JSGlobalObject::opaque_ref(global_object); | ||
| NonNull::new(JSModuleLoader__import(global, module_name)) | ||
| .or_else(|| Self::reject_with_thrown_exception(global)) | ||
| .ok_or(JsError::Thrown) | ||
| } | ||
|
|
||
| /// JSC resolves the specifier before it has a promise to reject | ||
| /// (Completion.cpp `loadAndEvaluateModule`, JSModuleLoader.cpp | ||
| /// `requestImportModule`), so an unresolvable one is thrown and the binding | ||
| /// returns null. One way to get there is a path whose bytes are not valid | ||
| /// UTF-8: once it is a JS string it no longer names the file. The callers | ||
| /// report load failures from the promise, so deliver the error that way, | ||
| /// marked handled like the loader's own promises so the unhandled | ||
| /// rejection tracker does not report it a second time. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| fn reject_with_thrown_exception(global: &JSGlobalObject) -> Option<NonNull<JSInternalPromise>> { | ||
| let exception = global.try_take_exception()?; | ||
| // `try_take_exception` leaves a termination exception pending; so do we. | ||
| if exception.is_termination_exception() { | ||
| return None; | ||
| } | ||
| let promise = JSInternalPromise::create(global); | ||
| promise | ||
| .reject_as_handled(global, exception.to_error().unwrap_or(exception)) | ||
| .ok()?; | ||
| Some(NonNull::from(promise)) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,22 +186,18 @@ impl ResolveMessage { | |
| let _ = write!(&mut out, "Module not found '{}'", BStr::new(specifier)); | ||
| return out; | ||
| } | ||
| if bun_resolver::is_package_path(specifier) | ||
| let what = if bun_resolver::is_package_path(specifier) | ||
| && !strings::contains_char(specifier, b'/') | ||
| { | ||
| let _ = write!( | ||
| &mut out, | ||
| "Cannot find package '{}' from '{}'", | ||
| BStr::new(specifier), | ||
| BStr::new(referrer), | ||
| ); | ||
| "package" | ||
| } else { | ||
| let _ = write!( | ||
| &mut out, | ||
| "Cannot find module '{}' from '{}'", | ||
| BStr::new(specifier), | ||
| BStr::new(referrer), | ||
| ); | ||
| "module" | ||
| }; | ||
| let _ = write!(&mut out, "Cannot find {what} '{}'", BStr::new(specifier)); | ||
| // Entry points and preloads are loaded directly, not imported | ||
| // from anywhere; Node words those the same way. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| if !referrer.is_empty() { | ||
| let _ = write!(&mut out, " from '{}'", BStr::new(referrer)); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3609,7 +3609,7 @@ JSC::Identifier GlobalObject::moduleLoaderResolve(JSGlobalObject* jsGlobalObject | |
| Zig::GlobalObject* globalObject = static_cast<Zig::GlobalObject*>(jsGlobalObject); | ||
|
|
||
| ErrorableString res; | ||
| res.success = false; | ||
| memset(&res, 0, sizeof(res)); | ||
|
|
||
| BunString keyZ; | ||
| if (key.isString()) { | ||
|
|
@@ -3682,7 +3682,11 @@ JSC::Identifier GlobalObject::moduleLoaderResolve(JSGlobalObject* jsGlobalObject | |
| return result; | ||
| } else { | ||
| auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); | ||
| throwException(scope, res.result.err, globalObject); | ||
| // The resolver leaves `res` untouched when it threw (a plugin's onResolve | ||
| // throwing, for instance); that exception is the one to keep. Same shape | ||
| // as moduleLoaderImportModule below. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| if (!scope.exception()) | ||
| throwException(scope, res.result.err, globalObject); | ||
| return globalObject->vm().propertyNames->emptyIdentifier; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,7 +141,23 @@ mod bun_test { | |
| } | ||
| } | ||
|
|
||
| /// The report declares `encoding="UTF-8"`, but file paths, the hostname and the | ||
| /// CI env vars are raw OS bytes: ill-formed sequences become U+FFFD, as the | ||
| /// console already prints them (`bstr::BStr`). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| pub(crate) fn escape_xml(str_: &[u8], writer: &mut impl bun_io::Write) -> crate::Result<()> { | ||
| if strings::is_valid_utf8(str_) { | ||
| return escape_xml_utf8(str_, writer); | ||
| } | ||
| for chunk in str_.utf8_chunks() { | ||
| escape_xml_utf8(chunk.valid().as_bytes(), writer)?; | ||
| if !chunk.invalid().is_empty() { | ||
| writer.write_all("\u{FFFD}".as_bytes())?; | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn escape_xml_utf8(str_: &[u8], writer: &mut impl bun_io::Write) -> crate::Result<()> { | ||
| let mut last: usize = 0; | ||
| let mut i: usize = 0; | ||
| let len = str_.len(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -712,8 +712,10 @@ fn generate_entry_point(_vm: &VirtualMachine, watch: bool, entry_path: &[u8]) -> | |
| /// preload promise if any, else null. | ||
| /// | ||
| /// Error mapping: resolver `Failure` returns the resolver error, | ||
| /// `Pending`/`NotFound` returns `error.ModuleNotFound`, | ||
| /// `JSModuleLoader.import` throwing returns `error.JSError`. | ||
| /// `Pending`/`NotFound` returns `error.ModuleNotFound`. A preload the module | ||
| /// loader cannot load comes back as a rejected promise like any other failing | ||
| /// preload; `JSModuleLoader.import` only fails (`error.JSError`) when the VM is | ||
| /// being terminated. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| /// | ||
| /// # Safety | ||
| /// `vm` is the live per-thread VM. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code