ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.68k stars 753 forks source link

[Bug]: Compiler throws NPE when trying to mock the generated client with generated mock client #41788

Open daneshk opened 11 months ago

daneshk commented 11 months ago

Description

$subject, please.

When trying to mock the generated persist client(pointing to MySQL DB) using the generated mock client(pointing to H2 DB) in the test file like below,

Note: Here both client codes are inside the generated directory, under a submodule called db

@test:Mock {functionName: "initializeClient"}
isolated function getMockClient() returns db:Client|error {
    return test:mock(db:Client, check new db:MockClient("jdbc:h2:./test", "sa", "", options = {}));         
}

Get the following error,

Compiling source
        danesh/mock_with_h2:0.1.0
ballerina: Oh no, something really went wrong. Bad. Sad.

We appreciate it if you can report the code that broke Ballerina in
https://github.com/ballerina-platform/ballerina-lang/issues with the
log you get below and your sample code.

We thank you for helping make us better.

[2023-11-29 10:44:09,843] SEVERE {b7a.log.crash} - Cannot read field "tag" because "referredType" is null 
java.lang.NullPointerException: Cannot read field "tag" because "referredType" is null
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:2232)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.accept(BLangRecordLiteral.java:81)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeExpr(CodeAnalyzer.java:3514)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:3190)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.expressions.BLangNamedArgsExpression.accept(BLangNamedArgsExpression.java:75)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeExpr(CodeAnalyzer.java:3514)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeExprs(CodeAnalyzer.java:3585)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:2706)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit.accept(BLangTypeInit.java:70)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeExpr(CodeAnalyzer.java:3514)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:3196)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.expressions.BLangCheckedExpr.accept(BLangCheckedExpr.java:70)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeExpr(CodeAnalyzer.java:3514)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeExprs(CodeAnalyzer.java:3585)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:2505)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation.accept(BLangInvocation.java:133)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeExpr(CodeAnalyzer.java:3514)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:732)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.statements.BLangReturn.accept(BLangReturn.java:53)
        at org.wso2.ballerinalang.compiler.tree.SimpleBLangNodeAnalyzer.visitNode(SimpleBLangNodeAnalyzer.java:215)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeNode(CodeAnalyzer.java:349)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:538)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.BLangBlockFunctionBody.accept(BLangBlockFunctionBody.java:65)
        at org.wso2.ballerinalang.compiler.tree.SimpleBLangNodeAnalyzer.visitNode(SimpleBLangNodeAnalyzer.java:215)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeNode(CodeAnalyzer.java:349)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visitFunction(CodeAnalyzer.java:513)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:456)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.BLangFunction.accept(BLangFunction.java:81)
        at org.wso2.ballerinalang.compiler.tree.SimpleBLangNodeAnalyzer.visitNode(SimpleBLangNodeAnalyzer.java:215)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeNode(CodeAnalyzer.java:349)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeTopLevelNodes(CodeAnalyzer.java:338)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:326)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:332)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.BLangTestablePackage.accept(BLangTestablePackage.java:42)
        at org.wso2.ballerinalang.compiler.tree.SimpleBLangNodeAnalyzer.visitNode(SimpleBLangNodeAnalyzer.java:215)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.lambda$visit$0(CodeAnalyzer.java:327)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:327)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.BLangPackage.accept(BLangPackage.java:172)
        at org.wso2.ballerinalang.compiler.tree.SimpleBLangNodeAnalyzer.visitNode(SimpleBLangNodeAnalyzer.java:215)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyze(CodeAnalyzer.java:314)
        at io.ballerina.projects.internal.CompilerPhaseRunner.codeAnalyze(CompilerPhaseRunner.java:196)
        at io.ballerina.projects.internal.CompilerPhaseRunner.performTypeCheckPhases(CompilerPhaseRunner.java:119)
        at io.ballerina.projects.ModuleContext.compileInternal(ModuleContext.java:435)
        at io.ballerina.projects.ModuleCompilationState$1.compile(ModuleCompilationState.java:45)
        at io.ballerina.projects.ModuleContext.compile(ModuleContext.java:383)
        at io.ballerina.projects.PackageCompilation.compileModulesInternal(PackageCompilation.java:208)
        at io.ballerina.projects.PackageCompilation.compileModules(PackageCompilation.java:192)
        at io.ballerina.projects.PackageCompilation.compile(PackageCompilation.java:99)
        at io.ballerina.projects.PackageCompilation.from(PackageCompilation.java:94)
        at io.ballerina.projects.PackageContext.getPackageCompilation(PackageContext.java:242)
        at io.ballerina.projects.Package.getCompilation(Package.java:150)
        at io.ballerina.projects.Package.runCodeGeneratorPlugins(Package.java:319)
        at io.ballerina.cli.task.CompileTask.execute(CompileTask.java:142)
        at io.ballerina.cli.TaskExecutor.executeTasks(TaskExecutor.java:40)
        at io.ballerina.cli.cmd.BuildCommand.execute(BuildCommand.java:288)
        at java.base/java.util.Optional.ifPresent(Optional.java:178)
        at io.ballerina.cli.launcher.Main.main(Main.java:58)

ERROR [mock_with_h2:(1:1,1:1)] Compilation failed due to: Cannot read field "tag" because "referredType" is null
error: compilation contains errors

Steps to Reproduce

Check out and build the mock_with_h2 project in github repo

branch: move_generated_dir

-> Compilation

Related issue(s)

Cannot read field "tag" because "referredType" is null

MaryamZi commented 11 months ago

MockClient is defined in the tests folder of db, right? We can't refer to test constructs outside the tests.

As with https://github.com/ballerina-platform/ballerina-lang/issues/41474, this should result in a compilation error for db:MockClient.

daneshk commented 11 months ago

MockClient is defined in the tests folder of db, right? We can't refer to test constructs outside the tests.

As with #41474, this should result in a compilation error for db:MockClient.

Yes, the generated folder structure looks like below,

Screenshot 2023-11-29 at 16 34 22
SasinduDilshara commented 10 months ago

This is happen with the check expression. Inside the check expression we add a silentCheck to check the type of the expression followed by check. A bug in that scenario lead to this null point exception