Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(artifact): write and detect artifacts through wide paths on Windows (#1171)
Martin Vogel7658954140b9
diff --git a/src/pipeline/artifact.c b/src/pipeline/artifact.cindex 2dd368c28..9594c8a09 100644--- a/src/pipeline/artifact.c+++ b/src/pipeline/artifact.c@@ -185,7 +185,10 @@ static int write_file_atomic(const char *path, const char *data, size_t len,return CBM_NOT_FOUND;}- FILE *fp = fopen(tmp, "wb");+ /* cbm_fopen / cbm_rename_replace: wide paths on Windows. The ANSI CRT fopen+ * and MoveFileExA mangled a repo path outside the active code page (a CJK+ * checkout), so every persistent export failed there (#1171). */+ FILE *fp = cbm_fopen(tmp, "wb");if (!fp) {file_error_set(out_err, "open_temp", errno);return CBM_NOT_FOUND;@@ -207,22 +210,14 @@ static int write_file_atomic(const char *path, const char *data, size_t len,return CBM_NOT_FOUND;}-#ifdef _WIN32- /* MoveFileEx replace approach suggested by @Ayush7Ranjan in #492. */- if (!MoveFileExA(tmp, path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {- DWORD saved_error = GetLastError();- cbm_unlink(tmp);- file_error_set(out_err, "rename_temp", (int)saved_error);- return CBM_NOT_FOUND;- }-#else- if (rename(tmp, path) != 0) {+ /* Replace-existing rename (MoveFileExW write-through on Windows, the+ * approach suggested by @Ayush7Ranjan in #492); errno carries the cause. */+ if (cbm_rename_replace(tmp, path) != 0) {int saved_errno = errno;cbm_unlink(tmp);file_error_set(out_err, "rename_temp", saved_errno);return CBM_NOT_FOUND;}-#endifreturn 0;}@@ -1188,7 +1183,7 @@ int cbm_artifact_import(const char *repo_path, const char *cache_db_path) {* stale WAL next to the cache path would be replayed on top of the* imported file at the next open (#897). */cbm_remove_db_sidecars(cache_db_path);- if (rename(tmp_path, cache_db_path) != 0) {+ if (cbm_rename_replace(tmp_path, cache_db_path) != 0) {cbm_log_error("artifact.import", "err", "rename_to_cache");cbm_unlink(tmp_path);return CBM_NOT_FOUND;@@ -1218,8 +1213,15 @@ bool cbm_artifact_exists(const char *repo_path) {char zst_path[CBM_SZ_4K];artifact_path(zst_path, sizeof(zst_path), repo_path, CBM_ARTIFACT_FILENAME);- struct stat st;- if (stat(zst_path, &st) != 0 || st.st_size == 0) {+ /* Present and non-empty. cbm_fopen, not stat(): the ANSI CRT stat misses a+ * repo path outside the active Windows code page (#1171). */+ FILE *fp = cbm_fopen(zst_path, "rb");+ if (!fp) {+ return false;+ }+ int first = fgetc(fp);+ (void)fclose(fp);+ if (first == EOF) {return false;}diff --git a/tests/test_artifact.c b/tests/test_artifact.cindex a7d96cdd9..6206463e3 100644--- a/tests/test_artifact.c+++ b/tests/test_artifact.c@@ -334,6 +334,47 @@ TEST(artifact_gitattributes_created) {PASS();}+/* #1171: a repository (and a cache directory) under a non-ASCII path must+ * export, report the artifact present, and import — also onto an existing+ * cache db. On Windows the atomic writer used the ANSI CRT fopen + MoveFileExA+ * and the existence check the ANSI stat, so CJK characters outside the active+ * code page made every step fail. The bytes are escaped UTF-8 for a CJK repo+ * directory name and a CJK cache directory name (kept ASCII in source). */+#define ART_CJK_REPO "\xe6\x8c\x81\xe4\xb9\x85\xe5\x8c\x96\xe5\xa4\x8d\xe7\x8e\xb0"+#define ART_CJK_CACHE "\xe5\xaf\xbc\xe5\x85\xa5"+TEST(artifact_roundtrip_non_ascii_paths) {+ setup_artifact_test();+ create_test_db(g_db);++ char repo[1024];+ snprintf(repo, sizeof(repo), "%s/cbm-" ART_CJK_REPO, g_tmpdir);+ ASSERT_TRUE(cbm_mkdir_p(repo, 0755));++ ASSERT_EQ(cbm_artifact_export(g_db, repo, "test-proj", CBM_ARTIFACT_FAST), 0);+ char zst[1024];+ snprintf(zst, sizeof(zst), "%s/.codebase-memory/graph.db.zst", repo);+ ASSERT_TRUE(cbm_file_exists(zst));+ ASSERT_TRUE(cbm_artifact_exists(repo));++ char cache_dir[1024];+ snprintf(cache_dir, sizeof(cache_dir), "%s/" ART_CJK_CACHE, g_tmpdir);+ ASSERT_TRUE(cbm_mkdir_p(cache_dir, 0755));+ char import_db[1024];+ snprintf(import_db, sizeof(import_db), "%s/imported.db", cache_dir);+ ASSERT_EQ(cbm_artifact_import(repo, import_db), 0);+ /* Re-import replaces the existing cache db (the second-session path). */+ ASSERT_EQ(cbm_artifact_import(repo, import_db), 0);++ cbm_store_t *s = cbm_store_open_path(import_db);+ ASSERT_NOT_NULL(s);+ ASSERT_EQ(cbm_store_count_nodes(s, "test-proj"), 2);+ ASSERT_EQ(cbm_store_count_edges(s, "test-proj"), 1);+ cbm_store_close(s);++ cleanup_dir(g_tmpdir);+ PASS();+}+TEST(artifact_export_rename_failure_logs_specific_error) {setup_artifact_test();create_test_db(g_db);@@ -1102,6 +1143,7 @@ SUITE(artifact) {RUN_TEST(artifact_schema_version_mismatch);RUN_TEST(artifact_import_missing);RUN_TEST(artifact_gitattributes_created);+ RUN_TEST(artifact_roundtrip_non_ascii_paths);RUN_TEST(artifact_export_rename_failure_logs_specific_error);RUN_TEST(pipeline_persistence_export_failure_returns_error);RUN_TEST(artifact_import_rejects_size_mismatch);