SONARPY-2729 Fix crash when Jupyter notebook source arrays contain multiline strings#2294
SONARPY-2729 Fix crash when Jupyter notebook source arrays contain multiline strings#2294mallory-scotton wants to merge 2 commits into
Conversation
…urce arrays The ipynb JSON schema allows a "source" array entry to contain embedded newlines - basically several Python lines packed into one array item. The parser assumed every entry was exactly one line, so an entry like that knocked the generated source out of sync with its line-location map and blew up with "No IPythonLocation found for line N" once the enricher hit a line the map didn't know about. Pulled the per-line splitting logic already used for plain-string "source" values into a shared helper and reused it for array entries with embedded newlines, so each embedded line gets its own map entry again.
…a trailing newline Caught in review: a "source" array element with embedded newlines but no trailing newline (e.g. ["a\nb", "c"]) gets glued onto the next element on the same physical line, but each one still got its own locationMap entry. One entry too many for the actual line count, so every location after it quietly drifts instead of crashing. Turns out plain, non-multiline array elements without a trailing newline had this exact problem already - my fix just carried it into the new code path. Now track whether the previous array element ended with a newline, and only give an element its own locationMap entry when it's actually starting a fresh physical line. One that continues an unterminated line just gets appended as plain text.
Code Review ✅ Approved 1 resolved / 1 findingsRefactors the source array parser to correctly map multiline strings by sharing existing splitting logic, resolving the location drift and crash issues. Comprehensive unit and end-to-end tests verify the fix. ✅ 1 resolved✅ Edge Case: Multiline non-last array element without trailing newline misaligns location map
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
Code Review ✅ Approved 1 resolved / 1 findingsRefactors the source array parser to correctly map multiline strings by sharing existing splitting logic, resolving the location drift and crash issues. Comprehensive unit and end-to-end tests verify the fix. ✅ 1 resolved✅ Edge Case: Multiline non-last array element without trailing newline misaligns location map
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
SONARPY-2729
Analysis crashed with
IllegalStateException: No IPythonLocation found for line Non notebooks where "source" was an array and one of its entriescontained embedded newlines. That's valid per the ipynb schema, but
IpynbNotebookParser.parseSourceArraycounted every array entry as exactlyone line. An entry spanning several lines only added a single entry to the
line-location map, so the map and the generated source drifted apart -
and once the parser reached a line number the map didn't have, it threw.
Fixed by pulling the per-line splitting logic already used for plain-string
"source" values out into a shared helper, then reusing it for array entries
that contain embedded newlines. Single-line entries take the exact same
path as before.
I added
notebook_multiline_string_in_array.ipynbto reproduce the reportedpayload, a unit test in
IpynbNotebookParserTestchecking the location mapgets one entry per line, and an end-to-end test in
IpynbNotebookParserScannerTestthat runs the realIPythonTreeMaker/TokenEnricherpath that used to throw. Fullpython-frontendsuite andthe notebook tests in
python-commonsboth pass.Green build: https://github.com/SonarSource/sonar-python/actions/runs/29499453281/job/87624388023