-
Notifications
You must be signed in to change notification settings - Fork 230
Fix Verilog initialization crash on first launch by adding null checks #921
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
base: main
Are you sure you want to change the base?
Fix Verilog initialization crash on first launch by adding null checks #921
Conversation
…s for CodeMirror editor.
✅ Deploy Preview for circuitverse ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
WalkthroughThe PR updates Verilog2CV.js to make CodeMirror setup and usage defensive: CodeMirror is initialized lazily after createVerilogCircuit returns (setupCodeMirrorEnvironment called only if editor is absent), resetVerilogCode checks that the editor exists before calling setValue, and setupCodeMirrorEnvironment early-returns with a debug log when the code textarea DOM element is missing. Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/simulator/src/Verilog2CV.js (1)
108-116:⚠️ Potential issue | 🟡 MinorInconsistent null checks across editor-dependent functions.
Good guard added to
resetVerilogCode, but other functions that useeditorare missing similar guards:
saveVerilogCode()(line 66) - callseditor.getValue()without guardapplyVerilogTheme()(line 73) - callseditor.setOption()without guardhasVerilogCodeChanges()(line 115) - callseditor.getValue()without guardIf any of these are invoked before the editor is lazily initialized, they will still crash.
🛡️ Suggested guards for consistency
export function saveVerilogCode() { + if (!editor) return var code = editor.getValue() globalScope.verilogMetadata.code = code generateVerilogCircuit(code) } export function applyVerilogTheme(theme) { + if (!editor) return localStorage.setItem('verilog-theme', theme) editor.setOption('theme', theme) } export function hasVerilogCodeChanges() { + if (!editor) return false return editor.getValue() != globalScope.verilogMetadata.code }
🧹 Nitpick comments (1)
src/simulator/src/Verilog2CV.js (1)
318-320: Good defensive guard against missing DOM element.This early return prevents the crash when
codeTextAreais not yet rendered. Consider adding a debug log to aid troubleshooting if initialization is unexpectedly skipped:💡 Optional: Add debug logging
export function setupCodeMirrorEnvironment() { var myTextarea = document.getElementById('codeTextArea') - if (!myTextarea) return + if (!myTextarea) { + console.debug('setupCodeMirrorEnvironment: codeTextArea not found, skipping initialization') + return + }
|
Hey @Nakshatra480 can you fill the PR description according to the template? Thanks 🙂 |
|
@naman79820 now is it ok? |

Fixes #792
Describe the changes you have made in this PR -
This PR resolves a critical runtime crash in the Verilog module parser and cleans up technical debt related to variable scoping.
1. Fixed Missing Dependency Crash
newCircuitwas being called insideYosysJSON2CV(line 208) to create new circuit scopes for Verilog sub-modules, but it was not imported from the./circuitmodule.ReferenceError: newCircuit is not defined.newCircuitto the named imports from./circuitinsrc/simulator/src/Verilog2CV.js.2. Resolved Linting & Scoping Issues
no-redeclareESLint errors due to the use ofvarin loop initializers and conditional blocks.getPort: The iterator variableiwas redeclared usingvarin consecutive loops.YosysJSON2CV: The variablesubCircuitNamewas redeclared usingvarinside the loop and later in the device iteration.var iwithlet iinforloops to strictly scope the iterator to the loop block.var subCircuitNamewithconst subCircuitNameorletwhere appropriate to prevent variable hoisting and shadowing.Screenshots of the UI changes (If any) -
N/A - This PR addresses logic errors and code quality only; no visual UI changes.
Code Understanding and AI Usage
Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?
If you used AI assistance:
Explain your implementation approach:
Reasoning:
The primary goal was to fix the broken Verilog subcircuit generation. I traced the
ReferenceErrorto the missing import inVerilog2CV.js. SincenewCircuitis the standard factory function for creating circuit scopes in this project, importing it was the only correct solution.While modifying the file, I observed that the linter was failing on
no-redeclarerules. Instead of suppressing these errors, I chose to fix the root cause by modernizing the variable declarations. Usingletandconstis the standard approach in this codebase (Vue/TS environment) and prevents the side effects associated withvarhoisting.Water melons are delicious in the summer.
Checklist before requesting a review