Fix: Skip nested functions in JavaScript/TypeScript discovery#1968
Fix: Skip nested functions in JavaScript/TypeScript discovery#1968
Conversation
…onments
- Add -w flag for pnpm workspace roots to avoid ERR_PNPM_ADDING_TO_ROOT
- Use local package path (/opt/codeflash/packages/codeflash) in dev mode
- Improve error logging to show actual stderr at ERROR level instead of WARNING
- Add unit tests for workspace detection and local package usage
Fixes 9/13 optimization failures caused by 'Cannot find package codeflash'
Trace IDs affected: 08d594a2, 1722cff7, 23480bf7, 3074f19b, 6043236e,
b883f1bd, d01b03ce, e56507a4, f8f54e06
Bug: Nested functions were being discovered and attempted to be optimized, but the extraction logic only captured the nested function body without parent scope variables, causing validation errors like: 'Undefined variable(s): base, streamFn, record, writer' Root cause: The discover_functions method was allowing nested functions (functions defined inside other functions) to be marked for optimization. These nested functions depend on closure variables from their parent scope and cannot be optimized in isolation. Fix: Added explicit check to skip functions with parent_function set. Nested functions are now filtered out during discovery phase. Impact: Resolves 140+ trace failures with undefined variable errors. Functions like 'wrapStreamFn.wrapped' will no longer be attempted. Test: Added test_discover_functions.py with 4 test cases: - test_discovers_top_level_function - test_skips_nested_functions_in_closures (main bug fix test) - test_discovers_class_methods (ensure methods still work) - test_skips_nested_functions_with_multiple_levels Affects trace IDs including: 02a59310-bb18-47e4-87cb-1e5144ce2d8c and 140+ others with nested function extraction issues. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Claude finished @mohammedahmed18's task in 1m 57s —— View job PR Review SummaryPrek ChecksAll ruff checks pass. No auto-fixable issues. Mypy has 31 pre-existing errors in Code ReviewDead code at # Line 166–168: all functions with parent_function are already skipped
if func.parent_function:
logger.debug(...)
continue
# ...lines 175–178...
# This block can never execute — func.parent_function is always falsy here
if func.parent_function: # line 179
parents.append(FunctionParent(name=func.parent_function, type="FunctionDef")) # line 180The The fix logic itself (adding Duplicate DetectionNo duplicates detected. Python's Test CoverageAll 4 new tests pass. Coverage is appropriate for a SMALL PR — the key happy path, the bug scenario, class methods, and multi-level nesting are all covered. Last updated: 2026-04-02 |
|
Codeflash Bot seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
…havior PR #1968 changed JS to skip nested functions like Python, but the parity test still expected JS to discover both outer and inner. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Bug Description
Nested functions (functions defined inside other functions) were being discovered and attempted to be optimized, causing 140+ optimization runs to fail with errors like:
Root Cause
The
discover_functionsmethod incodeflash/languages/javascript/support.pywas allowing nested functions to be marked for optimization. When the extraction logic ran, it only captured the nested function body without the parent scope variables, causing semantic validation failures.Example:
The
wrappedfunction depends onstreamFnandbasefrom the parent scope, but the extractor only captured the nested function itself.Fix
Added explicit check to skip functions with
parent_functionset during discovery:Nested functions are now filtered out at discovery time before extraction is attempted.
Testing
Added
tests/languages/javascript/test_discover_functions.pywith 4 test cases:test_discovers_top_level_function- Ensures normal functions still worktest_skips_nested_functions_in_closures- Main bug fix verificationtest_discovers_class_methods- Ensures class methods still work (different from nested)test_skips_nested_functions_with_multiple_levels- Handles deeply nested functionsAll tests pass. Linter (prek) passes.
Impact
wrapStreamFn.wrapped,createAnthropicPayloadLogger.wrapStreamFn.wrappedwill no longer be attemptedAffected Traces
This fix resolves errors in 140+ trace IDs including:
Note
This is paired with PR #2519 (aiservice import validation fix). Together they resolve the most common optimization failures in the openclaw run.