Skip to content
TheChatArchiveThe Chat Archive
fable-5· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

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

  1. User

    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.

  2. Assistant

    Let me look at the existing extract-function tests involving statements inside use bodies:

  3. Assistant

    Now let me write a reproduction test to confirm the bug:

  4. Assistant

    Let me check the existing snapshot for extract_use_in_tail_position_2 to understand current expected behavior with nested uses:

  5. Assistant

    Now let me implement the fix. First, in can_extract_expression — handle use callback fns and use desugared calls:

  6. Assistant

    Next, the can_extract_statement Use arm:

  7. Assistant

    Now the third piece — override visit_typed_use so the desugared call isn't extracted as a whole expression when the selection is only inside the callback body:

  8. Assistant

    All 1539 tests pass. Let me add one more test to lock in the improved behavior when only part of a use body is selected starting from its first statement (previously the shared-start also caused over-extraction):

  9. Assistant

    The output is correct: only the two selected statements are extracted, with c returned (the trailing c in the new function is the generated return value). Accept the snapshot and run the whole suite again:

  10. Assistant

    Let me also add a test that mirrors the reporter's exact shape — nested uses, a use in the middle of the selection, and a pipeline in tail position:

  11. Assistant

    This matches the report's expected output exactly. Accept and run the full suite:

  12. Assistant

    All tests pass. Now add a changelog entry:

  13. Assistant

    Now let's run a final verification — clippy plus the full language server test suite:

  14. Assistant

    Fixed. Clippy is clean and all 1541 language-server tests pass.

    Root cause

    When Gleam desugars use a <- wibble into wibble(fn(a) { ...rest of block... }), three AST nodes — the use statement, the desugared call, and the callback fn — all have locations that end at the end of the enclosing block, with no closing delimiter after the last statement.

    The extract-function code action decided "did the user select this whole node?" with !completely_within(selection, node_range), which treats a shared extreme as "whole node selected". That's correct for blocks/fns that end in }, but for use-desugared nodes any selection of trailing statements inside the use body shares its end with the whole chain of use nodes — so the outermost use grabbed the extraction and the entire function body was extracted.

    Changes (language-server/src/code_action.rs)

    1. can_extract_statement — a use statement is now only extracted as a whole if the selection touches the use ... <- line itself (use_.location); otherwise traversal continues into the callback body.
    2. visit_typed_use override — when the selection doesn't touch the use ... <- line, the desugared call is skipped as an extraction candidate (its children are still traversed), preventing the call expression from being extracted whole.
    3. can_extract_expression — a use callback Fn (whose location starts at its first body statement, also with no delimiters) is now only extracted when the selection spans its entire body, otherwise the individual selected statements are extracted.

    Tests

    Four new snapshot tests in language-server/src/tests/action.rs:

    • selection of trailing statements inside a use body → extracts just those, leaving use a <- wibble + function(a)
    • same inside nested uses (the reported scenario)
    • selection starting at the first body statement but covering only part of the body → extracts only the selected statements (previously also over-extracted via the shared start)
    • selection containing a mid-body use and a tail pipeline, mirroring the reporter's code shape — output matches the report's expected result

    Also added a changelog entry under Unreleased → Bug fixes in CHANGELOG.md (I left off the attribution line since I don't know whose name to credit — you may want to add it).

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