Skip to content

fix: handle unparseable @container conditions with error_recovery - #1330

Open
Larslllllll wants to merge 1 commit into
parcel-bundler:masterfrom
Larslllllll:fix/container-error-recovery
Open

fix: handle unparseable @container conditions with error_recovery#1330
Larslllllll wants to merge 1 commit into
parcel-bundler:masterfrom
Larslllllll:fix/container-error-recovery

Conversation

@Larslllllll

Copy link
Copy Markdown

Summary

When parsing @container <name> { ... } (named container query with no condition), the lightningcss parser fails with "Unexpected end of input" because the { token is not recognized as a valid container condition.

This affects Turbopack (used by Next.js) which uses error_recovery: true in its CSS parsing configuration.

Root Cause

In src/parser.rs, the @container case has this error handling:

Err(e) => {
  if name.is_some() && input.is_exhausted() {
    // name only, no condition - allowed by new syntax
    AtRulePrelude::Container(name, None)
  } else {
    return Err(e);  // <-- This is the problem
  }
}

When input.is_exhausted() is false (i.e., there's a { token waiting), it returns an error instead of checking error_recovery.

Fix

Check self.options.error_recovery before returning the error. When error recovery is enabled, use ContainerCondition::Unknown for unparseable conditions:

Err(e) => {
  if name.is_some() && input.is_exhausted() {
    AtRulePrelude::Container(name, None)
  } else if self.options.error_recovery {
    self.options.warn(e);
    AtRulePrelude::Container(name, Some(
      ContainerCondition::Unknown(TokenList::parse(input, &self.options, 0)?)
    ))
  } else {
    return Err(e);
  }
}

Testing

The fix allows CSS like this to be parsed without error:

@supports (container-type: inline-size scroll-state) {
  @container scroll-content {
    .foo { max-width: 100cqw; }
  }
}

This CSS is generated by real-world libraries like nhsuk-frontend.

…enabled

When parsing `@container scroll-content { ... }` (named container query with
no condition), the parser would fail with "Unexpected end of input" because
the `{` token is not a valid container condition.

With error_recovery enabled (used by Turbopack), the parser should use
ContainerCondition::Unknown for unparseable conditions instead of propagating
the error, allowing the CSS to be processed despite unsupported syntax.

Fixes a build failure in Turbopack when processing CSS containing
`@container <name> { ... }` with named container queries.
@devongovett

Copy link
Copy Markdown
Member

We should probably fix the actual parser instead of relying on error recovery

@yisibl

yisibl commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

We should probably fix the actual parser instead of relying on error recovery

Yes, since I last updated the @container implementation, the syntax in the CSS specification has changed significantly, so I’ll implement the new syntax.

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.

3 participants