diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 7b09b8e54ab..69455d14578 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -37,11 +37,11 @@ use crate::{ unaffected_cache::UnaffectedModulesCache, BoxDependency, BoxModule, CacheCount, CacheOptions, Chunk, ChunkByUkey, ChunkContentHash, ChunkGraph, ChunkGroupByUkey, ChunkGroupUkey, ChunkKind, ChunkUkey, CodeGenerationJob, - CodeGenerationResults, CompilationLogger, CompilationLogging, CompilerOptions, DependencyId, - DependencyType, Entry, EntryData, EntryOptions, EntryRuntime, Entrypoint, ExecuteModuleId, - Filename, ImportVarMap, LocalFilenameFn, Logger, Module, ModuleFactory, ModuleGraph, - ModuleGraphPartial, ModuleIdentifier, PathData, ResolverFactory, RuntimeGlobals, RuntimeModule, - RuntimeSpecMap, SharedPluginDriver, SourceType, Stats, + CodeGenerationResult, CodeGenerationResults, CompilationLogger, CompilationLogging, + CompilerOptions, DependencyId, DependencyType, Entry, EntryData, EntryOptions, EntryRuntime, + Entrypoint, ExecuteModuleId, Filename, ImportVarMap, LocalFilenameFn, Logger, Module, + ModuleFactory, ModuleGraph, ModuleGraphPartial, ModuleIdentifier, PathData, ResolverFactory, + RuntimeGlobals, RuntimeModule, RuntimeSpecMap, SharedPluginDriver, SourceType, Stats, }; pub type BuildDependency = ( @@ -870,19 +870,33 @@ impl Compilation { .into_par_iter() .map(|job| { let module = job.module; - self - .old_cache - .code_generate_occasion - .use_cache(job, |module, runtime| { - let module = module_graph - .module_by_identifier(&module) - .expect("should have module"); - module.code_generation(self, Some(runtime), None) - }) - .map(|(res, runtimes, from_cache)| (module, res, runtimes, from_cache)) + let runtimes = job.runtimes.clone(); + ( + module, + runtimes, + self + .old_cache + .code_generate_occasion + .use_cache(job, |module, runtime| { + let module = module_graph + .module_by_identifier(&module) + .expect("should have module"); + module.code_generation(self, Some(runtime), None) + }), + ) }) - .collect::>>()?; - for (module, codegen_res, runtimes, from_cache) in results { + .map(|(module, runtime, result)| match result { + Ok((result, runtime, from_cache)) => (module, result, runtime, from_cache, None), + Err(err) => ( + module, + CodeGenerationResult::default(), + runtime, + false, + Some(err), + ), + }) + .collect::>(); + for (module, codegen_res, runtimes, from_cache, err) in results { if let Some(counter) = cache_counter { if from_cache { counter.hit(); @@ -902,6 +916,10 @@ impl Compilation { .add(module, runtime, codegen_res_id); } self.code_generated_modules.insert(module); + + if let Some(err) = err { + self.push_diagnostic(Diagnostic::from(err).with_module_identifier(Some(module))); + } } Ok(()) } diff --git a/crates/rspack_core/src/concatenated_module.rs b/crates/rspack_core/src/concatenated_module.rs index 314ad7ec98b..f20613ca84a 100644 --- a/crates/rspack_core/src/concatenated_module.rs +++ b/crates/rspack_core/src/concatenated_module.rs @@ -1715,21 +1715,19 @@ impl ConcatenatedModule { Ok(res) => Program::Module(res), Err(err) => { let span: ErrorSpan = err.span().into(); - self - .diagnostics - .lock() - .expect("should have diagnostics") - .append(&mut map_box_diagnostics_to_module_parse_diagnostics(vec![ - rspack_error::TraceableError::from_source_file( - &fm, - span.start as usize, - span.end as usize, - "JavaScript parsing error".to_string(), - err.kind().msg().to_string(), - ) - .with_kind(DiagnosticKind::JavaScript), - ])); - return Ok(ModuleInfo::Concatenated(module_info)); + + // return empty error as we already push error to compilation.diagnostics + return Err( + rspack_error::TraceableError::from_source_file( + &fm, + span.start as usize, + span.end as usize, + "JavaScript parsing error:\n".to_string(), + err.kind().msg().to_string(), + ) + .with_kind(DiagnosticKind::JavaScript) + .into(), + ); } }; let mut ast = Ast::new(program, cm, Some(comments)); diff --git a/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/foo.js b/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/foo.js new file mode 100644 index 00000000000..789b8c6a932 --- /dev/null +++ b/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/foo.js @@ -0,0 +1,2 @@ +console.log(DEFINE_VAR) +export default 1 diff --git a/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/index.js b/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/index.js new file mode 100644 index 00000000000..de0e61e3cb2 --- /dev/null +++ b/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/index.js @@ -0,0 +1,3 @@ +import v from './foo' + +console.log(v) diff --git a/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/rspack.config.js b/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/rspack.config.js new file mode 100644 index 00000000000..61700973075 --- /dev/null +++ b/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/rspack.config.js @@ -0,0 +1,13 @@ +const rspack = require('@rspack/core') + +/** @type {import('@rspack/core').Configuration} */ +module.exports = { + plugins: [ + new rspack.DefinePlugin({ + DEFINE_VAR: '1 2 3' + }) + ], + optimization: { + concatenateModules: true + } +} diff --git a/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/stats.err b/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/stats.err new file mode 100644 index 00000000000..3b330356b29 --- /dev/null +++ b/packages/rspack-test-tools/tests/diagnosticsCases/module-parse-failed/concatenate_parse_module/stats.err @@ -0,0 +1,7 @@ +ERROR in × ./foo.js parsing error: + │ : Expected ',', got 'numeric literal (2, 2)' + ╭─[1:14] + 1 │ console.log(1 2 3) + · ─ + 2 │ /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (1); + ╰──── \ No newline at end of file