Write to MPS

How does the MPS writer of Gurobi work?

using JuMP, Gurobi

model = Model(Gurobi.Optimizer)

@variables(model, begin
    0 <= x <= 5
    0 <= y <= 10, Int
    z, Bin
end)

@objective(model, Max, x + 2y + 5z)
@constraint(model, x + y + z <= 10)
@constraint(model, x + 2y + z <= 15)

Gurobi.write_model(model.moi_backend.optimizer.model.inner, "test.mps")

gives me an empty file:

NAME 
ROWS
 N  OBJ
COLUMNS
RHS
BOUNDS
ENDATA

JuMP.write_to_file(model, "test_JuMP.mps") would work.
The reason seems to be that the inner model is empty until optimize! is called.
How do I transfer the model without a call of optimize!?

To avoid the copy_to on optimize!, use direct mode:

model = direct_model(Gurobi.Optimizer())
@variables(model, begin
    0 <= x <= 5
    0 <= y <= 10, Int
    z, Bin
end)
@objective(model, Max, x + 2y + 5z)
@constraint(model, x + y + z <= 10)
@constraint(model, x + 2y + z <= 15)
Gurobi.write_model(backend(model), "test.mps")