Repository navigation
ADFA-6172: Keep Pebble whole through R8, so templated docs pages render - #2056
Conversation
Release builds minify the app module, and R8's shrinker removes members Pebble
only reaches by reflection. Two of its three reflective paths were broken:
- ExpressionParser instantiates unary operator nodes from a class literal
(CoreExtension registers `new UnaryOperatorImpl("not", 500,
UnaryNotExpression.class)`, the parser calls Class.newInstance() later). With no
traceable `new`, R8 strips the constructor and both evaluate() overrides and marks
the class abstract, so a template containing `{% if not x %}` fails to parse with
"java.lang.Class<...UnaryNotExpression> cannot be instantiated".
- MemberCacheUtils resolves template attributes with getField()/getMethods(), which
is how `loop.last` reaches ForNode$LoopVariables. Nothing calls its five getters
from bytecode, so R8 removes them and `loop.last` evaluates to null with no error
- wrong output rather than a crash.
layout.pebble hits both: 3,241 of the templated pages in documentation.db render
through it, and it uses `{%- if not loop.last %}`.
Keep the whole library rather than the two classes we caught. Attribute resolution
makes anything a layout can name reachable invisibly - both expression hierarchies,
LoopVariables and its lazy values, the `template` and `_context` globals, and every
filter, test and function a future template might use - and BinaryOperatorImpl still
exposes the same Class-based constructor to any Extension that ZipRecipeExecutor
loads through ServiceLoader. Enumerating that surface is how this recurs. Measured
against R8 8.8.34: ~31 KB more than keeping only the two known classes.
Verified with R8 8.8.34, the version AGP 8.8.2 uses, run against this rules file: an
80-check sweep of Pebble's operators, filters, tests, functions, tags and attribute
access produces output identical to an unshrunk run, where the previous rules gave
four parse crashes and silently empty loop variables. Debug builds do not minify, so
this only changes release output.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.
Tip: disable this comment in your organization's Code Review settings.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review. 📝 Summary
WalkthroughThe ProGuard configuration now keeps the full Pebble library during shrinking. Comments document reflective class construction and attribute resolution. Current template contexts use JDK map types. ChangesPebble template shrinking
Priority: ➖ Normal Estimated code review effort: 1 (Trivial) | ~5 minutes Change: Bug fix Suggested reviewers: Merge Risk: ⚪ Minimal · up to The change preserves Pebble’s reflection-dependent classes for release builds without an identified regression, so it is ready to merge. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
A rabbit reads each line, Comment |
ADFA-6172
Release builds serve most of the Kotlin API reference as an error page instead of content. Requesting
k/kotlin-stdlib/kotlin/apply.htmlfails with:Neither the templates nor
documentation.dbare at fault. This is R8's shrinker.Cause
Pebble reaches parts of itself only by reflection, so the shrinker sees no bytecode caller and removes them. There are exactly three such paths in 4.1.1, and two were broken:
ExpressionParser:120instantiates unary operator nodes from a class literal.CoreExtensionregistersnew UnaryOperatorImpl("not", 500, UnaryNotExpression.class)and the parser later callsClass.newInstance(). With no traceablenew, R8 strips the constructor and bothevaluate()overrides and marks the class abstract, so any template containing{% if not x %}fails to parse. Against the current rules file, R8 emitted:MemberCacheUtils:66/:199resolve every template attribute withgetField()/getMethods(). That is howloop.lastreachesForNode$LoopVariables, whose five getters (isFirst,isLast,getIndex,getLength,getRevindex) nothing calls from bytecode. R8 removes all five, andloop.lastthen evaluates to null with no exception — wrong output rather than a crash, which is harder to notice than the failure above.BinaryOperatorImpl:38has the sameClass-based constructor as (1), butCoreExtensionregisters binary operators asOrExpression::newsuppliers that R8 traces, so nothing is broken today. It remains public API, andZipRecipeExecutorloads template-suppliedExtensions throughServiceLoader.-dontoptimizedoes not help here; this is the shrinker, not the optimize pass.Fix
Kept whole rather than per-class. Attribute resolution makes anything a layout can name reachable invisibly — both expression hierarchies,
LoopVariablesand itsLazyLength/LazyRevIndexvalues, thetemplateand_contextglobals (PebbleTemplateImpl,GlobalContext), and every filter, test, function and node type a future template might use. Enumerating that surface is how a third instance of this bug gets found in production.Scope
Debug builds set
isMinifyEnabled = false, so this only ever affected release, which is why it did not surface in development. Indocumentation.db:layout.pebble{%- if not loop.last %}, so it hits both defectspage.pebis not emptyformnav.peblayout.pebblereadsloop.*20 times (11 ×loop.first, 7 ×loop.last, 2 ×loop.index), all of which were silently resolving to null.Verification
R8 8.8.34 — the version AGP 8.8.2 resolves — run against this rules file in
--classfilemode with the app's real flags (-dontobfuscate,-dontoptimize,-ignorewarnings). An 80-check sweep of Pebble's operators, filters, tests, functions and tags, plusinclude/import/from/extends/block/embed/cache/parallel/autoescape,loopvariables, and attribute access on maps, lists and a POJO:not,+,-) and silently emptyloop.index/first/last/lengthThree checks fail in every run including unshrunk (
is number,is string,is equalTodo not exist in Pebble 4.1.1); no keep rule affects those.Known trade-off
Keeping the package costs ~31 KB over keeping only the two classes that were demonstrably broken, and ~39 KB over keeping nothing (402,013 B vs 370,685 B vs 362,615 B of shrunk output in the harness above). Taken deliberately: the narrow version is what produced defect (2) after defect (1) was already fixed.
Sites checked
grepover the Pebble 4.1.1 sources finds four reflective call sites in total —ExpressionParser:120,BinaryOperatorImpl:38,MemberCacheUtils:66andMemberCacheUtils:199— and noServiceLoaderorClass.forNameanywhere. Everything else (all ~30 filters, the tests, functions, token parsers and node classes) is registered with a directnewor::newinCoreExtension/EscaperExtensionand needs no rule.One gap this cannot close, noted in a comment beside the rule: objects we pass to
PebbleEngine.evaluate()are read by the same reflection. Both call sites passMap<String, ...>of JDK types today —DocumentationContentSource.renderNameda Gson-parsed map,ZipRecipeExecutor.renderTemplateEntryvalues fromresolveString— which Pebble'sMapResolverreads without reflection, so no app class needs a rule yet. The first POJO or data class placed in a template context will need one, or its properties render empty.🤖 Generated with Claude Code