Skip to content

IconTreeShakerException: Invalid ConstFinder result #147189

Closed

Description

@Piinks

Working on a new widget in the widgets layer, I defined a const Icon using the ascii value for the IconData:

const Icon(IconData(0x25BA));

Since the Icon widget is defined in the widgets layer, I thought this should work fine. Running an app with this builds and works fine.

When I pushed a PR, CI was very unhappy. This use of Icon results in an error from the IconTreeShaker.

flutter build apk --release

Target aot_android_asset_bundle failed: IconTreeShakerException: Invalid ConstFinder result. Expected "fontPackage" to be a String, "fontFamily" to be a String, and "codePoint" to be an int, got: {codePoint: 9658, fontFamily: null, fontPackage: null, matchTextDirection: false, fontFamilyFallback: null}.

To disable icon tree shaking, pass --no-tree-shake-icons to the requested flutter build command


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileFlutterBuildRelease'.
> Process 'command '/Users/katelovett/_github/flutter/bin/flutter'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 14s
Running Gradle task 'assembleRelease'...                           14.7s
Gradle task assembleRelease failed with exit code 1

I figured I was holding it wrong and was not allowed to do this, but @goderbauer suspects this is a bug in the icon tree shaker script.

This can be reproduced with the following code and the command flutter build apk --release

import 'package:flutter/material.dart';

void main() => runApp(const SliverTreeExampleApp());

class SliverTreeExampleApp extends StatelessWidget {
  const SliverTreeExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: SliverTreeExample(),
    );
  }
}

class SliverTreeExample extends StatelessWidget {
  const SliverTreeExample({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(child: Icon(IconData(0x25BA)),),
    );
  }
}

This is an example build where I saw this in CI: https://ci.chromium.org/ui/p/flutter/builders/try/Linux%20build_tests_2_3/33418/overview

Activity

  1. added
    team-toolOwned by Flutter Tool team
    toolAffects the "flutter" command-line tool. See also t: labels.
    a: buildBuilding flutter applications with the tool
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    on Apr 22, 2024
  2. jonahwilliams commented on Apr 22, 2024

    @jonahwilliams
    Contributor

    I actually think this is WAI. Without any font information associated with that Icon there is no way for icon tree shaker to work without retaining that codepoint for any possible icon font. You should provide the fontFamily/fontPackage that the code point is expected to be from.

  3. Piinks commented on Apr 22, 2024

    @Piinks
    ContributorAuthor

    At first I thought we just couldn't do this in the framework from the widgets layer. Is there a built in fontFamily/fontPackage I can point to at that layer?

  4. Piinks commented on Apr 22, 2024

    @Piinks
    ContributorAuthor

    Or maybe better question, when I run it in debug mode and it works alright, what fontFamily/fontPackage is it using by default?

  5. jonahwilliams commented on Apr 22, 2024

    @jonahwilliams
    Contributor

    The icon tree shake doesn't run in debug mode. Which Icon are you trying to refer to? If its a Material Icon you need to specify fontFamily: 'MaterialIcons'

    If you don't specify a font then you'll get whatever random glyph happens to be in the first font the OS font manager selects, which is probably going to be a tofu character.

  6. Piinks commented on Apr 22, 2024

    @Piinks
    ContributorAuthor

    The icon tree shake doesn't run in debug mode.

    Yeah, and it works as expected..

    Screenshot 2024-04-22 at 4 25 44 PM

    I'm just using a little arrow icon, 0x25BA. I am trying to avoid material because I am in the widgets layer.

    Looking at the code, the Icon itself just boils down to a TextSpan and does not require a font family or font package. Can it not just fallback like regular text? That is what happens in debug, and I get the arrow I expect.

  7. jonahwilliams commented on Apr 22, 2024

    @jonahwilliams
    Contributor

    Whether or not you add the MaterialIcons string to the IconData, you're depending on the material icons font being present. If you don't specify that, you're relying on the fact that the material icons font is present and is the only font with a 0x25BA code point.

  8. Piinks commented on Apr 22, 2024

    @Piinks
    ContributorAuthor

    Oh interesting. I didn't take the value from material icons, it's just unicode. https://unicodes.jessetane.com/%E2%96%BA

    This feels odd, that an Icon will use same fallback font Flutter uses for text (Roboto), but the tree shaker does not allow it.

    I don't think it is using the material icons font, I have specifically excluded anything material.

  9. jonahwilliams commented on Apr 22, 2024

    @jonahwilliams
    Contributor

    Oh I C! So essentially you want whatever is on the system. You could try specifying a font, but maybe the right solution is some kind of tree shaker opt out?

  10. goderbauer commented on Apr 22, 2024

    @goderbauer
    Member

    but maybe the right solution is some kind of tree shaker opt out?

    This is what I was thinking when discussing this with Kate. It's supposed to use a system font that isn't bundled with the app so the tree shaker should just ... let it be.

  11. jonahwilliams commented on Apr 22, 2024

    @jonahwilliams
    Contributor

    We could make the script ignore IconData without a fontFamil or fontPackage.

  12. Piinks commented on Apr 22, 2024

    @Piinks
    ContributorAuthor

    Happy to send a PR, wasn't sure if that would pass the sniff test!

  13. self-assigned this
    on Apr 22, 2024
  14. github-actions commented on May 7, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  15. locked as resolved and limited conversation to collaborators on May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: buildBuilding flutter applications with the toolhas reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool teamwaiting for PR to land (fixed)A fix is in flight

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions