[BUG][code-analyzer] SFGE engine fails on for loops with ?? operator

Open
#1,813 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
java
Domain
devtools

Research direction

Reproduce the failure with the Apex for-loop example, then start at MethodPathBuilderVisitor.java, especially visitForEachStatement, and follow the GremlinUtil.getChildren call shown in the stack trace. Confirm that SFGE loads the source and builds its graph without throwing UnexpectedException when the ?? operator appears in the loop.

Written by the indexing model from the issue text.

Description

SFGE
Have you tried to resolve this issue yourself first?
  • I confirm I have gone through the above steps and still have an issue to report
Bug Description

When running SFGE engine, we see that when ?? is used in a for loop, there is an error
Example snippet

for (Account a : aList ?? new List<Account>()) { system.debug('do something'); }

I assume this is a bug/issue with underlying AST parser or similar as the ?? operator is relatively new in Apex

Output / Logs

2025-05-20 00:23:01 ERROR Sfge:159 - Unexpected exception while loading graph
com.salesforce.exception.UnexpectedException: vp[FirstChild->true], vp[BeginLine->29], vp[DefiningType_CaseSafe->EXAMPLECLASS...], vp[LastChild->true], vp[DefiningType->EXAMPLECLASS...], vp[EndLine->29], vp[childIdx->0], vp[BeginColumn->4]
at com.salesforce.graph.build.GremlinUtil.getChildren(GremlinUtil.java:60) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.MethodPathBuilderVisitor.visitForEachStatement(MethodPathBuilderVisitor.java:235) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.MethodPathBuilderVisitor._visit(MethodPathBuilderVisitor.java:130) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.MethodPathBuilderVisitor._visit(MethodPathBuilderVisitor.java:159) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.MethodPathBuilderVisitor._visit(MethodPathBuilderVisitor.java:159) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.MethodPathBuilderVisitor._visit(MethodPathBuilderVisitor.java:159) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.MethodPathBuilderVisitor.apply(MethodPathBuilderVisitor.java:97) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.AbstractApexVertexBuilder.afterInsert(AbstractApexVertexBuilder.java:169) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.AbstractApexVertexBuilder.buildVertices(AbstractApexVertexBuilder.java:153) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.AbstractApexVertexBuilder.buildVertices(AbstractApexVertexBuilder.java:125) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.AbstractApexVertexBuilder.buildVertices(AbstractApexVertexBuilder.java:35) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.CustomerApexVertexBuilder.build(CustomerApexVertexBuilder.java:21) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.build.Util.buildGraph(Util.java:39) ~[sfge-1.0.0.jar:?]
at com.salesforce.graph.ops.GraphUtil.loadSourceFilesAndFolders(GraphUtil.java:170) ~[sfge-1.0.0.jar:?]
at com.salesforce.Sfge$Dependencies.loadSourceFilesAndFoldersToGraph(Sfge.java:249) ~[sfge-1.0.0.jar:?]
at com.salesforce.Sfge.execute(Sfge.java:153) [sfge-1.0.0.jar:?]
at com.salesforce.Sfge.process(Sfge.java:103) [sfge-1.0.0.jar:?]
at com.salesforce.Sfge.main(Sfge.java:71) [sfge-1.0.0.jar:?]

Steps To Reproduce
  1. Write apex class containing ?? in for loop.
  2. run SFGE engine covering aforementioned code
  3. refer to logs
Expected Behavior

?? should be accepted as is valid json.

Operating System

Windows 11

Salesforce CLI Version

@salesforce/cli/2.88.6 win32-x64 node-v22.14.0

Code Analyzer Plugin (code-analyzer) Version

code-analyzer 5.0.0 (>=5.0.0 <6.0.0-0)

Node Version

v22.14.0

Java Version

openjdk version "17.0.11" 2024-04-16 LTS OpenJDK Runtime Environment Zulu17.50+19-CA (build 17.0.11+9-LTS) OpenJDK 64-Bit Server VM Zulu17.50+19-CA (build 17.0.11+9-LTS, mixed mode, sharing)

Python Version

No response

Additional Context (Screenshots, Files, etc)

No response

Workaround

bring ?? outside of for loop, assign variable, then use in for loop.

Urgency

Low

Dominant language
TypeScript
Stars
240
Forks
52
Avg merge
1d 23h
Merged PRs (30d)
5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from forcedotcom/code-analyzer

All issues in forcedotcom/code-analyzer

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.