Skip to content

fix: validate return type of external option callback#9545

Closed
shulaoda wants to merge 2 commits into
mainfrom
05-25-fix_validate_return_type_of_option_callback
Closed

fix: validate return type of external option callback#9545
shulaoda wants to merge 2 commits into
mainfrom
05-25-fix_validate_return_type_of_option_callback

Conversation

@shulaoda

Copy link
Copy Markdown
Member

Summary

  • Add assertCallbackReturn utility to validate return values of user-provided option callbacks at the JS layer, before they cross to Rust
  • Apply validation to the external option callback, producing a clear error like Invalid return value for option "external": expected boolean, got string. instead of the cryptic NAPI error Value is non of these types 'bool', 'UnknownReturnValue'
  • The utility supports multiple expected types (string | string[]) for future use with other option callbacks

Closes #9005

@shulaoda shulaoda requested a review from sapphi-red May 25, 2026 08:49
@shulaoda shulaoda changed the title fix: validate return type of option callback fix: validate return type of external option callback May 25, 2026
@netlify

netlify Bot commented May 25, 2026

Copy link
Copy Markdown

Deploy Preview for rolldown-rs ready!

Name Link
🔨 Latest commit 8256e57
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6a191a4828d75600087e1a91
😎 Deploy Preview https://deploy-preview-9545--rolldown-rs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@shulaoda

Copy link
Copy Markdown
Member Author

I’ve currently validated only the external case. I’ll gradually expand the validation to other function options in subsequent iterations.

@sapphi-red

Copy link
Copy Markdown
Member

Wouldn't this have additional overhead because now we have a duplicate check?

@shulaoda

Copy link
Copy Markdown
Member Author

Wouldn't this have additional overhead because now we have a duplicate check?

To be honest, this should be a very cheap check, so I don’t think we need to worry about it too much.

@sapphi-red

sapphi-red commented May 29, 2026

Copy link
Copy Markdown
Member

For this specific case, I agree that it's cheap. But for example, the return value of transform hook wouldn't be cheap, especially when the map is passed as an object.
https://rolldown.rs/reference/Interface.FunctionPluginHooks#transform

@shulaoda

Copy link
Copy Markdown
Member Author

For this specific case, I agree that it's cheap. But for example, the return value of transform hook wouldn't be cheap, especially when the map is passed as an object. https://rolldown.rs/reference/Interface.FunctionPluginHooks#transform

My understanding is that we currently only check the top-level type, i.e., simple categories like boolean | number | object. Are you suggesting that we should validate these types more strictly?

@sapphi-red

Copy link
Copy Markdown
Member

My understanding is that we currently only check the top-level type

Oh, really? I thought napi-rs checks the nested properties as well. If napi-rs's generated code only checks the top-level type, the only concern with this approach is how to ensure the code is synced with the actual types.

@shulaoda

Copy link
Copy Markdown
Member Author

My understanding is that we currently only check the top-level type

Oh, really? I thought napi-rs checks the nested properties as well. If napi-rs's generated code only checks the top-level type, the only concern with this approach is how to ensure the code is synced with the actual types.

Sorry, what I meant is that our assertCallbackReturn in this PR only checks the top-level return type, while napi-rs actually validates full type consistency.

@sapphi-red

Copy link
Copy Markdown
Member

But that means that the error message would be the same if the user returns a different nested type. If this is only for the top-level type, I'm not sure if it helps a lot.

@shulaoda

Copy link
Copy Markdown
Member Author

But that means that the error message would be the same if the user returns a different nested type. If this is only for the top-level type, I'm not sure if it helps a lot.

I still feel there's some mismatch here, since the Rust-side types don't always align with the JavaScript-side types, and there may be internal conversions happening. So do you think we should keep investing in this?

@sapphi-red

Copy link
Copy Markdown
Member

If you don't have any ideas, let's hold off for now.

@shulaoda shulaoda marked this pull request as draft June 15, 2026 14:24
graphite-app Bot pushed a commit that referenced this pull request Jun 17, 2026
Made it possible to add a message to the type error by `.context` and included the option name in it.

close #9545
@graphite-app graphite-app Bot closed this in 8606737 Jun 18, 2026
@shulaoda shulaoda deleted the 05-25-fix_validate_return_type_of_option_callback branch June 18, 2026 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The Value is non of these types error thrown for invalid return value in function options is difficult to figure the reason

2 participants