dart-lang / build

A build system for Dart written in Dart
https://pub.dev/packages/build
BSD 3-Clause "New" or "Revised" License
791 stars 210 forks source link

Build runner fails with Dart 3 switch expressions that doesn't include `case` keywords. #3612

Closed rasitayaz closed 1 year ago

rasitayaz commented 1 year ago
dart --version
Dart SDK version: 3.1.5 (stable) (Tue Oct 24 04:57:17 2023 +0000) on "macos_arm64"

I'm using build_runner: ^2.4.6.

When I run dart run build_runner build --delete-conflicting-outputs, with the following piece of code:

import 'package:flutter/material.dart';

enum ButtonType { outlined, cta, alt }

const type = ButtonType.cta;

final backgroundColor = switch (type) {
  ButtonType.cta => Colors.blue,
  ButtonType.alt => Colors.blue.withOpacity(0.2),
  ButtonType.outlined => Colors.transparent,
};

I'm getting following errors:

test.dart:7:25: Expected an identifier.
test.dart:7:23: Expected to find ';'.
test.dart:7:25: 'switch' can't be used as an identifier because it's a keyword.
jakemac53 commented 1 year ago

@rasitayaz what version of analyzer are you using? Are you getting a warning at the top of your build about an outdated analyzer?

It would also help to have a full stack trace and build log, this one doesn't provide the context necessary to diagnose.

rasitayaz commented 1 year ago

Sorry for not including full stack trace, here it is:

build runner output ``` [INFO] Generating build script completed, took 156ms [INFO] Precompiling build script... completed, took 2.4s [INFO] Building new asset graph completed, took 335ms [INFO] Checking for unexpected pre-existing outputs. completed, took 1ms [INFO] Generating SDK summary completed, took 2.2s [SEVERE] copy_with_extension_gen on lib/ui/pages/flick_page.dart: This builder requires Dart inputs without syntax errors. However, package:app/ui/pages/flick_page.dart (or an existing part) contains the following errors. flick_page.dart:60:18: Expected an identifier. flick_page.dart:60:11: Expected to find ';'. flick_page.dart:61:13: Expected to find 'case'. Try fixing the errors and re-running the build. [SEVERE] copy_with_extension_gen on lib/ui/pages/person_page.dart: This builder requires Dart inputs without syntax errors. However, package:app/ui/pages/person_page.dart (or an existing part) contains the following errors. person_page.dart:47:18: Expected an identifier. person_page.dart:47:11: Expected to find ';'. person_page.dart:48:13: Expected to find 'case'. Try fixing the errors and re-running the build. [WARNING] copy_with_extension_gen on lib/ui/pages/gallery_page.dart: Your current `analyzer` version may not fully support your current SDK version. Analyzer language version: 2.19.0 SDK language version: 3.1.0 Please update to the latest `analyzer` version (6.3.0) by running `flutter packages upgrade`. If you are not getting the latest version by running the above command, you can try adding a constraint like the following to your pubspec to start diagnosing why you can't get the latest version: dev_dependencies: analyzer: ^6.3.0 [SEVERE] copy_with_extension_gen on lib/ui/widgets/atomic/button.dart: This builder requires Dart inputs without syntax errors. However, package:app/ui/widgets/atomic/button.dart (or an existing part) contains the following errors. button.dart:36:11: Expected an identifier. button.dart:36:9: Expected to find ';'. button.dart:37:13: Expected to find 'case'. And 3 more... Try fixing the errors and re-running the build. [SEVERE] copy_with_extension_gen on lib/core/cubit/entity/fetch_state.dart: This builder requires Dart inputs without syntax errors. However, package:app/core/cubit/entity/fetch_state.dart (or an existing part) contains the following errors. fetch_state.dart:3:1: Variables must be declared using the keywords 'const', 'final', 'var' or a type name. fetch_state.dart:3:1: Expected to find ';'. Try fixing the errors and re-running the build. [INFO] Running build completed, took 5.7s [INFO] Caching finalized dependency graph completed, took 43ms [SEVERE] Failed after 5.8s ```

I was able to get the warning about outdated analyzer after running flutter clean and flutter pub get.

Seems like I'm not using the latest version of analyzer: analyzer: ^4.7.0

my pubspec.yaml ```yaml name: app description: description publish_to: "none" version: 0.1.0+1 environment: sdk: ">=3.0.0 <4.0.0" dependencies: back_button_interceptor: ^7.0.0 balanced_text: ^0.0.3 bloc: ^8.1.2 conditional_wrap: ^3.0.0 copy_with_extension: ^5.0.4 dart_jsonwebtoken: ^2.12.1 decorated_icon: ^1.2.1 dismissible_page: ^1.0.2 equatable: ^2.0.5 fading_edge_scrollview: ^3.0.0 flutter: sdk: flutter flutter_bloc: ^8.1.3 flutter_launcher_icons: ^0.13.1 flutter_localizations: sdk: flutter flutter_spinkit: ^5.2.0 flutter_sticky_header: ^0.6.5 flutter_svg: ^2.0.9 font_awesome_flutter: ^10.6.0 http: ^1.1.0 intl: ^0.18.1 photo_view: ^0.14.0 pie_menu: ^2.0.2 recase: ^4.1.0 sliver_tools: ^0.2.12 sorted: ^2.1.0 url_launcher: ^6.2.1 dev_dependencies: analyzer: ^4.7.0 build_runner: ^2.4.6 copy_with_extension_gen: ^5.0.4 dart_code_metrics: ^4.19.2 flutter_lints: ^3.0.1 flutter_test: sdk: flutter # flutter pub run flutter_launcher_icons:main flutter_icons: android: "launcher_icon" ios: true image_path: "assets/launcher/icon.png" adaptive_icon_background: "#2c2e3e" adaptive_icon_foreground: "assets/launcher/icon_foreground.png" flutter: uses-material-design: true assets: - assets/keys.json - assets/images/ fonts: - family: CustomIcons fonts: - asset: assets/fonts/CustomIcons.ttf - family: Poppins fonts: - asset: assets/fonts/poppins/Poppins-Regular.ttf - asset: assets/fonts/poppins/Poppins-Italic.ttf style: italic - asset: assets/fonts/poppins/Poppins-Medium.ttf weight: 500 - asset: assets/fonts/poppins/Poppins-ExtraBold.ttf weight: 800 ```

When I try to upgrade analyzer to 6.3.0, I'm getting the following error:

Because every version of flutter_test from sdk depends on meta 1.9.1 and analyzer >=6.3.0 depends on meta ^1.11.0, flutter_test from sdk is incompatible with analyzer >=6.3.0.
So, because app depends on both analyzer ^6.3.0 and flutter_test from sdk, version solving failed.
jakemac53 commented 1 year ago

You will need the latest analyzer to resolve this issue. Can you update your flutter version possibly? If not you can try using a dependency override to force the latest analyzer.

rasitayaz commented 1 year ago

I'm already using the latest flutter version right now.

flutter upgrade                                         
Flutter is already up to date on channel stable
Flutter 3.13.9 • channel stable • https://github.com/flutter/flutter.git
Framework • revision d211f42860 (3 weeks ago) • 2023-10-25 13:42:25 -0700
Engine • revision 0545f8705d
Tools • Dart 3.1.5 • DevTools 2.25.0

but somehow can't update the analyzer to the latest version for this project like I mentioned in my previous reply. how do I use a dependency override to force the latest analyzer?

jakemac53 commented 1 year ago

but somehow can't update the analyzer to the latest version for this project like I mentioned in my previous reply. how do I use a dependency override to force the latest analyzer?

in your pubspec.yaml:

dependency_overrides:
  analyzer: ^6.3.0
jakemac53 commented 1 year ago

I'm already using the latest flutter version right now.

That is not good if it is pinned to a version of analyzer that doesn't support the language features that ship with that version.... 😱

rasitayaz commented 1 year ago

in your pubspec.yaml:

dependency_overrides:
  analyzer: ^6.3.0

still getting the same error unfortunately:

Because analyzer >=6.3.0 depends on meta ^1.11.0 and every version of flutter_test from sdk depends on meta 1.9.1, analyzer >=6.3.0 is incompatible with flutter_test from sdk.
So, because app depends on both flutter_test from sdk and analyzer ^6.3.0, version solving failed.
exit code 1
jakemac53 commented 1 year ago

Try also adding an override for meta:

dependency_overrides:
  analyzer: ^6.3.0
  meta: ^1.11.0
jakemac53 commented 1 year ago

Note that the constraint on meta was increased here https://github.com/flutter/flutter/commit/3e60999b1155bb7784f2ff6c6372eb535fa43d82. I don't know anything about how flutter actually does releases, or what the next release will be, but the first release version this is tagged with is 3.16.0-10.0.pre, so it will likely be a part of the 3.16 release? I don't know if that is the next stable release though.

jakemac53 commented 1 year ago

@rasitayaz note that I also checked with the analyzer team, and it looks like version 5.8.0 of the analyzer should also work, the meta constraint at that point was only ^1.7.0.

jakemac53 commented 1 year ago

I am going to close this issue but feel free to chime back in if you are still blocked

rasitayaz commented 1 year ago

Thank you so much for your help! Overriding both meta & analyzer worked. Though I had to set analyzer to 5.8.0 as you mentioned in your other comment. With 6.3.0 I was getting a different error, dropping it here in case it would be helpful for the development team:

error log with analyzer 6.3.0 ``` Failed to build build_runner:build_runner: ../../../.pub-cache/hosted/pub.dev/build_resolvers-2.0.10/lib/src/resolver.dart:259:32: Error: The getter 'parts2' isn't defined for the class 'LibraryElement'. - 'LibraryElement' is from 'package:analyzer/dart/element/element.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/dart/element/element.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'parts2'. for (final part in element.parts2) { ^^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:577:18: Error: The getter 'exceptionParameter2' isn't defined for the class 'CatchClause'. - 'CatchClause' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'exceptionParameter2'. visit(node.exceptionParameter2); ^^^^^^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:579:18: Error: The getter 'stackTraceParameter2' isn't defined for the class 'CatchClause'. - 'CatchClause' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'stackTraceParameter2'. visit(node.stackTraceParameter2); ^^^^^^^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:601:16: Error: The getter 'name2' isn't defined for the class 'ClassDeclaration'. - 'ClassDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. token(node.name2); ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:620:18: Error: The getter 'name2' isn't defined for the class 'ClassTypeAlias'. - 'ClassTypeAlias' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. token(node.name2); ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:767:16: Error: The getter 'name2' isn't defined for the class 'ConstructorDeclaration'. - 'ConstructorDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. token(node.name2); ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:984:16: Error: The getter 'name2' isn't defined for the class 'EnumConstantDeclaration'. - 'EnumConstantDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. token(node.name2); ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:1009:16: Error: The getter 'name2' isn't defined for the class 'EnumDeclaration'. - 'EnumDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. token(node.name2); ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:1253:14: Error: The getter 'name2' isn't defined for the class 'ExtensionDeclaration'. - 'ExtensionDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. if (node.name2 != null) { ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:1255:18: Error: The getter 'name2' isn't defined for the class 'ExtensionDeclaration'. - 'ExtensionDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. token(node.name2); ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:1610:18: Error: The getter 'name2' isn't defined for the class 'FunctionTypeAlias'. - 'FunctionTypeAlias' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. node.name2, ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:1613:49: Error: The getter 'name2' isn't defined for the class 'FunctionTypeAlias'. - 'FunctionTypeAlias' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. node.returnType?.beginToken ?? node.name2); ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:1620:41: Error: The getter 'name2' isn't defined for the class 'FunctionTypeAlias'. - 'FunctionTypeAlias' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. node.returnType, null, node.name2, null, node.parameters); ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:1630:18: Error: The getter 'name2' isn't defined for the class 'FunctionTypeAlias'. - 'FunctionTypeAlias' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. token(node.name2); ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:1672:62: Error: The getter 'name2' isn't defined for the class 'GenericTypeAlias'. - 'GenericTypeAlias' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. _visitGenericTypeAliasHeader(node.typedefKeyword, node.name2, ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:2025:18: Error: The getter 'name' isn't defined for the class 'LibraryDirective'. - 'LibraryDirective' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name'. visit(node.name); ^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:2124:16: Error: The getter 'name2' isn't defined for the class 'MixinDeclaration'. - 'MixinDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. token(node.name2); ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:2161:16: Error: The getter 'name' isn't defined for the class 'NamedType'. - 'NamedType' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name'. visit(node.name); ^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:2507:18: Error: The getter 'name2' isn't defined for the class 'TypeParameter'. - 'TypeParameter' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. token(node.name2); ^^^^^ ../../../.pub-cache/hosted/pub.dev/dart_style-2.2.4/lib/src/source_visitor.dart:2524:16: Error: The getter 'name2' isn't defined for the class 'VariableDeclaration'. - 'VariableDeclaration' is from 'package:analyzer/src/dart/ast/ast.dart' ('../../../.pub-cache/hosted/pub.dev/analyzer-6.3.0/lib/src/dart/ast/ast.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'name2'. token(node.name2); ^^^^^ ```
jakemac53 commented 1 year ago

The error looks like just a result of the override causing you to get an invalid combination of packages (as is generally the danger with overrides). So probably nothing to do there, but thanks :)