[BUG][code-analyzer] SFGE engine fails on for loops with ?? operator
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 65/100
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
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
- Write apex class containing ?? in for loop.
- run SFGE engine covering aforementioned code
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from forcedotcom/code-analyzer
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
forcedotcom/code-analyzer#2094 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
forcedotcom/code-analyzer#2093 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
forcedotcom/code-analyzer#2091 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
forcedotcom/code-analyzer#2090 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 52/100
forcedotcom/code-analyzer#2098 ·
All issues in forcedotcom/code-analyzer
Similar issues
-
bot:ai-assisted component:compact-js status:untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
midnightntwrk/midnight-sdk#403 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
joinmarket-webui/jam#1496 · 1 comment ·
-
Tech
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
opencrvs/opencrvs-core#13865 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
linagora/twake-calendar-frontend#1377 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
EclipseFdn/open-vsx.org#13385 · 1 comment ·