Skip to content
TheChatArchiveThe Chat Archive
MiniMaxminimax/minimax-m3· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION Extract function extracts too much When triggering Extract function…

  1. User

    ISSUE DESCRIPTION

    Extract function extracts too much When triggering Extract function with the selection:

    /// prepare an empty gleam project in the `target_path`
    ///
    fn prepare_project_structure(
      target_path target: TargetPath,
      user_id user_id: uuid.Uuid,
    ) -> Result(Nil, SubmitError) {
      // get the priv dir
      use priv <- result.try(
        application.priv_directory("gleeps")
        |> result.replace_error(FailedToFindPrivDirectory),
      )
    
      // copy the base project to the target_path
      use _ <- result.try(
        simplifile.copy_directory(filepath.join(priv, "base_project"), target.path)
        |> result.replace_error(FailedToCopyBaseProject),
      )
    
      // replace $PROJECT_NAME placeholder in the gleam.toml
      // <-------------------------------------------------------------- selected from here
      let toml_path =
        target.path
        |> filepath.join("gleam.toml")
    
      use gleam_toml <- result.try(
        toml_path
        |> simplifile.read
        |> result.replace_error(FailedToReadGleamToml),
      )
    
      let gleam_toml =
        gleam_toml
        |> string.replace("$PROJECT_NAME", with: user_id_to_string(user_id))
    
      simplifile.write(toml_path, gleam_toml)
      |> result.replace_error(FailedToWriteGleamToml)
      // <---------------------------------------------------------------- to here
    }
    

    It will extract the entire function content into a new function leaving:

    fn prepare_project_structure(
      target_path target: TargetPath,
      user_id user_id: uuid.Uuid,
    ) -> Result(Nil, SubmitError) {
      function(target, user_id)
    }
    

    instead of the expected:

    /// prepare an empty gleam project in the `target_path`
    ///
    fn prepare_project_structure(
      target_path target: TargetPath,
      user_id user_id: uuid.Uuid,
    ) -> Result(Nil, SubmitError) {
      // get the priv dir
      use priv <- result.try(
        application.priv_directory("gleeps")
        |> result.replace_error(FailedToFindPrivDirectory),
      )
    
      // copy the base project to the target_path
      use _ <- result.try(
        simplifile.copy_directory(filepath.join(priv, "base_project"), target.path)
        |> result.replace_error(FailedToCopyBaseProject),
      )
      // replace $PROJECT_NAME placeholder in the gleam.toml
      function(target, user_id)
    }
    
    fn function(
      target: TargetPath,
      user_id: uuid.Uuid,
    ) -> Result(Nil, SubmitError) {
      let toml_path =
        target.path
        |> filepath.join("gleam.toml")
    
      use gleam_toml <- result.try(
        toml_path
        |> simplifile.read
        |> result.replace_error(FailedToReadGleamToml),
      )
    
      let gleam_toml =
        gleam_toml
        |> string.replace("$PROJECT_NAME", with: user_id_to_string(user_id))
    
      simplifile.write(toml_path, gleam_toml)
      |> result.replace_error(FailedToWriteGleamToml)
    }
    

    Using gleam 1.16.0 in neovim nightly

    Interface

    No new interfaces are introduced.

    ADDITIONAL ADVICE

    Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use git checkout <file> to restore the file to its original state. You can also reset all changes in the repository using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.

    CHECKLIST

    Before submitting your solution, please go over the following checklist and make sure you've done everything:

    • If an error was reported in the issue description, I have successfully reproduced it.
    • If an error was reported in the issue description, I have confirmed that my fix resolves the error.
    • I have thought about edge cases and made sure my fix handles them as well.
    • I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.

    Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!

    (Current directory: /gleam, current file: none) bash-$

  2. MiniMax Assistant

    Let me start by exploring the repository structure to understand what we're working with.

  3. MiniMax Assistant

    This is the Gleam compiler repository. Let me explore the language server code since the issue is related to code extraction in the language server.

  4. MiniMax Assistant

    Let me look at the ExtractFunction implementation:

  5. MiniMax Assistant

    Now let me look at the helpers that decide whether we can extract:

  6. MiniMax Assistant

    Let me look at the test cases for extract_function to understand how it currently works:

  7. MiniMax Assistant

    Let me continue looking at more tests and look at extract_function_partially_selected:

  8. MiniMax Assistant

    Let me look at the within and completely_within helper functions:

  9. MiniMax Assistant

    Let me look at the engine setup and understand how to set up tests:

  10. MiniMax Assistant

    Let me look at find_position_of and select_until:

  11. MiniMax Assistant

    Let me look for the RangeSelector's find_range method:

  12. MiniMax Assistant

    Let me see the test setup to understand how to write a reproducer:

  13. MiniMax Assistant

    Let me try running the existing tests to confirm baseline:

  14. MiniMax Assistant

    Now let me create a temporary reproducer test to confirm the issue:

  15. MiniMax Assistant

    Let me create a temporary test that I can run to confirm the issue:

  16. MiniMax Assistant

    Let me look at the action more carefully and reproduce the exact issue:

  17. MiniMax Assistant

    Let me try with a different end position:

  18. MiniMax Assistant

    Let me check how the test framework handles raw strings:

  19. MiniMax Assistant

    Let me first look at what happens with this specific code, and trace the logic:

  20. MiniMax Assistant

    Let me first add some debug logging to trace through this:

  21. MiniMax Assistant

    Let me modify the test to print all actions to debug:

  22. MiniMax Assistant

    Now let me run the actual repro test that should show the issue:

  23. MiniMax Assistant

    Let me try with selection ending after the function's closing brace:

  24. MiniMax Assistant

    Let me add some print statements to trace through the issue:

  25. MiniMax Assistant

    Let me add print statements to the source code temporarily:

  26. MiniMax Assistant

    Let me add debug logging to understand what's happening:

  27. MiniMax Assistant

    Let me add debug print statements:

  28. MiniMax Assistant

    Let me investigate the AST locations for these statements:

  29. MiniMax Assistant

    Let me look at what location is for a Use statement:

  30. MiniMax Assistant

    Let me look at how use_.call.location() is computed for the typed call:

  31. MiniMax Assistant

    OK so for TypedExpr::Call, the location field is the location of the whole call expression. This includes the function and ALL its arguments.

    For a use priv <- result.try(application.priv_directory("gleeps") |> result.replace_error(...), fn(priv) { ... }) statement, the call field (typed) is a TypedExpr::Call. The location of this call covers EVERYTHING from result.try(...) through the END of the callback function (which is the END of the block).

    So when the use statement's location is computed as use_.location.merge(&use_.call.location()), it includes the entire block.

    This explains why all statements after the FIRST use statement have end: 1054 (the end of the function body).

    Now, in can_extract_statement, we use overlaps(statement_range, selected_range). So when the user's selection is from let toml_path to end of function, even use priv <- result.try(...) has its END at end of function, so it OVERLAPS with the selection (since the start of selection is at line 27, but use priv ends at line 42).

    But wait, overlaps checks if any corner is in the other range:

    pub fn overlaps(a: Range, b: Range) -> bool {
        position_within(a.start, b)
            || position_within(a.end, b)
            || position_within(b.start, a)
            || position_within(b.end, a)
    }
    

    So for Statement 1 (line 16-42) and selection (line 27-42):

    • statement.start (16) within selection? No.
    • statement.end (42) within selection? Yes (27-42 includes 42).
    • selection.start (27) within statement? Yes (16-42 includes 27).
    • selection.end (42) within statement? Yes.

    So overlaps = true. That's the BUG.

    So the fix needs to address that the Use statement's location extends to the end of the block, which is wrong for determining the actual statement's range. We need a way to get the statement's REAL end position (not the position that extends to end of block due to the callback).

    Looking at last_location:

    pub fn last_location(&self) -> SrcSpan {
        match self {
            Statement::Expression(expression) => expression.last_location(),
            Statement::Assignment(assignment) => assignment.value.last_location(),
            Statement::Use(use_) => use_.call.last_location(),
            Statement::Assert(assert) => assert.value.last_location(),
        }
    }
    

    For a Use, last_location returns use_.call.last_location(). The call's last_location is the location of the LAST argument, which would be the callback function. The callback function's last_location would be the location of the last statement in its body. So this would still cover the entire rest of the block.

    Hmm. Let me look at this differently. Maybe the issue is that for extract_statement, we should use the START of the next statement to determine the END of the current one, not the END of the use statement.

    Actually wait, let me re-read the comment in the AST:

    Statement::Use(use_) => use_.location.merge(&use_.call.location()),
    

    And the comment says:

    /// This is the location of the whole use line, starting from the `use`
    /// keyword and ending with the function call on the right hand side of
    /// `<-`.
    ///
    /// ```gleam
    /// use a <- result.try(result)
    /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    /// ```
    ///
    pub location: SrcSpan,
    

    So use_.location is just the use line. But use_.call.location() for a TypedExpr::Call covers the whole function call INCLUDING the callback function.

    So merging these gives the full block coverage. This is the BUG.

    Actually wait, let me look at this in code_action.rs's can_extract_statement:

    fn can_extract_statement(&self, statement: &TypedStatement) -> bool {
        let statement_range = self.edits.src_span_to_lsp_range(statement.location());
        let selected_range = self.params.range;
    

    So we use statement.location(). For a Use, this is use_.location.merge(&use_.call.location()) which spans the whole block.

    I think the fix should be: for a Use statement, we should use the location of just the use line, NOT the full extent of the callback. Because the user-visible "use statement" is just one line.

    But wait, there are tests that already test that a Use statement covering the entire block is correctly NOT extracted when only the inner block is selected:

    // If the selected range falls completely within the expression, we don't
    // want to extract it.
    ast::Statement::Use(_) | ast::Statement::Assert(_) => {
        !completely_within(selected_range, statement_range)
    }
    

    So this is checking that if the selection is completely within the Use statement, don't extract. This makes sense because if the selection is INSIDE the Use's callback, that's a different scenario.

    But this check uses statement_range, which incorrectly extends to the end of the block.

    OK so the fix needs to be more careful. Let me look at what the last_location would tell us.

    Actually, the right approach might be: for the purpose of extracting a function, we should determine the STATEMENT's location more carefully.

    Looking at last_location:

    • For Use, returns use_.call.last_location().
    • call.last_location() for TypedExpr::Call returns the location of the LAST element. Let me look:
    pub fn last_location(&self) -> SrcSpan {
        match self {
            TypedExpr::Int { location, .. }
            | TypedExpr::Float { location, .. }
            | TypedExpr::String { location, .. }
            | TypedExpr::Var { location, .. }
            | TypedExpr::List { location, .. }
            | TypedExpr::Call { location, .. }
            ...
        }
    }
    

    For TypedExpr::Call { location, .. }, it returns the call's location field. And for TypedExpr::Fn:

    Let me check:

  32. MiniMax Assistant

    Let me look at how right_hand_side_location is set:

  33. MiniMax Assistant

    Let me check how the typing phase constructs the TypedUse:

  34. MiniMax Assistant

    Let me see what call_location is in infer_call:

  35. MiniMax Assistant

    Let me check what other places use TypedStatement::location():

  36. MiniMax Assistant

    Let me check tests that depend on the current behavior:

  37. MiniMax Assistant

    Now let me revert my debug print statement first:

  38. MiniMax Assistant

    Now I understand the bug. Let me revert all the debug stuff:

  39. MiniMax Assistant

    Let me verify by looking at the typing code:

  40. MiniMax Assistant

    Let me look at additional use statement extraction tests:

  41. MiniMax Assistant

    Let me code up the fix. I need to handle this in multiple places:

  42. MiniMax Assistant

    Let me look at the actual construction of use's callback:

  43. MiniMax Assistant

    OK so the sequence_location is the location of the use line + the rest of the statements. Then call_location = use_.location.start to sequence_location.end. This call_location is what becomes the typed call's location.

    Now this is the SEQUENCE of statements, not just the immediate next one. So the typed call extends to the end of the block (containing all subsequent statements).

    Hmm wait, the sequence_location is from the un-typed use's parser. Let me find where this is computed:

  44. MiniMax Assistant

    Now I understand the issue. Let me implement the fix:

    The issue is that TypedStatement::Use(use_).location() returns a span that extends to the end of the block (because the typed call's location includes the callback that extends to the end of block). This is intentional in most cases - the use conceptually "owns" the rest of the block via its callback.

    But for can_extract_statement, we want to determine if the selection overlaps with the use's USER-VISIBLE source code (just the use line), not the callback.

    Let me first verify the reproducer more carefully:

  45. MiniMax Assistant

    Now let me apply the fix:

  46. MiniMax Assistant

    Let me look at Assert's location:

  47. MiniMax Assistant

    Now let me apply the fix:

  48. MiniMax Assistant

    Let me use the replace command:

  49. MiniMax Assistant

    Good. The fix is applied. Let me also remove the now-redundant comment block above the new code (the old comment was about "if the selected range falls completely within the expression"). Let me check what's there now:

  50. MiniMax Assistant

    Let me clean up the redundant comment:

  51. MiniMax Assistant

    Let me build and run the tests:

  52. MiniMax Assistant

    All extract tests pass. Let me also run a broader set of tests:

  53. MiniMax Assistant

    All 1537 language-server tests pass! Let me also run the compiler-core tests to ensure nothing else is broken:

  54. MiniMax Assistant

    Now let me create a temporary reproducer to verify the fix works for the exact issue:

  55. MiniMax Assistant

    The output matches the expected behavior exactly! Let me remove the temporary test and verify the snapshot:

  56. MiniMax Assistant

    Let me also look at more edge case tests:

  57. MiniMax Assistant

    Let me verify the snapshot tests pass:

  58. MiniMax Assistant

    Let me also look at more edge cases. Let me make sure all edge cases are tested:

  59. MiniMax Assistant

    Let me re-add the test to verify the behavior:

  60. MiniMax Assistant

    OK so the test is the same as the original. Since I have a temp test at the END, it should be a separate test. Let me run both:

  61. MiniMax Assistant

    Let me now run a comprehensive test:

  62. MiniMax Assistant

    Let me check the failures:

  63. MiniMax Assistant

    These failures are unrelated to my fix - they're due to escript not being installed. Let me verify these tests were failing before my changes:

  64. MiniMax Assistant

    These failures are pre-existing and unrelated. Let me clean up the leftover snapshot files:

  65. MiniMax Assistant

    Let me view the diff:

  66. MiniMax Assistant

    Let me run all language server tests once more:

  67. MiniMax Assistant

    Let me add an end-to-end reproducer test that matches the exact issue scenario:

  68. MiniMax Assistant

    The fix produces exactly the expected output from the issue. Let me clean up:

  69. MiniMax Assistant

    Now let me view the final diff:

  70. MiniMax Assistant

    Let me run all tests in the language server once more:

  71. MiniMax Assistant

    All 1537 tests pass. Let me also clean up the snapshot files for tests I may have created:

  72. MiniMax Assistant

    Let me also clean up the temp files:

  73. MiniMax Assistant

    Now let me do a final review of my changes:

  74. MiniMax Assistant

    Let me go through the checklist explicitly:

    Checklist Review

    1. If an error was reported in the issue description, I have successfully reproduced it.

    YES. I created a temporary test that reproduces the issue using the exact code and selection range from the issue description. Without my fix, the action incorrectly extracted the entire function body, leaving only function(target, user_id) in the original function. This matches the bug described in the issue.

    2. If an error was reported in the issue description, I have confirmed that my fix resolves the error.

    YES. After applying the fix, my temporary test produces the exact expected output from the issue - the original function keeps the use priv <- result.try(...) and use _ <- result.try(...) statements, then has function(target, user_id) replacing the selected code. The new function contains exactly the code that was selected.

    3. I have thought about edge cases and made sure my fix handles them as well.

    YES. The fix changes can_extract_statement for Statement::Use to use use_.location (which is just the use line span from the use keyword to the end of the right-hand side expression) instead of the full statement location (which includes the desugared callback extending to the end of the block). This handles:

    • Selection entirely before use line: overlaps returns false, returns false. ✓
    • Selection entirely after use line: overlaps returns false, returns false. ✓
    • Selection completely within use line: overlaps returns true, completely_within returns true, returns false (descend further). ✓
    • Selection contains use line: overlaps returns true, completely_within returns false, returns true (extract as Use). ✓
    • Selection partially overlaps use line (extends past): extract as Use, the use still consumes the rest of the block via the existing merge logic. ✓
    • Selection is exactly the use line boundary: overlaps returns true (position_within allows equality), extracted. ✓
    • Two use statements before selection: only the ones whose use lines actually overlap the selection are considered. ✓

    4. I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass.

    YES. All 1537 tests in the language-server package pass, including:

    • extract_function_* tests (22 tests)
    • extract_use_* tests (7 tests including extract_use_inside_function, extract_use_inside_block, extract_use_with_some_statements_before, extract_use_including_some_statements_after, extract_use_in_tail_position, extract_use_in_tail_position_2)
    • All other extract tests (179 tests for "extract" prefix)
    • All other action tests (1537 total)

    All 3890 compiler-core tests also pass.

    The pre-existing escript_* failures in test-commands are unrelated infrastructure issues (missing escript command in the test environment) and were failing before my changes as well.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.