Skip to content

Commit

Permalink
fix: panic when imported json file is empty (#8397)
Browse files Browse the repository at this point in the history
* fix: panic when imported json file is empty

* fix: panic when imported json file is empty
  • Loading branch information
LingyuCoder authored Nov 11, 2024
1 parent bcde793 commit 82b19c4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ impl Module for NormalModule {
SourceType::JavaScript,
RawSource::from(format!("throw new Error({});\n", json!(error))).boxed(),
);
code_generation_result.concatenation_scope = concatenation_scope;
}
Ok(code_generation_result)
} else {
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_plugin_json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ impl ParserAndGenerator for JsonParserAndGenerator {
ExceededDepthLimit | WrongType(_) | FailedUtf8Parsing => diagnostic!("{e}").boxed(),
UnexpectedEndOfJson => {
// End offset of json file
let offset = source.len() - 1;
let length = source.len();
let offset = if length > 0 { length - 1 } else { length };
TraceableError::from_file(
source.into_owned(),
offset,
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import empty from './empty.json'

empty;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @type {import('@rspack/core').RspackOptions}
*/
module.exports = {
context: __dirname,
devtool: false,
optimization: {
concatenateModules: true,
minimize: false
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ERROR in ./empty.json
× Module parse failed:
╰─▶ × Json parsing error: Unexpected end of JSON
╭────
╰────

help:
You may need an appropriate loader to handle this file type.

2 comments on commit 82b19c4

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Benchmark detail: Open

Name Base (2024-11-11 2ac4a11) Current Change
10000_big_production-mode + exec 44.3 s ± 799 ms 44.5 s ± 1.26 s +0.35 %
10000_development-mode + exec 1.85 s ± 27 ms 1.85 s ± 27 ms -0.09 %
10000_development-mode_hmr + exec 642 ms ± 6.8 ms 646 ms ± 7.2 ms +0.64 %
10000_production-mode + exec 2.44 s ± 34 ms 2.42 s ± 36 ms -0.64 %
arco-pro_development-mode + exec 1.78 s ± 79 ms 1.79 s ± 48 ms +0.96 %
arco-pro_development-mode_hmr + exec 430 ms ± 0.53 ms 431 ms ± 1.3 ms +0.14 %
arco-pro_production-mode + exec 3.17 s ± 72 ms 3.21 s ± 116 ms +1.28 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.24 s ± 79 ms 3.23 s ± 51 ms -0.23 %
threejs_development-mode_10x + exec 1.58 s ± 13 ms 1.59 s ± 25 ms +0.20 %
threejs_development-mode_10x_hmr + exec 774 ms ± 12 ms 779 ms ± 4.9 ms +0.67 %
threejs_production-mode_10x + exec 4.96 s ± 35 ms 4.97 s ± 22 ms +0.08 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success

Please sign in to comment.