Skip to content

perl func_start block-slicing: _line (mojo/Template.pm) swallows 432 extra lines through the CLI pipeline but not via direct _function_slice #1517

Closed

Description

@squid-protocol

Summary

Found while verifying #1517/#1518's perl args-counting fix (PR pending): mojo/Template.pm::_line is a real, tiny 4-line sub (lines 236-239):

sub _line {
  my $name = shift->name;
  $name =~ y/"//d;
  return qq{#line @{[shift]} "$name"};
}

But the committed golden master (tests/golden_master_audit.json, confirmed present on main BEFORE any of #1517/#1518's changes -- this is not a regression from that work) records _line spanning lines 236-672 (437 LOC, all the way to the end of the file, past _trim/_wrap/_compile/several other real subs, into the POD documentation section). This is a func_start/block-slicing bug, not an args-counting bug -- #1517/#1518 just made its damage more visible: Input Parameters for this phantom-huge block went from a plausible-looking 1 to a clearly-wrong 13 once the args counter started summing every shift/my (...) = @_ occurrence across the whole (wrongly huge) block, instead of only finding the first one.

Confirmed repro

Fresh, uncached galaxyscope scan of just this one file reproduces it:

{'func_name': '_line', 'args': 13, 'loc': 437}

But calling StructuralExtractor("perl", ...)._function_slice() directly on the same raw file content gives the CORRECT span:

_line 236 239 4   # correct

So the bug is NOT in _function_slice itself (confirmed via direct unit-level call) -- it must be introduced somewhere else in the full pipeline between raw file content and _function_slice's input (most likely prism.py's comment/string-shielding stage, since _line's body contains qq{#line @{[shift]} "$name"} -- a qq{...} custom-delimited quote-like operator whose delimiter ({/}) collides with an interpolated @{[shift]} "baby cart" expression also containing a {/} pair. This is exactly the kind of delimiter-collision shape that's broken prism.py's shielding before). Not confirmed as the root cause, just the most likely candidate based on the code shape -- whoever picks this up should verify by diffing prism.py's shielded output for this file against the raw content around line 236-240.

I did NOT find a caching explanation (checked for a stale incremental-scan cache reusing old state -- the repro above used a fresh output directory and a file copied fresh into an empty scratch directory, so it's not state_rehydrator.py serving stale data).

Scope

Not fixed here -- filed as a follow-up while working on a different (args-counting) fix, per repo convention. This affects func_start block-boundary accuracy for at least this one real-world shape; unknown how many other perl files in the corpus have the same swallowed-block issue (only noticed because _line's tiny real size made the wrong 437-LOC span glaring in the crucible diff -- a function whose real vs. wrong size were closer wouldn't have been as obviously wrong).

Activity

  1. added
    bug
    testing
    core-engine
    threat: false-positive
    on Aug 14, 2026
  2. squid-protocol commented on Aug 14, 2026

    @squid-protocol
    OwnerAuthor

    Root cause now fully confirmed (this session's investigation), and it's different from the original hypothesis -- not a brace-delimiter collision inside qq{...}'s interpolation, but a much more general bug in prism.py's _strip_single_line_comments: perl's #-to-end-of-line comment stripper has no awareness of perl's quote-like operators (qq{...}, q{...}, etc.) OR of /regex/ literals (bare or keyword-prefixed: m/.../, s/.../.../ , etc.) -- any # character that appears as literal CONTENT inside either of those, on the same line, gets misread as a real comment start, and everything from that # to end-of-line is stripped out of code_stream before detector.py ever sees it.

    Confirmed via THREE separate real corpus functions (not just _line), all in language-crucible/data/perl (sibling checkout):

    1. mojo/Template.pm::_line (the original case): return qq{#line @{[shift]} "$name"}; -- the #line right after qq{ is real quote-op content (a perl #line directive template), but prism.py's stripper doesn't know it's inside qq{...} and treats the # as a comment start, deleting #line @{[shift]} "$name"}; and desyncing everything downstream through end of file.
    2. exiftool/exiftool::FilterArgfileLine (line 4893, real 23-line function): if ($arg =~ /^#/) { # comment lines begin with '#' -- the bare /^#/ regex literal (checking "does the string start with #") has its OWN # misread as a comment start, again with no quote-op keyword prefix at all (just =~ /.../), stripping the rest of that line and desyncing the function's body -- confirmed measured as swallowing 1048 lines (loc 1048 instead of ~23), which also swallows the real definition of ReadStayOpen (line 4922) entirely out of GitGalaxy's output.
    3. spamassassin/Makefile.PL::MY::dist (line 972): next if $line =~ /^\s*#/; -- same mechanism, same bare-regex-with-embedded-# shape.

    Reproduction: load any of the above files, call Prism(LEXICAL_FAMILY_HEURISTICS, LANGUAGE_DEFINITIONS).split_streams(content, 'perl')['code_stream'], and diff against the raw source around the cited line -- the text after the embedded # is missing/blank in code_stream, confirming prism.py (not detector.py) is where the text is actually lost.

    Verified NOT the same bug as #1606 (POD-apostrophe, already fixed) or #1607 (legacy-prototype args, already fixed) -- both of those are detector.py-level (_build_brace_safe_stream) and already shipped. This one is upstream in prism.py, before detector.py ever runs.

    Two sub-cases of the SAME general shape were separately fixed this session, in detector.py's _build_brace_safe_stream (NOT prism.py, so they don't fix _line/FilterArgfileLine above, which are prism.py-level):

    • An escaped \{/\} inside an unshielded /regex/ literal desyncing the brace-depth counter (exiftool/exiftool::SuggestedExtension, was swallowing 1206 lines instead of 57) -- fixed by blanking \{/\} globally before brace-depth counting.
    • A slash-delimited quote-like operator (y/"//d) containing a bare "/' in its own content false-pairing with an unrelated later quote -- fixed by recognizing keyword-prefixed /.../ quote-ops (m/, s/, y/, tr/, etc.) and shielding them before the generic quote-shield runs.

    What's needed to close #1517 for real (not attempted this session -- prism.py's _strip_single_line_comments is shared across the whole "line_exclusive" language family, ~20 languages, with a documented history of subtle regressions -- see that method's own docstring citing #1184/#1193/#1271 -- so this needs its own careful, well-tested PR, not a rushed addition): teach _strip_single_line_comments, gated strictly to lang_id == "perl", to mask two shapes per-line (bounded, no cross-line state, same conservative philosophy the method already uses) BEFORE running its #-delimiter search: (a) qq{...}/q{...}/etc. brace-delimited quote-ops (can likely reuse/adapt the bounded balanced-brace approach detector.py's _build_brace_safe_stream already has for this), and (b) /regex/ literals -- both keyword-prefixed (m/, s/, etc., can likely adapt the slash-terminator scan detector.py already added this session) AND bare ($x =~ /.../ , if ($x =~ /.../ ), no keyword prefix, which is the more common real-world form and wasn't attempted anywhere this session since disambiguating a bare / as regex-open vs. division is the genuinely hard, ambiguous part -- likely needs a preceding-token heuristic: / immediately preceded by =~, !~, (, ,, ;, {, &&, ||, or a keyword like if/unless/while/split/grep/map/return is very likely a regex open; preceded by an identifier/number/)/] is very likely division).

    Once fixed, this is very likely to close most/all of perl's remaining func_recall gap -- all 3 confirmed cases above are currently the ONLY "missing function" entries left in the tree-sitter-accuracy baseline for perl (down from ~126 before this session's #1606/#1607/#1608 fixes).

  3. added this to the v2.4.8 milestone on Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-enginetestingthreat: false-positive

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions