From 86b3eb9582cc1fbb182e78e861ff789ce4716036 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 5 Mar 2026 13:00:24 -0500 Subject: [PATCH] fix(setup): guard against overwriting existing problem files If the destination source file already exists on disk and belongs to a different platform/contest/problem in the cache, abort with an error rather than silently clobbering it. --- lua/cp/setup.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lua/cp/setup.lua b/lua/cp/setup.lua index e27ceb0..48bdf33 100644 --- a/lua/cp/setup.lua +++ b/lua/cp/setup.lua @@ -278,6 +278,19 @@ function M.setup_problem(problem_id, language) return end + if vim.fn.filereadable(source_file) == 1 then + local existing = cache.get_file_state(vim.fn.fnamemodify(source_file, ':p')) + if existing and (existing.platform ~= platform or existing.contest_id ~= (state.get_contest_id() or '') or existing.problem_id ~= problem_id) then + logger.log( + ('File %q already exists for %s/%s %s.'):format( + source_file, existing.platform, existing.contest_id, existing.problem_id + ), + { level = vim.log.levels.ERROR } + ) + return + end + end + local contest_dir = vim.fn.fnamemodify(source_file, ':h') local is_new_dir = vim.fn.isdirectory(contest_dir) == 0 vim.fn.mkdir(contest_dir, 'p')