From 3f4bdf2a254e7bfa7f9b2e6f00c3de02ff5ae5ea Mon Sep 17 00:00:00 2001 From: maggch Date: Mon, 2 Mar 2026 10:52:36 +0800 Subject: [PATCH] fix: use local variable for position to avoid cross-view contamination When creating a task, calculateNewPositionForTask is called in a loop for each view. Previously it wrote the calculated position back to the shared *Task struct (t.Position), causing subsequent views to skip their own position calculation and reuse the first view's value. This resulted in new tasks appearing in the middle of other views instead of at the top. Use a local variable instead of mutating t.Position so each view calculates its position independently. Ref: https://github.com/go-vikunja/vikunja/issues/2329 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- pkg/models/task_position.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/models/task_position.go b/pkg/models/task_position.go index 49f45b4c8..9465f2d4a 100644 --- a/pkg/models/task_position.go +++ b/pkg/models/task_position.go @@ -303,7 +303,8 @@ func recalculateTaskPositionsForRepair(s *xorm.Session, view *ProjectView) error } func calculateNewPositionForTask(s *xorm.Session, a web.Auth, t *Task, view *ProjectView) (*TaskPosition, error) { - if t.Position == 0 { + position := t.Position + if position == 0 { lowestPosition := &TaskPosition{} exists, err := s.Where("project_view_id = ?", view.ID). OrderBy("position asc"). @@ -327,14 +328,14 @@ func calculateNewPositionForTask(s *xorm.Session, a web.Auth, t *Task, view *Pro } } - t.Position = lowestPosition.Position / 2 + position = lowestPosition.Position / 2 } } return &TaskPosition{ TaskID: t.ID, ProjectViewID: view.ID, - Position: calculateDefaultPosition(t.Index, t.Position), + Position: calculateDefaultPosition(t.Index, position), }, nil }