Problem with Optim.optimize

Consider the minimal example

using Optim

f(x) = (x[1]-2)^2 + (x[2]-3)^2 + sin(3*x[1] + 1) + cos(5*x[2])

optimize(f, [1., 1.], BFGS())

For reasons that I don’t comprehend, I get the following error:

ERROR: BoundsError: attempt to access Float64 at index [2]
Stacktrace:
  [1] indexed_iterate(I::Float64, i::Int64, state::Nothing)
    @ Base .\tuple.jl:171
  [2] (::LineSearches.var"#ϕdϕ#make_ϕdϕ##0"{Optim.ManifoldObjective{…}, Vector{…}, Vector{…}, Vector{…}})(α::Float64)
    @ LineSearches C:\Users\xyz\.julia\packages\LineSearches\2BCHN\src\LineSearches.jl:32
  [3] (::LineSearches.HagerZhang{…})(ϕ::Function, ϕdϕ::LineSearches.var"#ϕdϕ#make_ϕdϕ##0"{…}, c::Float64, phi_0::Float64, dphi_0::Float64)
    @ LineSearches C:\Users\xyz\.julia\packages\LineSearches\2BCHN\src\hagerzhang.jl:147
  [4] HagerZhang
    @ C:\Users\xyz\.julia\packages\LineSearches\2BCHN\src\hagerzhang.jl:103 [inlined]
  [5] perform_linesearch!(state::Optim.BFGSState{…}, method::Optim.BFGS{…}, d::Optim.ManifoldObjective{…})
    @ Optim C:\Users\xyz\.julia\packages\Optim\7krni\src\utilities\perform_linesearch.jl:58
  [6] update_state!(d::NLSolversBase.OnceDifferentiable{…}, state::Optim.BFGSState{…}, method::Optim.BFGS{…})
    @ Optim C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\solvers\first_order\bfgs.jl:143
  [7] optimize(d::NLSolversBase.OnceDifferentiable{…}, initial_x::Vector{…}, method::Optim.BFGS{…}, options::Optim.Options{…}, state::Optim.BFGSState{…})       
    @ Optim C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\optimize\optimize.jl:65
  [8] optimize
    @ C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\optimize\optimize.jl:43 [inlined]
  [9] #optimize#67
    @ C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\optimize\interface.jl:274 [inlined]
 [10] optimize
    @ C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\optimize\interface.jl:264 [inlined]
 [11] optimize(f::Function, initial_x::Vector{…}, method::Optim.BFGS{…})
    @ Optim C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\optimize\interface.jl:264
 [12] top-level scope
    @ c:\Users\xyz\Desktop\debug.jl:5
Some type information was truncated. Use `show(err)` to see complete types.

I’m using Optim v1.13.2

are you able to try from a clean julia instance? I can’t reproduce from master

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.12.4 (2026-01-06)
 _/ |\__'_|_|_|\__'_|  |
|__/                   |

julia> using Optim

julia> f(x) = (x[1]-2)^2 + (x[2]-3)^2 + sin(3*x[1] + 1) + cos(5*x[2])
f (generic function with 1 method)

julia> optimize(f, [1., 1.], BFGS())
 * Status: success

 * Candidate solution
    Final objective value:     3.649729e+00

 * Found with
    Algorithm:     BFGS

 * Convergence measures
    |x - x'|               = 1.10e-06 ≰ 0.0e+00
    |x - x'|/|x'|          = 7.98e-07 ≰ 0.0e+00
    |f(x) - f(x')|         = 7.52e-12 ≰ 0.0e+00
    |f(x) - f(x')|/|f(x')| = 2.06e-12 ≰ 0.0e+00
    |g(x)|                 = 5.35e-09 ≤ 1.0e-08

 * Work counters
    Seconds run:   0  (vs limit Inf)
    Iterations:    6
    f(x) calls:    16
    ∇f(x) calls:   16
    ∇f(x)ᵀv calls: 0

julia> versioninfo()
Julia Version 1.12.4
Commit 01a2eadb04* (2026-01-06 16:56 UTC)
Build Info:

    Note: This is an unofficial build, please report bugs to the project
    responsible for this build and not to the Julia project unless you can
    reproduce the issue using official builds available at https://julialang.org

Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 14 × Intel(R) Core(TM) Ultra 7 165U
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, alderlake)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 14 virtual cores)