go module_error ai_generated partial

go: 歧义导入:在多个模块中发现包 X:github.com/foo/bar 和 github.com/baz/bar

go: ambiguous import: found package X in multiple modules: github.com/foo/bar and github.com/baz/bar

ID: go/go-mod-tidy-ambiguous-import

其他格式: JSON · Markdown 中文 · English
82%修复率
87%置信度
1证据数
2024-05-18首次发现

版本兼容性

版本状态引入弃用备注
go1.21 active
go1.22 active

根因分析

两个不同的模块提供了相同的导入路径,导致模块解析歧义。

English

Two different modules provide the same import path, causing ambiguity in the module resolution.

generic

官方文档

https://go.dev/ref/mod#go-mod-file-require

解决方案

  1. 在 go.mod 中使用 replace 指令强制指定一个模块:replace github.com/foo/bar => github.com/foo/bar v1.2.3
  2. 重命名冲突的包之一,或通过 vendor 使用不同的导入路径。

无效尝试

常见但无效的做法:

  1. Running go mod tidy repeatedly hoping it resolves 95% 失败

    go mod tidy cannot resolve ambiguity; it requires manual intervention to specify which module to use.

  2. Deleting go.sum and re-running go mod download 80% 失败

    Deleting go.sum doesn't fix the import path conflict; it only removes checksums.