cmake
network_error
ai_generated
true
CMake 错误:FetchContent:仓库 https://github.com/user/repo.git 的 git refspec 无效:'refs/heads/mainx' 不是有效引用。
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
92%修复率
87%置信度
1证据数
2024-01-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| cmake 3.14 | active | — | — | — |
| cmake 3.20 | active | — | — | — |
| cmake 3.27 | active | — | — | — |
根因分析
FetchContent_GitDeclare 或 FetchContent_Populate 指定的 GIT_TAG 指向不存在的分支、标签或提交哈希。
English
FetchContent_GitDeclare or FetchContent_Populate specifies a GIT_TAG that points to a nonexistent branch, tag, or commit hash.
官方文档
https://cmake.org/cmake/help/latest/module/FetchContent.html解决方案
-
验证正确的分支/标签名称:运行 'git ls-remote --heads --tags https://github.com/user/repo.git' 列出所有有效引用,然后更新 GIT_TAG,例如 set(FETCHCONTENT_SOURCE_DIR_MYPROJ /local/path) 或更正标签为 'main'。
-
如果使用提交哈希,确保它是完整的 SHA(SHA-1 为 40 个字符)并存在于远程:GIT_TAG "abc123def4567890abcdef1234567890abcdef12"。
无效尝试
常见但无效的做法:
-
Cleaning the build directory and re-running cmake without changing GIT_TAG
100% 失败
The refspec is still invalid; cleaning only removes cached content, but the same tag will fail again.
-
Setting FETCHCONTENT_UPDATES_DISCONNECTED to ON to skip fetching
95% 失败
This prevents fetching entirely, so the dependency won't be populated at all, causing a different error.