Skip to content

fix(loader): reject non-JSON modules for JSON imports - #36135

Merged
nathanwhit merged 1 commit into
denoland:mainfrom
nathanwhit:fix/json-module-type-check
Jul 20, 2026
Merged

nathanwhit merged 1 commit into
denoland:mainfrom
nathanwhit:fix/json-module-type-check

Conversation

@nathanwhit

Copy link
Copy Markdown
Member

Summary

  • require modules requested with type: "json" to resolve to JSON media
  • apply the check to prepared modules and direct npm module loading
  • add a BYONM regression covering a JavaScript package requested as JSON

Details

The loader already rejected JSON modules imported without the JSON attribute, but it did not consistently enforce the reverse direction. In the npm/BYONM path, a non-JSON module requested as JSON could continue through JavaScript loading.

This change reports a module-type error before CJS translation or module evaluation when the requested and loaded types do not match.

Validation

  • ./tools/format.js
  • cargo check -p deno_resolver --all-features
  • cargo build --bin deno
  • cargo test -p specs_tests --test specs run::byonm_json_attribute_mismatch -- --nocapture
  • cargo test -p specs_tests --test specs run::npm_json_without_attribute_error -- --nocapture
  • cargo test -p specs_tests --test specs run::byonm_exact_version -- --nocapture
  • cargo test -p specs_tests --test specs run::import_attributes_dynamic_import -- --nocapture

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nathanwhit
nathanwhit merged commit 9049005 into denoland:main Jul 20, 2026
267 of 269 checks passed
bartlomieju pushed a commit that referenced this pull request Jul 23, 2026
## Summary

- require modules requested with `type: "json"` to resolve to JSON media
- apply the check to prepared modules and direct npm module loading
- add a BYONM regression covering a JavaScript package requested as JSON

## Details

The loader already rejected JSON modules imported without the JSON
attribute, but it did not consistently enforce the reverse direction. In
the npm/BYONM path, a non-JSON module requested as JSON could continue
through JavaScript loading.

This change reports a module-type error before CJS translation or module
evaluation when the requested and loaded types do not match.

## Validation

- `./tools/format.js`
- `cargo check -p deno_resolver --all-features`
- `cargo build --bin deno`
- `cargo test -p specs_tests --test specs
run::byonm_json_attribute_mismatch -- --nocapture`
- `cargo test -p specs_tests --test specs
run::npm_json_without_attribute_error -- --nocapture`
- `cargo test -p specs_tests --test specs run::byonm_exact_version --
--nocapture`
- `cargo test -p specs_tests --test specs
run::import_attributes_dynamic_import -- --nocapture`
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.

2 participants