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

Unhandled exception: Crash when compiling: Null check operator used on a null value #54267

Closed
cosminbodnariuc opened this issue Dec 7, 2023 · 7 comments
Assignees
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. cfe-crashes Crashes in the CFE type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@cosminbodnariuc
Copy link

cosminbodnariuc commented Dec 7, 2023

I'm receiving the following error when trying to compile the dart code within a Dockerfile:

Unhandled exception:
Crash when compiling:
Null check operator used on a null value

#0      CfeEnumOperations.getEnumElementValue (package:front_end/src/fasta/kernel/exhaustiveness.dart:323)
#1      EnumInfo._createEnumElements (package:_fe_analyzer_shared/src/exhaustiveness/types/enum.dart:60)
#2      EnumInfo.enumElements (package:_fe_analyzer_shared/src/exhaustiveness/types/enum.dart:47)
#3      EnumStaticType._createEnumElements (package:_fe_analyzer_shared/src/exhaustiveness/types/enum.dart:97)
#4      EnumStaticType.enumElements (package:_fe_analyzer_shared/src/exhaustiveness/types/enum.dart:92)
#5      EnumStaticType.getSubtypes (package:_fe_analyzer_shared/src/exhaustiveness/types/enum.dart:90)
#6      _Checker._unmatched (package:_fe_analyzer_shared/src/exhaustiveness/exhaustive.dart:131)
#7      reportErrors (package:_fe_analyzer_shared/src/exhaustiveness/exhaustive.dart:43)
#8      ConstantsTransformer._checkExhaustiveness (package:front_end/src/fasta/kernel/constant_evaluator.dart:1550)
#9      ConstantsTransformer.visitPatternSwitchStatement (package:front_end/src/fasta/kernel/constant_evaluator.dart:1517)
#10     PatternSwitchStatement.accept1 (package:kernel/src/ast/patterns.dart:1859)
#11     RemovingTransformer.transform (package:kernel/visitor.dart:1883)
#12     LabeledStatement.transformOrRemoveChildren (package:kernel/ast.dart:9324)
#13     RemovingTransformer.defaultTreeNode (package:kernel/visitor.dart:2166)
#14     TreeVisitor1Default.defaultExpression (package:kernel/visitor.dart:739)
#15     StatementVisitor1DefaultMixin.visitLabeledStatement (package:kernel/visitor.dart:2559)
#16     LabeledStatement.accept1 (package:kernel/ast.dart:9309)
#17     RemovingTransformer.transformOrRemove (package:kernel/visitor.dart:1919)
#18     ConstantsTransformer.visitBlock (package:front_end/src/fasta/kernel/constant_evaluator.dart:820)
#19     Block.accept1 (package:kernel/ast.dart:9104)
#20     RemovingTransformer.transform (package:kernel/visitor.dart:1883)
#21     ConstantsTransformer.visitFunctionNode (package:front_end/src/fasta/kernel/constant_evaluator.dart:643)
#22     FunctionNode.accept1 (package:kernel/ast.dart:3800)
#23     RemovingTransformer.transform (package:kernel/visitor.dart:1883)
#24     ConstantsTransformer.visitProcedure.<anonymous closure> (package:front_end/src/fasta/kernel/constant_evaluator.dart:560)
#25     ConstantEvaluator.withNewEnvironment (package:front_end/src/fasta/kernel/constant_evaluator.dart:5351)
#26     ConstantsTransformer.visitProcedure (package:front_end/src/fasta/kernel/constant_evaluator.dart:558)
#27     Procedure.accept1 (package:kernel/ast.dart:3170)
#28     RemovingTransformer.transformOrRemove (package:kernel/visitor.dart:1919)
#29     RemovingTransformer.transformList (package:kernel/visitor.dart:2143)
#30     RemovingTransformer.transformProcedureList (package:kernel/visitor.dart:2035)
#31     ConstantsTransformer.visitClass.<anonymous closure> (package:front_end/src/fasta/kernel/constant_evaluator.dart:513)
#32     ConstantEvaluator.withNewEnvironment (package:front_end/src/fasta/kernel/constant_evaluator.dart:5351)
#33     ConstantsTransformer.visitClass (package:front_end/src/fasta/kernel/constant_evaluator.dart:508)
#34     Class.accept1 (package:kernel/ast.dart:1447)
#35     RemovingTransformer.transformOrRemove (package:kernel/visitor.dart:1919)
#36     RemovingTransformer.transformList (package:kernel/visitor.dart:2143)
#37     RemovingTransformer.transformClassList (package:kernel/visitor.dart:1998)
#38     ConstantsTransformer.convertLibrary (package:front_end/src/fasta/kernel/constant_evaluator.dart:460)
#39     transformLibraries (package:front_end/src/fasta/kernel/constant_evaluator.dart:112)
#40     KernelTarget.runBuildTransformations (package:front_end/src/fasta/kernel/kernel_target.dart:1532)
#41     KernelTarget.buildComponent.<anonymous closure> (package:front_end/src/fasta/kernel/kernel_target.dart:624)
<asynchronous suspension>
#42     withCrashReporting (package:front_end/src/fasta/crash.dart:133)
<asynchronous suspension>
#43     KernelTarget.buildComponent (package:front_end/src/fasta/kernel/kernel_target.dart:574)
<asynchronous suspension>
#44     _buildInternal (package:front_end/src/kernel_generator_impl.dart:210)
<asynchronous suspension>
#45     withCrashReporting (package:front_end/src/fasta/crash.dart:133)
<asynchronous suspension>
#46     kernelForProgramInternal.<anonymous closure> (package:front_end/src/api_prototype/kernel_generator.dart:62)
<asynchronous suspension>
#47     CompilerContext.clear (package:front_end/src/fasta/compiler_context.dart:139)
<asynchronous suspension>
#48     kernelForProgramInternal (package:front_end/src/api_prototype/kernel_generator.dart:61)
<asynchronous suspension>
#49     kernelForProgram (package:front_end/src/api_prototype/kernel_generator.dart:50)
<asynchronous suspension>
#50     compileToKernel (package:vm/kernel_front_end.dart:460)
<asynchronous suspension>
#51     runCompiler (package:vm/kernel_front_end.dart:307)
<asynchronous suspension>
#52     main (file:///b/s/w/ir/x/w/sdk/pkg/vm/bin/gen_kernel.dart:34)
<asynchronous suspension>
Error: AOT compilation failed
Generating AOT kernel dill failed!
The command '/bin/sh -c dart compile exe bin/server.dart -o bin/server' returned a non-zero code: 64
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 64

The output for dart --version from the Dockerfile is:
Dart SDK version: 3.2.0 (stable) (Tue Nov 14 18:26:59 2023 +0000) on "linux_x64"

The output for dart info thrown an error when running it within Dockerfile, here are the logs for it:

#12 [build  8/12] RUN dart info
#12 0.147 
#12 0.147 If providing this information as part of reporting a bug, please review the information
#12 0.147 below to ensure it only contains things you're comfortable posting publicly.
#12 0.147 
#12 0.147 #### General info
#12 0.147 
#12 0.147 - Dart 3.2.3 (stable) (Tue Dec 5 17:58:33 2023 +0000) on "linux_arm64"
#12 0.147 - on linux / Linux 6.4.16-linuxkit #1 SMP PREEMPT Tue Oct 10 20:38:06 UTC 2023
#12 0.147 - locale is en_US
#12 0.167 
#12 0.167 #### Project info
#12 0.167 
#12 0.167 - sdk constraint: '^3.0.6'
#12 0.167 - dependencies: collection, freezed_annotation, intl, json_annotation, path, protobuf
#12 0.167 - dev_dependencies: build_runner, freezed, grpc, json_serializable, very_good_analysis
#12 0.177 ProcessException: No such file or directory
#12 0.177   Command: ps -eo rss,pcpu,etime,args
#12 0.178 #0      _ProcessImpl._runAndWait (dart:io-patch/process_patch.dart:488:7)
#12 0.178 #1      _runNonInteractiveProcessSync (dart:io-patch/process_patch.dart:633:18)
#12 0.178 #2      Process.runSync (dart:io-patch/process_patch.dart:68:12)
#12 0.178 #3      _getProcessInfoLinux (package:dartdev/src/processes.dart:251:24)
#12 0.178 #4      ProcessInfo.getProcessInfo (package:dartdev/src/processes.dart:169:21)
#12 0.178 #5      InfoCommand.run (package:dartdev/src/commands/info.dart:68:21)
#12 0.178 #6      CommandRunner.runCommand (package:args/command_runner.dart:212:27)
#12 0.178 #7      DartdevRunner.runCommand (package:dartdev/dartdev.dart:240:30)
#12 0.178 #8      CommandRunner.run.<anonymous closure> (package:args/command_runner.dart:122:25)
#12 0.178 #9      new Future.sync (dart:async/future.dart:305:31)
#12 0.178 #10     CommandRunner.run (package:args/command_runner.dart:122:14)
#12 0.178 #11     runDartdev (package:dartdev/dartdev.dart:59:29)
#12 0.178 #12     main (file:///b/s/w/ir/x/w/sdk/pkg/dartdev/bin/dartdev.dart:11:9)
#12 0.178 #13     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:293:33)
#12 0.178 #14     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
#12 0.178 
#12 ERROR: process "/bin/sh -c dart info" did not complete successfully: exit code: 1
------
 > [build  8/12] RUN dart info:
0.178 #6      CommandRunner.runCommand (package:args/command_runner.dart:212:27)
0.178 #7      DartdevRunner.runCommand (package:dartdev/dartdev.dart:240:30)
0.178 #8      CommandRunner.run.<anonymous closure> (package:args/command_runner.dart:122:25)
0.178 #9      new Future.sync (dart:async/future.dart:305:31)
0.178 #10     CommandRunner.run (package:args/command_runner.dart:122:14)
0.178 #11     runDartdev (package:dartdev/dartdev.dart:59:29)
0.178 #12     main (file:///b/s/w/ir/x/w/sdk/pkg/dartdev/bin/dartdev.dart:11:9)
0.178 #13     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:293:33)
0.178 #14     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

Here is my Dockerfile:

# Dockerfile for building the Back-end, this file should be in the `backend/cloud_run` folder
# but beucase of Docker's context limitation we had to put it outside
FROM dart:stable AS build

WORKDIR /app

# Copy app source code and AOT compile it.
COPY backend/cloud_run backend/cloud_run
# Copy the core package
COPY /core core

WORKDIR core
RUN dart pub get

WORKDIR ../backend/cloud_run

RUN dart pub get
RUN dart --version
# RUN dart info  This LINE CRASHES IF UNCOMMENTED
RUN dart compile exe bin/server.dart -o bin/server

# Build minimal serving image from AOT-compiled `/server` and required system
# libraries and configuration files stored in `/runtime/` from the build stage.
FROM scratch
COPY --from=build /runtime/ /
COPY --from=build /app/backend/cloud_run/bin/server /app/bin/

# Start server.
EXPOSE 8080
CMD ["/app/bin/server"]

For more context, here is my project structure:

project/
├─ core /
│  ├─ pubspec.yaml
├─ backend/
├─ Dockerfile
  • The core folder is a package module that contains shared data models used both by the Flutter app and the back-end dart code
  • The dart backend code is in the backend/cloud_run/ where the pubspec.yaml file is also located, in the pubspec, we reference the core module this way:
  core:
    path: ../../core
@lrhn lrhn added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) area-front-end Use area-front-end for front end / CFE / kernel format related issues. labels Dec 7, 2023
@johnniwinther johnniwinther self-assigned this Dec 8, 2023
@cosminbodnariuc
Copy link
Author

Managed to found what was the issue, there was an import for which its content was not copied over in the docker image. I will close this as it's fixed.

@parlough
Copy link
Member

parlough commented Dec 8, 2023

Glad you were able to get everything working @cosminbodnariuc!

However, it'd be great if the CFE(compiler frontend) didn't crash in this scenario, but rather provided information to help you debug the situation.

Could you provide more details on the issue and what your fix was?

@parlough parlough reopened this Dec 8, 2023
@cosminbodnariuc
Copy link
Author

cosminbodnariuc commented Dec 9, 2023

@parlough The problem was that some files (.dart files) within the core module were not copied into the docker image's file system.
So we have a folder named generated within the core module (Path: core/lib/generated/**) with the generated dart files based on the proto definition, like these:

name_service.pb.dart
name_service.pbenum.dart
name_service.pbgrpc.dart
name_service.pbjson.dart

The issue was caused when the core library was importing the generated files like this:

import 'package:core/generated/protos.dart' as p;

but the files were not present in the folder.
After making sure that all the imports were valid and the paths were there, the compilation worked fine.

@Oxore
Copy link

Oxore commented Dec 15, 2023

I stumbled across this issue when I was playing with enhanced enums and using switch-case on them. Here are a couple of somewhat MRE for this issue (although completely made up and nonsense code):

  1. A comma (,) used instead of semicolon (;) after the last enum value:
enum A {
  a(0),
  b(1),
  final int value;
  const A(this.value);
}

int fn(A a) => switch (a) {
  A.a => 0,
  A.b => 1,
};
  1. Enum's instance variable name does not match the generative constructor's argument name:
enum A {
  a(0),
  b(1);
  final int value2;
  const A(this.value);
}

int fn(A a) => switch (a) {
  A.a => 0,
  A.b => 1,
};

With these examples I get the exact same stack trace as in the original post.

My SDK version:

Dart SDK version: 3.2.3 (stable) (Tue Dec 5 17:58:33 2023 +0000) on "linux_x64"

Hope it helps for those who need a sensible compiler error, but got the compiler crashing instead. Or it may help compiler developers to fix the issue.

@srawlins
Copy link
Member

srawlins commented Jan 3, 2024

I am currently hitting what looks like the same error, with different line numbers, while compiling an Analyzer plugin:

1704305394332:PluginErr:RequestErrorCode.PLUGIN_ERROR:IsolateSpawnException:: Unable to spawn isolate:: Crash when compiling::
Null check operator used on a null value

#0      CfeEnumOperations.getEnumElementValue (package::front_end/src/fasta/kernel/exhaustiveness.dart::395::70)
#1      EnumInfo._createEnumElements (package::_fe_analyzer_shared/src/exhaustiveness/types/enum.dart::60::49)
#2      EnumInfo.enumElements (package::_fe_analyzer_shared/src/exhaustiveness/types/enum.dart::47::45)
#3      EnumStaticType._createEnumElements (package::_fe_analyzer_shared/src/exhaustiveness/types/enum.dart::97::22)
#4      EnumStaticType.enumElements (package::_fe_analyzer_shared/src/exhaustiveness/types/enum.dart::92::58)
#5      EnumStaticType.getSubtypes (package::_fe_analyzer_shared/src/exhaustiveness/types/enum.dart::90::64)
#6      _Checker._unmatched (package::_fe_analyzer_shared/src/exhaustiveness/exhaustive.dart::131::31)
#7      reportErrors (package::_fe_analyzer_shared/src/exhaustiveness/exhaustive.dart::43::30)
#8      ConstantsTransformer._checkExhaustiveness (package::front_end/src/fasta/kernel/constant_evaluator.dart::1569::9)
#9      ConstantsTransformer.visitPatternSwitchStatement (package::front_end/src/fasta/kernel/constant_evaluator.dart::1534::5)
#10     PatternSwitchStatement.accept1 (package::kernel/src/ast/patterns.dart::1862::14)
#11     RemovingTransformer.transform (package::kernel/visitor.dart::1883::17)
#12     LabeledStatement.transformOrRemoveChildren (package::kernel/ast.dart::9422::14)
#13     RemovingTransformer.defaultTreeNode (package::kernel/visitor.dart::2166::10)
#14     TreeVisitor1Default.defaultStatement (package::kernel/visitor.dart::743::48)
#15     StatementVisitor1DefaultMixin.visitLabeledStatement (package::kernel/visitor.dart::2559::7)
#16     LabeledStatement.accept1 (package::kernel/ast.dart::9407::9)
#17     RemovingTransformer.transformOrRemove (package::kernel/visitor.dart::1919::21)
#18     ConstantsTransformer.visitBlock (package::front_end/src/fasta/kernel/constant_evaluator.dart::827::27)
#19     Block.accept1 (package::kernel/ast.dart::9198::58)
#20     RemovingTransformer.transform (package::kernel/visitor.dart::1883::17)
#21     ConstantsTransformer.visitFunctionNode (package::front_end/src/fasta/kernel/constant_evaluator.dart::650::19)
#22     FunctionNode.accept1 (package::kernel/ast.dart::3890::9)
#23     RemovingTransformer.transform (package::kernel/visitor.dart::1883::17)
#24     ConstantsTransformer.visitProcedure.<anonymous closure> (package::front_end/src/fasta/kernel/constant_evaluator.dart::561::23)
#25     ConstantEvaluator.withNewEnvironment (package::front_end/src/fasta/kernel/constant_evaluator.dart::5484::18)
#26     ConstantsTransformer.visitProcedure (package::front_end/src/fasta/kernel/constant_evaluator.dart::559::23)
#27     Procedure.accept1 (package::kernel/ast.dart::3246::55)
#28     RemovingTransformer.transformOrRemove (package::kernel/visitor.dart::1919::21)
#29     RemovingTransformer.transformList (package::kernel/visitor.dart::2143::19)
#30     RemovingTransformer.transformProcedureList (package::kernel/visitor.dart::2035::5)
#31     ConstantsTransformer.visitClass.<anonymous closure> (package::front_end/src/fasta/kernel/constant_evaluator.dart::514::7)
#32     ConstantEvaluator.withNewEnvironment (package::front_end/src/fasta/kernel/constant_evaluator.dart::5484::18)
#33     ConstantsTransformer.visitClass (package::front_end/src/fasta/kernel/constant_evaluator.dart::509::23)
#34     Class.accept1 (package::kernel/ast.dart::1464::53)
#35     RemovingTransformer.transformOrRemove (package::kernel/visitor.dart::1919::21)

I am going to trry to debug this on my end a bit. Because that stack trace is probably not helpful without a repro :D

@srawlins
Copy link
Member

srawlins commented Jan 5, 2024

Oh, I missed that this comment has a perfect minimal repro.

@johnniwinther any chance we can prioritize this, as it seems to be blocking some of my work? What's weird is that I don't think there is any erroneous code in my case, I think all of my enum definitions are sound. So I can also keep looking at why my code is triggering this, and maybe produce a minimal repro with valid Dart code.

@parlough parlough added the cfe-crashes Crashes in the CFE label Jan 5, 2024
@srawlins
Copy link
Member

srawlins commented Jan 8, 2024

Thanks much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. cfe-crashes Crashes in the CFE type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

6 participants