Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyzer crash Null check operator used on a null value #0 ElementAnnotationExtensions.targetKinds (package:analyzer/src/dart/element/extensions.dart #53681

Open
insinfo opened this issue Oct 3, 2023 · 7 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. crash Process exits with SIGSEGV, SIGABRT, etc. An unhandled exception is not a crash. P4 type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@insinfo
Copy link

insinfo commented Oct 3, 2023

I've been working on an AngularDart project of mine every day, and today I started getting this error "Null check operator used on a null value #0 ElementAnnotationExtensions.targetKinds"

I created a simple project to demonstrate the problem

https://github.com/insinfo/dart_bug_demo

PS C:\MyDartProjects\dart_bug_demo> webdev serve
[INFO] Building new asset graph completed, took 1.7s
[INFO] Checking for unexpected pre-existing outputs. completed, took 0ms
[INFO] Serving `web` on http://127.0.0.1:8080
[SEVERE] ngdart on web/main.dart: Null check operator used on a null value
#0      ElementAnnotationExtensions.targetKinds (package:analyzer/src/dart/element/extensions.dart:44:53)
#1      BestPracticesVerifier.visitAnnotation (package:analyzer/src/error/best_practices_verifier.dart:323:25)
#2      AnnotationImpl.accept (package:analyzer/src/dart/ast/ast.dart:318:50)
#3      NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:8777:20)
#4      AnnotatedNodeImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:154:17)
#5      ClassDeclarationImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:1823:11)
#6      RecursiveAstVisitor.visitClassDeclaration (package:analyzer/dart/ast/visitor.dart:793:10)
#7      BestPracticesVerifier.visitClassDeclaration (package:analyzer/src/error/best_practices_verifier.dart:404:13)
#8      ClassDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:1799:50)
#9      NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:8777:20)
#10     CompilationUnitImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:2455:21)
#11     RecursiveAstVisitor.visitCompilationUnit (package:analyzer/dart/ast/visitor.dart:817:10)
#12     CompilationUnitImpl.accept (package:analyzer/src/dart/ast/ast.dart:2448:50)
#13     LibraryAnalyzer._computeHints (package:analyzer/src/dart/analysis/library_analyzer.dart:344:10)
#14     LibraryAnalyzer._computeDiagnostics.<anonymous closure> (package:analyzer/src/dart/analysis/library_analyzer.dart:274:9)
#15     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
#16     LibraryAnalyzer._computeDiagnostics (package:analyzer/src/dart/analysis/library_analyzer.dart:273:13)        
#17     LibraryAnalyzer.analyze (package:analyzer/src/dart/analysis/library_analyzer.dart:92:5)
#18     AnalysisDriver._computeAnalysisResult.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1373:11)
<asynchronous suspension>
#19     PerformanceLog.runAsync (package:analyzer/src/dart/analysis/performance_logger.dart:50:14)
<asynchronous suspension>
#20     AnalysisDriver._computeErrors (package:analyzer/src/dart/analysis/driver.dart:1410:26)
<asynchronous suspension>
#21     AnalysisDriver.performWork (package:analyzer/src/dart/analysis/driver.dart:1118:20)
<asynchronous suspension>
#22     AnalysisDriverScheduler._run (package:analyzer/src/dart/analysis/driver.dart:2171:7)
<asynchronous suspension>


[INFO] 10.7s elapsed, 29/33 actions completed.^CDeseja finalizar o arquivo em lotes (S/N)? s
PS C:\MyDartProjects\dart_bug_demo>

angulardart-community/angular#67

@insinfo
Copy link
Author

insinfo commented Oct 3, 2023

It's strange because it's reporting the problem in this line 44 var index = kindObject.getField('index')!.toIntValue()!;

C:\Users\isaque.neves\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\analyzer-4.7.0\lib\src\dart\element\extensions.dart

If I edit this file by placing an if to check if kindObject.getField('index') != null the problem apparently disappears

after

/// Return the target kinds defined for this [ElementAnnotation].
  Set<TargetKind> get targetKinds {
    final element = this.element;
    InterfaceElement? interfaceElement;
    if (element is PropertyAccessorElement) {
      if (element.isGetter) {
        var type = element.returnType;
        if (type is InterfaceType) {
          interfaceElement = type.element2;
        }
      }
    } else if (element is ConstructorElement) {
      interfaceElement = element.enclosingElement3;
    }
    if (interfaceElement == null) {
      return const <TargetKind>{};
    }
    for (var annotation in interfaceElement.metadata) {
      if (annotation.isTarget) {
        var value = annotation.computeConstantValue()!;
        var kinds = <TargetKind>{};

        for (var kindObject in value.getField('kinds')!.toSetValue()!) {
          // We can't directly translate the index from the analyzed TargetKind
          // constant to TargetKinds.values because the analyzer from the SDK
          // may have been compiled with a different version of pkg:meta.
          // add this if
        if(kindObject.getField('index') != null){
            var index = kindObject.getField('index')!.toIntValue()!;
            var targetKindClass =
                (kindObject.type as InterfaceType).element2 as EnumElementImpl;
            // Instead, map constants to their TargetKind by comparing getter
            // names.
            var getter = targetKindClass.constants[index];
            var name = 'TargetKind.${getter.name}';

            var foundTargetKind = _targetKindsByName[name];
            if (foundTargetKind != null) {
              kinds.add(foundTargetKind);
            }
          }
        }
        return kinds;
      }
    }
    return const <TargetKind>{};
  }

insinfo added a commit to insinfo/analyzer-4.7.0 that referenced this issue Oct 4, 2023
@insinfo
Copy link
Author

insinfo commented Oct 4, 2023

I did a test here using dart 2.10.5, installed webdev 2.7.1 and stagehand 3.3.11 and created a new project with "stagehand web-angular" and ran it with "webdev serve" and it worked normally with angular 6.0.0

With dart 2.12.4 and angular 6.0.0 and webdev 2.7.2 I get the error below

PS C:\my_projects_dart\teste_bug> cls
PS C:\my_projects_dart\teste_bug> webdev serve
[INFO] Building new asset graph completed, took 1.4s
[INFO] Checking for unexpected pre-existing outputs. completed, took 1ms
[INFO] Serving `web` on http://127.0.0.1:8080
[SEVERE] angular:angular on web/main.dart: NoSuchMethodError: The method 'toIntValue' was called on null.
Receiver: null
Tried calling: toIntValue()
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      BestPracticesVerifier._targetKindsFor (package:analyzer/src/error/best_practices_verifier.dart:1554:52)      
#2      BestPracticesVerifier.visitAnnotation (package:analyzer/src/error/best_practices_verifier.dart:249:17)       
#3      AnnotationImpl.accept (package:analyzer/src/dart/ast/ast.dart:281:49)
#4      NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7553:20)
#5      AnnotatedNodeImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:149:17)
#6      MethodDeclarationImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:6980:11)
#7      RecursiveAstVisitor.visitMethodDeclaration (package:analyzer/dart/ast/visitor.dart:1067:10)
#8      BestPracticesVerifier.visitMethodDeclaration (package:analyzer/src/error/best_practices_verifier.dart:586:13)
#9      MethodDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:6976:49)
#10     NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7553:20)
#11     ClassDeclarationImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:1552:13)
#12     RecursiveAstVisitor.visitClassDeclaration (package:analyzer/dart/ast/visitor.dart:707:10)
#13     BestPracticesVerifier.visitClassDeclaration (package:analyzer/src/error/best_practices_verifier.dart:321:13) 
#14     ClassDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:1522:49)
#15     NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7553:20)
#16     CompilationUnitImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:2120:21)
#17     RecursiveAstVisitor.visitCompilationUnit (package:analyzer/dart/ast/visitor.dart:731:10)
#18     CompilationUnitImpl.accept (package:analyzer/src/dart/ast/ast.dart:2113:49)
#19     LibraryAnalyzer._computeHints (package:analyzer/src/dart/analysis/library_analyzer.dart:282:10)
#20     LibraryAnalyzer.analyzeSync.<anonymous closure> (package:analyzer/src/dart/analysis/library_analyzer.dart:167:9)
#21     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:397:8)
#22     LibraryAnalyzer.analyzeSync (package:analyzer/src/dart/analysis/library_analyzer.dart:166:13)
#23     LibraryAnalyzer.analyze (package:analyzer/src/dart/analysis/library_analyzer.dart:107:12)
#24     AnalysisDriver._computeAnalysisResult2.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1317:63)
#25     PerformanceLog.run (package:analyzer/src/dart/analysis/performance_logger.dart:32:15)
#26     AnalysisDriver._computeAnalysisResult2 (package:analyzer/src/dart/analysis/driver.dart:1294:20)
#27     AnalysisDriver._computeAnalysisResult.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1247:14)
#28     _rootRun (dart:async/zone.dart:1354:13)
#29     _CustomZone.run (dart:async/zone.dart:1258:19)
#30     _runZoned (dart:async/zone.dart:1788:10)
#31     runZoned (dart:async/zone.dart:1710:10)
#32     NullSafetyUnderstandingFlag.enableNullSafetyTypes (package:analyzer/dart/element/null_safety_understanding_flag.dart:42:12)
#33     AnalysisDriver._computeAnalysisResult (package:analyzer/src/dart/analysis/driver.dart:1246:40)
#34     AnalysisDriver._computeErrors (package:analyzer/src/dart/analysis/driver.dart:1372:41)
#35     AnalysisDriver.performWork (package:analyzer/src/dart/analysis/driver.dart:979:20)
#36     AnalysisDriverScheduler._run (package:analyzer/src/dart/analysis/driver.dart:2013:24)
<asynchronous suspension>


[WARNING] No actions completed for 15.0s, waiting on:
  - angular:angular on package:angular/src/core/linker/component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service.dart
  - angular:angular on package:angular/src/core/linker/dynamic_component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service_impl.dart
  - angular:angular on package:angular/src/security/sanitization_service.dart
  .. and 15 more

[WARNING] No actions completed for 15.1s, waiting on:
  - angular:angular on package:angular/src/core/linker/component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service.dart
  - angular:angular on package:angular/src/core/linker/dynamic_component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service_impl.dart
  - angular:angular on package:angular/src/security/sanitization_service.dart
  .. and 15 more

[WARNING] No actions completed for 15.1s, waiting on:
  - angular:angular on package:angular/src/core/linker/component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service.dart
  - angular:angular on package:angular/src/core/linker/dynamic_component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service_impl.dart
  - angular:angular on package:angular/src/security/sanitization_service.dart
  .. and 15 more

[WARNING] No actions completed for 15.1s, waiting on:
  - angular:angular on package:angular/src/core/linker/component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service.dart
  - angular:angular on package:angular/src/core/linker/dynamic_component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service_impl.dart
  - angular:angular on package:angular/src/security/sanitization_service.dart
  .. and 15 more

[WARNING] No actions completed for 15.0s, waiting on:
  - angular:angular on package:angular/src/core/linker/component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service.dart
  - angular:angular on package:angular/src/core/linker/dynamic_component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service_impl.dart
  - angular:angular on package:angular/src/security/sanitization_service.dart
  .. and 15 more

[WARNING] No actions completed for 15.1s, waiting on:
  - angular:angular on package:angular/src/core/linker/component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service.dart
  - angular:angular on package:angular/src/core/linker/dynamic_component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service_impl.dart
  - angular:angular on package:angular/src/security/sanitization_service.dart
  .. and 15 more

[WARNING] No actions completed for 15.1s, waiting on:
  - angular:angular on package:angular/src/core/linker/component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service.dart
  - angular:angular on package:angular/src/core/linker/dynamic_component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service_impl.dart
  - angular:angular on package:angular/src/security/sanitization_service.dart
  .. and 15 more

[WARNING] No actions completed for 15.0s, waiting on:
  - angular:angular on package:angular/src/core/linker/component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service.dart
  - angular:angular on package:angular/src/core/linker/dynamic_component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service_impl.dart
  - angular:angular on package:angular/src/security/sanitization_service.dart
  .. and 15 more

[WARNING] No actions completed for 15.0s, waiting on:
  - angular:angular on package:angular/src/core/linker/component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service.dart
  - angular:angular on package:angular/src/core/linker/dynamic_component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service_impl.dart
  - angular:angular on package:angular/src/security/sanitization_service.dart
  .. and 15 more

[WARNING] No actions completed for 15.1s, waiting on:
  - angular:angular on package:angular/src/core/linker/component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service.dart
  - angular:angular on package:angular/src/core/linker/dynamic_component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service_impl.dart
  - angular:angular on package:angular/src/security/sanitization_service.dart
  .. and 15 more

[WARNING] No actions completed for 15.1s, waiting on:
  - angular:angular on package:angular/src/core/linker/component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service.dart
  - angular:angular on package:angular/src/core/linker/dynamic_component_loader.dart
  - angular:angular on package:angular/src/security/dom_sanitization_service_impl.dart
  - angular:angular on package:angular/src/security/sanitization_service.dart
  .. and 15 more

[WARNING] No actions completed for 15.1s, waitin

This is very strange because all my Angular 6.0.0 projects normally ran on Dart 2.12.4 until last month

see that the error is exactly in kindObject.getField('index') which is returning null

@lrhn lrhn added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Oct 6, 2023
@srawlins
Copy link
Member

These are fairly old versions of Dart (~2.10) and analyzer (~4.7.0). We don't generally backport bug fixes. I recommend finding a way to use newer analyzer releases, hopefully >= 6.0.0.

@srawlins srawlins added the P4 label Oct 17, 2023
@greglittlefield-wf
Copy link
Contributor

greglittlefield-wf commented Oct 18, 2023

I also ran into what seems like the same issue on Dart 2.18.7. Specifically, resolving to meta>=1.10.0 and test >=1.21.2 would cause any files with @TestOn annotations in them to silently fail to analyze, as if they were excluded.

This particular issue seemed to be related to by the the addition of @Target({TargetKind.library}) to class TestOn in test 1.21.2, and the change of TargetKind from an enum to a class in meta 1.10.0.

No errors were surfaced via dart analyze or in the IDE (and syntax highlighting also wouldn't update), but if you went to the analyzer diagnostics page from your IDE and clicked on the AST link for the file, you'd get pretty much the same error as this issue's description:

Null check operator used on a null value
#0 ElementAnnotationExtensions.targetKinds (package:analyzer/src/dart/element/extensions.dart:42:51)
...
(expand to show full error)
Null check operator used on a null value
#0 ElementAnnotationExtensions.targetKinds (package:analyzer/src/dart/element/extensions.dart:42:51)
#1 BestPracticesVerifier.visitAnnotation (package:analyzer/src/error/best_practices_verifier.dart:298:25)
#2 AnnotationImpl.accept (package:analyzer/src/dart/ast/ast.dart:304:50)
#3 NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:8408:20)
#4 AnnotatedNodeImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:149:17)
#5 LibraryDirectiveImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:7366:11)
#6 RecursiveAstVisitor.visitLibraryDirective (package:analyzer/dart/ast/visitor.dart:1138:10)
#7 LibraryDirectiveImpl.accept (package:analyzer/src/dart/ast/ast.dart:7362:50)
#8 NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:8408:20)
#9 CompilationUnitImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:2316:19)
#10 RecursiveAstVisitor.visitCompilationUnit (package:analyzer/dart/ast/visitor.dart:784:10)
#11 CompilationUnitImpl.accept (package:analyzer/src/dart/ast/ast.dart:2310:50)
#12 LibraryAnalyzer._computeHints (package:analyzer/src/dart/analysis/library_analyzer.dart:338:10)
#13 LibraryAnalyzer._computeDiagnostics.<anonymous closure> (package:analyzer/src/dart/analysis/library_analyzer.dart:268:9)
#14 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
#15 LibraryAnalyzer._computeDiagnostics (package:analyzer/src/dart/analysis/library_analyzer.dart:267:13)
#16 LibraryAnalyzer.analyze (package:analyzer/src/dart/analysis/library_analyzer.dart:92:5)
#17 AnalysisDriver._computeAnalysisResult.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1337:11) <asynchronous suspension>
#18 PerformanceLog.runAsync (package:analyzer/src/dart/analysis/performance_logger.dart:50:14) <asynchronous suspension>
#19 AnalysisDriver.performWork (package:analyzer/src/dart/analysis/driver.dart:1045:24) <asynchronous suspension>
#20 AnalysisDriverScheduler._run (package:analyzer/src/dart/analysis/driver.dart:2105:7) <asynchronous suspension>

#0      AnalysisDriver._computeAnalysisResult.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1366:9)
<asynchronous suspension>
#1      PerformanceLog.runAsync (package:analyzer/src/dart/analysis/performance_logger.dart:50:14)
<asynchronous suspension>
#2      AnalysisDriver.performWork (package:analyzer/src/dart/analysis/driver.dart:1045:24)
<asynchronous suspension>
#3      AnalysisDriverScheduler._run (package:analyzer/src/dart/analysis/driver.dart:2105:7)
<asynchronous suspension>

Things that fixed it:

  • restricting to meta 1.9.1 or lower
  • restricting to test 1.21.1 or lower
  • switching to Dart 2.19.0+ (even without changing any dependencies, including transitive dependencies like analyzer)

Since Dart 2.19 fixed it, I'm fine with that being the path forward, since we're in the process of upgrading the affected packages anyways.

@insinfo Perhaps restricting to a lower meta or test could help you work around the issue? It looks like meta 1.10.0 was published on Sep 6, which seems to line up with when things stopped working for you.

@greglittlefield-wf
Copy link
Contributor

For posterity, I just confirmed that other annotations that use TargetKind exhibit the same issue as TestOn, which includes many of the annotations in the meta package itself (@mustCallSuper , @mustBeOverridden, and @useResult) and several other annotations from the test package.

So, restricting to meta 1.9.1 or lower is the best option to avoid this issue.

@insinfo
Copy link
Author

insinfo commented Oct 20, 2023

@greglittlefield-wf

the first moment I detected this problem I just created a repository with the
analyzer and adding null checking and applying with dependency_overrides

dependency_overrides:
  analyzer:
    git:
      url: https://github.com/insinfo/analyzer-4.7.0
      ref: main   

This problem affected all the projects I work on ranging from Dart 2.12 - 2.18.7, it just didn't affect Dart 2.10 projects

@insinfo
Copy link
Author

insinfo commented Oct 20, 2023

I think the change made in the meta 1.10.0 package that transformed TargetKind enum to a class was a big mistake, I think this change should have been published as meta 2.0.0 as a compatibility break, to avoid these problems

wsv-accidis added a commit to wsv-accidis/sjoslaget that referenced this issue Oct 30, 2023
@srawlins srawlins added the crash Process exits with SIGSEGV, SIGABRT, etc. An unhandled exception is not a crash. label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. crash Process exits with SIGSEGV, SIGABRT, etc. An unhandled exception is not a crash. P4 type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants