Skip to content
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

fix(ext/node): lossy UTF-8 read node_modules files #24140

Merged
merged 2 commits into from
Jun 8, 2024

Conversation

lucacasonato
Copy link
Member

Previously various reads of files in node_modules would error on invalid UTF-8. These were cases involving:

  • reading package.json from Rust
  • reading package.json from JS
  • reading CommonJS files from JS
  • reading CommonJS files from Rust (for ESM translation)
  • reading ESM files from Rust

There are tests for all these cases now.

Fixes #24027

Needs denoland/deno_core#773 to land first.

Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

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

Nice. LGTM

Comment on lines +323 to +328
let code = match String::from_utf8_lossy(&code) {
Cow::Owned(code) => code,
// SAFETY: `String::from_utf8_lossy` guarantees that the result is valid
// UTF-8 if `Cow::Borrowed` is returned.
Cow::Borrowed(_) => unsafe { String::from_utf8_unchecked(code) },
};
Copy link
Member

Choose a reason for hiding this comment

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

It's surprising there isn't a version of this on String.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah... I have to reimplement this helper frequently 🥲

Maybe I should make a PR to the standard library.

@lucacasonato lucacasonato merged commit c1f23c5 into denoland:main Jun 8, 2024
17 checks passed
nathanwhit pushed a commit that referenced this pull request Jun 13, 2024
Previously various reads of files in `node_modules` would error on
invalid UTF-8. These were cases involving:

- reading package.json from Rust
- reading package.json from JS
- reading CommonJS files from JS
- reading CommonJS files from Rust (for ESM translation)
- reading ESM files from Rust
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.

fails to import a module because of invalid utf8 sequence from npm packages
2 participants