cmake network_error ai_generated true

CMake Error: FetchContent: Invalid git refspec for repository https://github.com/user/repo.git: 'refs/heads/mainx' is not a valid reference.

ID: cmake/fetchcontent-invalid-git-refspec

Also available as: JSON · Markdown · 中文
92%Fix Rate
87%Confidence
1Evidence
2024-01-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
cmake 3.14 active
cmake 3.20 active
cmake 3.27 active

Root Cause

FetchContent_GitDeclare or FetchContent_Populate specifies a GIT_TAG that points to a nonexistent branch, tag, or commit hash.

generic

中文

FetchContent_GitDeclare 或 FetchContent_Populate 指定的 GIT_TAG 指向不存在的分支、标签或提交哈希。

Official Documentation

https://cmake.org/cmake/help/latest/module/FetchContent.html

Workarounds

  1. 95% success Verify the correct branch/tag name: run 'git ls-remote --heads --tags https://github.com/user/repo.git' to list all valid references, then update GIT_TAG accordingly, e.g., set(FETCHCONTENT_SOURCE_DIR_MYPROJ /local/path) or correct the tag to 'main'.
    Verify the correct branch/tag name: run 'git ls-remote --heads --tags https://github.com/user/repo.git' to list all valid references, then update GIT_TAG accordingly, e.g., set(FETCHCONTENT_SOURCE_DIR_MYPROJ /local/path) or correct the tag to 'main'.
  2. 90% success If using a commit hash, ensure it is the full SHA (40 characters for SHA-1) and exists on the remote: GIT_TAG "abc123def4567890abcdef1234567890abcdef12".
    If using a commit hash, ensure it is the full SHA (40 characters for SHA-1) and exists on the remote: GIT_TAG "abc123def4567890abcdef1234567890abcdef12".

中文步骤

  1. 验证正确的分支/标签名称:运行 'git ls-remote --heads --tags https://github.com/user/repo.git' 列出所有有效引用,然后更新 GIT_TAG,例如 set(FETCHCONTENT_SOURCE_DIR_MYPROJ /local/path) 或更正标签为 'main'。
  2. 如果使用提交哈希,确保它是完整的 SHA(SHA-1 为 40 个字符)并存在于远程:GIT_TAG "abc123def4567890abcdef1234567890abcdef12"。

Dead Ends

Common approaches that don't work:

  1. Cleaning the build directory and re-running cmake without changing GIT_TAG 100% fail

    The refspec is still invalid; cleaning only removes cached content, but the same tag will fail again.

  2. Setting FETCHCONTENT_UPDATES_DISCONNECTED to ON to skip fetching 95% fail

    This prevents fetching entirely, so the dependency won't be populated at all, causing a different error.