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: should create an empty codegen result for module that has failure in codegen #7777

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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();
JSerFeng marked this conversation as resolved.
Show resolved Hide resolved
(
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::<Result<Vec<_>>>()?;
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::<Vec<_>>();
for (module, codegen_res, runtimes, from_cache, err) in results {
if let Some(counter) = cache_counter {
if from_cache {
counter.hit();
Expand All @@ -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(())
}
Expand Down
28 changes: 13 additions & 15 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1730,21 +1730,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));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(DEFINE_VAR)
export default 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import v from './foo'

console.log(v)
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ERROR in ./index.js + 1 modules
× JavaScript 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);
╰────
Loading