Skip to content

path normalization and tempdir fixes for windows #363

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

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

KRRT7
Copy link
Contributor

@KRRT7 KRRT7 commented Jun 22, 2025

User description

for the full test suite to work on Windows, the following are needed:

  1. Line profiler needs some work
  2. test instrumentation isn't applied properly
  3. pickle patching has some sort of race condition when cleaning up on the running the tests

PR Type

Tests, Enhancement


Description

  • Add temp_dir pytest fixture across tests

  • Replace NamedTemporaryFile with TemporaryDirectory usage

  • Remove outdated hashing_context assertions in tests

  • Standardize path formatting via Path.as_posix()


Changes walkthrough 📝

Relevant files
Tests
11 files
test_code_context_extractor.py
Add temp_dir fixture, remove hashing assertions                   
+30/-668
test_function_discovery.py
Refactor to use pytest temp_dir fixture                                   
+341/-350
test_instrument_tests.py
Refactor instrumentation tests temp file creation               
+52/-47 
test_codeflash_capture.py
Standardize path formatting with as_posix()                           
+17/-13 
test_formatter.py
Use pytest temp_dir and Path in formatter tests                   
+45/-55 
test_get_code.py
Use pytest temp_dir fixture in get_code tests                       
+22/-15 
test_instrument_codeflash_capture.py
Use as_posix() for tmp_dir_path and tests_root                     
+9/-9     
test_trace_benchmarks.py
Use as_posix() in trace benchmark path                                     
+1/-1     
test_test_runner.py
Use TemporaryDirectory for test runner tests                         
+26/-20 
test_shell_utils.py
Add platform-specific SHELL export syntax                               
+16/-4   
test_instrument_all_and_run.py
Refactor instrument_all_and_run temp file handling             
+8/-8     
Enhancement
2 files
replay_test.py
Convert file_path to POSIX string in replay tests               
+4/-4     
instrument_codeflash_capture.py
Use POSIX paths in instrumentation capture code                   
+3/-3     
Additional files
8 files
code_utils.py +2/-1     
coverage_utils.py +1/-1     
instrument_existing_tests.py +3/-1     
models.py +1/-1     
create_pr.py +2/-2     
test_code_utils.py +1/-1     
test_get_helper_code.py +7/-6     
test_tracer.py +2/-2     

Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Missing Import

    The new calls to find_all_functions_in_file and inspect_top_level_functions_or_methods are not imported, causing runtime NameError.

    functions_found = find_all_functions_in_file(temp_path)
    assert functions_found[temp_path][0].function_name == "test_function_eligible_for_optimization"
    Fixture Duplication

    The temp_dir fixture is defined locally in this file (and elsewhere); consider moving it to conftest.py to avoid duplication and potential conflicts.

    @pytest.fixture
    def temp_dir():
        with tempfile.TemporaryDirectory() as temp_dir:
            yield Path(temp_dir)

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Preserve full parent path

    Use the full current_path instead of just its name so the candidate paths preserve
    the actual directory structure. This avoids dropping directory levels when building
    relative paths.

    codeflash/code_utils/coverage_utils.py [47]

    -candidate_path = (Path(current_path.name) / candidates[-1]).as_posix()
    +candidate_path = (current_path / candidates[-1]).as_posix()
    Suggestion importance[1-10]: 8

    __

    Why: Using current_path.name drops directory context, leading to incorrect candidate paths; switching to the full current_path fixes the logic bug.

    Medium
    Normalize site-packages paths

    Resolve each site-packages path before checking is_relative_to to avoid false
    negatives when comparing resolved paths. This ensures both sides of the comparison
    are normalized.

    codeflash/code_utils/code_utils.py [174-176]

     file_path_resolved = file_path.resolve()
    -return any(file_path_resolved.is_relative_to(site_package_path) for site_package_path in site_packages)
    +return any(
    +    file_path_resolved.is_relative_to(site_package_path.resolve())
    +    for site_package_path in site_packages
    +)
    Suggestion importance[1-10]: 6

    __

    Why: Resolving only file_path but not each site_package_path can yield false negatives; normalizing both sides improves correctness without major complexity.

    Low
    Use pytest tmp_path fixture

    Instead of defining a custom temp_dir fixture, use pytest's built-in tmp_path
    fixture for temporary directories and update the test signature accordingly.

    tests/test_get_code.py [15]

    -def test_get_code_function(temp_dir: Path) -> None:
    +def test_get_code_function(tmp_path: Path) -> None:
    Suggestion importance[1-10]: 5

    __

    Why: Switching to the built-in tmp_path fixture simplifies the test by using pytest conventions and reduces custom fixture code.

    Low
    Specify file encoding

    Explicitly specify the file encoding when opening files for writing to ensure
    consistent behavior across platforms.

    tests/test_get_code.py [19-20]

    -with (temp_dir / "temp_file.py").open(mode="w") as f:
    +with (temp_dir / "temp_file.py").open(mode="w", encoding="utf-8") as f:
         f.write(code)
    Suggestion importance[1-10]: 5

    __

    Why: Explicitly adding encoding="utf-8" ensures consistent file writing behavior across platforms, improving portability.

    Low
    Remove duplicate assignment

    The second assignment to project_root_path is redundant and can be removed to avoid
    duplication.

    tests/test_get_helper_code.py [220-221]

    -project_root_path = tempdir_path.resolve()
     project_root_path = tempdir_path.resolve()
    Suggestion importance[1-10]: 4

    __

    Why: The duplicate assignment to project_root_path is redundant and removing it cleans up the code with minimal impact.

    Low

    codeflash-ai bot added a commit that referenced this pull request Jun 22, 2025
    …(`part-1-windows-fixes`)
    
    Here's a **runtime-optimized rewrite** based directly on the profile. The main high-cost issues are.
    
    1. **Repeated module/function alias generation** (especially in two function_imports loops) — avoid recomputing!
    2. **Many Path(file_path).as_posix() calls** — precompute or cache since file_path is not mutated.
    3. **Constant string formatting and dedent/indent in loops** — minimize usage.
    4. **Code structure** — restrain from repeated lookups and temporary allocations in large inner loops.
    
    Below is the **optimized code** (all results/behavior unchanged).
    
    
    
    ### Key improvements.
    - **All function/class/alias/file_path strings are now generated once up-front** and referenced by mapping, not recomputed every iteration.
    - **No .split/.join calls or Path() constructions in inner loops.**
    - **No textwrap.dedent/indent in inner loop. Uses fast string join with one indentation pass.**
    - **Eliminated duplicate lookups in functions_data.**
    - **Minimized unnecessary set/list and string allocations.**
    
    This should yield a significant performance boost (especially at scale). Output/behavior is identical; semantic minimization confirmed.
    Copy link
    Contributor

    codeflash-ai bot commented Jun 22, 2025

    ⚡️ Codeflash found optimizations for this PR

    📄 32% (0.32x) speedup for create_trace_replay_test_code in codeflash/benchmarking/replay_test.py

    ⏱️ Runtime : 23.4 milliseconds 17.7 milliseconds (best of 250 runs)

    I created a new dependent PR with the suggested changes. Please review:

    If you approve, it will be merged into this PR (branch part-1-windows-fixes).

    codeflash-ai bot added a commit that referenced this pull request Jun 22, 2025
    …t-1-windows-fixes`)
    
    Here’s a rewritten version of your program optimized for speed and minimal memory usage.
    
    - Avoid list "append" in the loop and instead preallocate the list using an iterative approach, then reverse at the end if needed.
    - Direct string concatenation and caching reduce creation of Path objects.
    - Explicit variable assignments reduce property accesses and speed up the while loop.
    
    Optimized code.
    
    
    
    **What changed:**
    - Avoided repeated property accesses by caching `parent`.
    - Used string formatting (which benchmarks very well in 3.11+) to avoid unnecessary Path object creation and method calls in the loop.
    - Otherwise, maintained the exact function signature and return values.
    Copy link
    Contributor

    codeflash-ai bot commented Jun 22, 2025

    ⚡️ Codeflash found optimizations for this PR

    📄 1,729% (17.29x) speedup for generate_candidates in codeflash/code_utils/coverage_utils.py

    ⏱️ Runtime : 247 milliseconds 13.5 milliseconds (best of 165 runs)

    I created a new dependent PR with the suggested changes. Please review:

    If you approve, it will be merged into this PR (branch part-1-windows-fixes).

    codeflash-ai bot added a commit that referenced this pull request Jun 22, 2025
    …`part-1-windows-fixes`)
    
    Here is an optimized version of your program with reduced code execution overhead, memory allocation, and efficient logic. All return values remain exactly the same, and function signatures are unchanged. Comments are preserved as required.
    
    **Key Optimizations:**
    - Convert repeated property accesses/calls to local variables where possible.
    - Reuse the same decorator AST node object where safe (AST is not mutated elsewhere).
    - Pre-check presence of target class and short-circuit early.
    - In the loop, short-circuit when `__init__` is found to avoid unnecessary iterations.
    
    
    
    **Summary of improvements:**
    - Reused AST decorator components, minimizing repeated object creation.
    - Early exit on target class and `__init__` findings.
    - Less variable assignment and number of iterations.
    - All core logic and comments are preserved.
    Copy link
    Contributor

    codeflash-ai bot commented Jun 22, 2025

    ⚡️ Codeflash found optimizations for this PR

    📄 91% (0.91x) speedup for InitDecorator.visit_ClassDef in codeflash/verification/instrument_codeflash_capture.py

    ⏱️ Runtime : 548 microseconds 287 microseconds (best of 104 runs)

    I created a new dependent PR with the suggested changes. Please review:

    If you approve, it will be merged into this PR (branch part-1-windows-fixes).

    codeflash-ai bot added a commit that referenced this pull request Jun 22, 2025
    … (`part-1-windows-fixes`)
    
    Here's an optimized rewrite of **your original code**, focusing on critical hotspots from the profiler data.
    
    **Optimization summary:**
    - Inline the `node_in_call_position` logic directly into **find_and_update_line_node** to avoid repeated function call overhead for every AST node; because inner loop is extremely hot.
    - Pre-split self.call_positions into an efficient lookup format for calls if positions are reused often.
    - Reduce redundant attribute access and method calls by caching frequently accessed values where possible.
    - Move branching on the most frequent path (ast.Name) up, and short-circuit to avoid unnecessary checks.
    - Fast path for common case: ast.Name, skipping .unparse and unnecessary packing/mapping.
    - Avoid repeated `ast.Name(id="codeflash_loop_index", ctx=ast.Load())` construction by storing as a field (`self.ast_codeflash_loop_index` etc.) (since they're repeated many times for a single method walk, re-use them).
    - Stop walking after the first relevant call in the node; don't continue iterating once we've performed a replacement.
    
    Below is the optimized code, with all comments and function signatures unmodified except where logic was changed.
    
    
    
    **Key performance wins:**
    - Hot inner loop now inlines the call position check, caches common constants, and breaks early.
    - AST node creation for names and constants is avoided repeatedly—where possible, they are re-used or built up front.
    - Redundant access to self fields or function attributes is limited, only happening at the top of find_and_update_line_node.
    - Fast path (ast.Name) is handled first and breaks early, further reducing unnecessary work in the common case.
    
    This will **substantially improve the speed** of the code when processing many test nodes with many function call ASTs.
    Copy link
    Contributor

    codeflash-ai bot commented Jun 22, 2025

    ⚡️ Codeflash found optimizations for this PR

    📄 24% (0.24x) speedup for InjectPerfOnly.visit_FunctionDef in codeflash/code_utils/instrument_existing_tests.py

    ⏱️ Runtime : 5.76 milliseconds 4.65 milliseconds (best of 191 runs)

    I created a new dependent PR with the suggested changes. Please review:

    If you approve, it will be merged into this PR (branch part-1-windows-fixes).

    @misrasaurabh1
    Copy link
    Contributor

    can you run atleast one e2e and unit test on windows? otherwise we will keep breaking this. I would also like a windows compatibility doc so that when the windows test fails, the dev knows what are the gotchas for windows, and they dont waste much time fixing it.

    @github-actions github-actions bot added the workflow-modified This PR modifies GitHub Actions workflows label Jun 23, 2025
    @KRRT7 KRRT7 enabled auto-merge July 3, 2025 20:57
    @KRRT7 KRRT7 requested a review from Saga4 July 3, 2025 20:57
    @KRRT7
    Copy link
    Contributor Author

    KRRT7 commented Jul 3, 2025

    CF-427

    @KRRT7 KRRT7 requested a review from misrasaurabh1 July 4, 2025 00:00
    codeflash-ai bot added a commit that referenced this pull request Jul 4, 2025
    …part-1-windows-fixes`)
    
    Here is an optimized version of your program, rewritten to minimize unnecessary work, allocation, and redundant computation, addressing the main bottlenecks surfaced by your profiling data.
    
    - **Tabulate**: Main performance issue is repeated function calls and list comprehensions inside loops. The column/row transforms, especially for header formatting and alignments, are the heaviest. We reduce allocation, avoid repeated calls when not needed, and specialize “headers” and “no headers” branches.
    - **existing_tests_source_for**: Avoids unnecessary dict lookups and string formatting by grouping updates, and directly iterates/precomputes keys, minimizing set/dict operations.
    - **General**: Inline tiny helpers, use local variables to reduce global lookups, and use tuple/list comprehension where possible.
    
    **Note**: All logic, side-effects, return values, and signatures are **preserved exactly** per your requirements.
    
    
    
    **Summary of main optimizations**.
    
    - **No repeated list comprehensions** in tight loops, especially for column and header formatting.
    - **Locals for small globals** (MIN_PADDING, width_fn, etc.), and cache path computation in `existing_tests_source_for`.
    - **No repeated dict/set membership tests**; minimized lookups to once per unique key.
    - **Fast header/row formatting** with minimal allocations and in-place width calculations.
    
    You should observe a faster runtime and lower memory usage, especially on large tables or when invoked many times. All function behaviors and signatures are precisely preserved.
    Copy link
    Contributor

    codeflash-ai bot commented Jul 4, 2025

    ⚡️ Codeflash found optimizations for this PR

    📄 43% (0.43x) speedup for existing_tests_source_for in codeflash/result/create_pr.py

    ⏱️ Runtime : 6.13 milliseconds 4.28 milliseconds (best of 364 runs)

    I created a new dependent PR with the suggested changes. Please review:

    If you approve, it will be merged into this PR (branch part-1-windows-fixes).

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    Review effort 4/5 workflow-modified This PR modifies GitHub Actions workflows
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants