Unverified Commit e35ab359 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: prevent panic in swc issue emitter (#76595)

parent bf1dfbba
...@@ -23,7 +23,7 @@ pub struct AnalyzeIssue { ...@@ -23,7 +23,7 @@ pub struct AnalyzeIssue {
impl AnalyzeIssue { impl AnalyzeIssue {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn new( pub fn new(
severity: ResolvedVc<IssueSeverity>, severity: IssueSeverity,
source_ident: ResolvedVc<AssetIdent>, source_ident: ResolvedVc<AssetIdent>,
title: ResolvedVc<RcStr>, title: ResolvedVc<RcStr>,
message: ResolvedVc<StyledString>, message: ResolvedVc<StyledString>,
...@@ -31,7 +31,7 @@ impl AnalyzeIssue { ...@@ -31,7 +31,7 @@ impl AnalyzeIssue {
source: Option<IssueSource>, source: Option<IssueSource>,
) -> Vc<Self> { ) -> Vc<Self> {
Self { Self {
severity, severity: severity.resolved_cell(),
source_ident, source_ident,
title, title,
message, message,
......
...@@ -30,7 +30,7 @@ use crate::{ ...@@ -30,7 +30,7 @@ use crate::{
}; };
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(PartialOrd, Ord, Copy, Clone, Hash, Debug, DeterministicHash)] #[derive(PartialOrd, Ord, Copy, Clone, Hash, Debug, DeterministicHash, TaskInput)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub enum IssueSeverity { pub enum IssueSeverity {
Bug, Bug,
......
...@@ -413,7 +413,7 @@ pub async fn expand_star_exports( ...@@ -413,7 +413,7 @@ pub async fn expand_star_exports(
async fn emit_star_exports_issue(source_ident: Vc<AssetIdent>, message: RcStr) -> Result<()> { async fn emit_star_exports_issue(source_ident: Vc<AssetIdent>, message: RcStr) -> Result<()> {
AnalyzeIssue::new( AnalyzeIssue::new(
IssueSeverity::Warning.cell(), IssueSeverity::Warning,
source_ident, source_ident,
Vc::cell("unexpected export *".into()), Vc::cell("unexpected export *".into()),
StyledString::Text(message).cell(), StyledString::Text(message).cell(),
......
...@@ -939,7 +939,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( ...@@ -939,7 +939,7 @@ pub(crate) async fn analyse_ecmascript_module_internal(
analysis.set_async_module(async_module); analysis.set_async_module(async_module);
} else if let Some(span) = top_level_await_span { } else if let Some(span) = top_level_await_span {
AnalyzeIssue::new( AnalyzeIssue::new(
IssueSeverity::Error.cell(), IssueSeverity::Error,
source.ident(), source.ident(),
Vc::cell("unexpected top level await".into()), Vc::cell("unexpected top level await".into()),
StyledString::Text("top level await is only supported in ESM modules.".into()) StyledString::Text("top level await is only supported in ESM modules.".into())
......
...@@ -27,19 +27,46 @@ impl IssueCollector { ...@@ -27,19 +27,46 @@ impl IssueCollector {
}; };
for issue in issues { for issue in issues {
issue.to_resolved().await?.emit(); AnalyzeIssue::new(
issue.severity,
issue.source.ident(),
Vc::cell(issue.title),
issue.message.cell(),
issue.code,
issue.issue_source,
)
.to_resolved()
.await?
.emit();
} }
Ok(()) Ok(())
} }
pub fn last_emitted_issue(&self) -> Option<Vc<AnalyzeIssue>> { pub fn last_emitted_issue(&self) -> Option<Vc<AnalyzeIssue>> {
let inner = self.inner.lock(); let inner = self.inner.lock();
inner.emitted_issues.last().copied() inner.emitted_issues.last().map(|issue| {
AnalyzeIssue::new(
issue.severity,
issue.source.ident(),
Vc::cell(issue.title.clone()),
issue.message.clone().cell(),
issue.code.clone(),
issue.issue_source.clone(),
)
})
} }
} }
struct IssueCollectorInner { struct IssueCollectorInner {
emitted_issues: Vec<Vc<AnalyzeIssue>>, emitted_issues: Vec<PlainAnalyzeIssue>,
}
struct PlainAnalyzeIssue {
severity: IssueSeverity,
source: ResolvedVc<Box<dyn Source>>,
title: RcStr,
message: StyledString,
code: Option<RcStr>,
issue_source: Option<IssueSource>,
} }
pub struct IssueEmitter { pub struct IssueEmitter {
...@@ -88,7 +115,7 @@ impl Emitter for IssueEmitter { ...@@ -88,7 +115,7 @@ impl Emitter for IssueEmitter {
.as_ref() .as_ref()
.is_some_and(|d| matches!(d, DiagnosticId::Lint(_))); .is_some_and(|d| matches!(d, DiagnosticId::Lint(_)));
let severity = (if is_lint { let severity = if is_lint {
IssueSeverity::Suggestion IssueSeverity::Suggestion
} else { } else {
match level { match level {
...@@ -101,8 +128,7 @@ impl Emitter for IssueEmitter { ...@@ -101,8 +128,7 @@ impl Emitter for IssueEmitter {
Level::Cancelled => IssueSeverity::Error, Level::Cancelled => IssueSeverity::Error,
Level::FailureNote => IssueSeverity::Note, Level::FailureNote => IssueSeverity::Note,
} }
}) };
.resolved_cell();
let title; let title;
if let Some(t) = self.title.as_ref() { if let Some(t) = self.title.as_ref() {
...@@ -118,14 +144,16 @@ impl Emitter for IssueEmitter { ...@@ -118,14 +144,16 @@ impl Emitter for IssueEmitter {
}); });
// TODO add other primary and secondary spans with labels as sub_issues // TODO add other primary and secondary spans with labels as sub_issues
let issue = AnalyzeIssue::new( // This can be invoked by swc on different threads, so we cannot call any turbo-tasks or
*severity, // create cells here.
self.source.ident(), let issue = PlainAnalyzeIssue {
Vc::cell(title), severity,
StyledString::Text(message.into()).cell(), source: self.source,
title,
message: StyledString::Text(message.into()),
code, code,
source, issue_source: source,
); };
let mut inner = self.inner.lock(); let mut inner = self.inner.lock();
inner.emitted_issues.push(issue); inner.emitted_issues.push(issue);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment