亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當頂級模塊及其子模塊之一分別作為單獨版本導入時,如何解決沖突的 go 模塊依賴關系?

當頂級模塊及其子模塊之一分別作為單獨版本導入時,如何解決沖突的 go 模塊依賴關系?

Go
躍然一笑 2023-07-26 13:16:10
我的項目中有兩個依賴項。go.mod:module github.com/test-org/test-repogo 1.12require (    github.com/foo/bar v1.0.0    github.com/raz/mataz v1.0.0)運行后go mod download,這兩個依賴項會導致github.com/shared/dependency下載兩個不同版本的 a。有趣的是,它github.com/shared/dependency包含子模塊,例如:dependency  -- go.mod  -- api      -- go.mod檢查下載的模塊顯示兩個版本已下載到我的本地計算機:ls ${GOPATH}/pkg/mod/github.com/shared:[dir] dependency    [dir] [email protected] ${GOPATH}/pkg/mod/github.com/shared/dependency:[dir] [email protected]查看這些目錄的內容:${GOPATH}/pkg/mod/github.com/shared/[email protected]:v1.1.0 中整個存儲庫的文件內容,包括api帶有自己go.mod文件的文件夾。${GOPATH}/pkg/mod/github.com/shared/dependency/[email protected]:apiv1.2.0 中 repo 文件夾的文件內容,包括go.mod文件。最后,.go我的文件中有一個test-repo具有以下設置的文件:package test-packageimport (    "github.com/foo/bar")func MyFunc() {...bar.NewBar()...}MyFunc當我嘗試運行(其他地方存在的)測試時,我收到一條unknown import path...ambiguous import...錯誤消息。例如go test github.com/test-org/test-repo/test-package -test.run=TestMyFunc -v:unknown import path "github.com/shared/dependency/api": ambiguous import: found github.com/shared/dependency/api in multiple modules:    github.com/shared/dependency v1.1.0 (${GOPATH}/pkg/mod/github.com/shared/[email protected]/api)    github.com/shared/dependency v1.2.0 (${GOPATH}/pkg/mod/github.com/shared/dependency/[email protected])錯誤指向存儲庫中導入的文件import行。鑒于有兩個可用版本,它不知道在我的本地文件夾中選擇哪個:.gogithub.com/shared/dependency/apigithub.com/foo/barapi${GOPATH}/pkg/mod${GOPATH}/pkg/mod/github.com/shared/[email protected]/api${GOPATH}/pkg/mod/github.com/shared/dependency/[email protected]有什么方法可以使該go test調用工作(解決依賴沖突)?我的兩個依賴項都沒有明確要求下載完整的shared/[email protected],但由于某種原因它被拉進來。如果不存在,它似乎會解決問題。
查看完整描述

3 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

事實證明,問題在于其中一個依賴項引用了pre-go-modulesgithub.com/shared/dependency/api的版本。

這導致 go 工具具有對子模塊的模塊引用,而且還對go-modules 之前版本的github.com/shared/dependency/api整個存儲庫進行黑盒導入。github.com/shared/dependency在這個例子中,這意味著v1.2.0有 go 模塊(有一個go.mod文件),而v1.1.0沒有。

將以下行添加到我的go.mod文件中能夠解決該問題,并且該解決方案適用于我遇到此類沖突的多個依賴項:

replace (
    github.com/shared/dependency => github.com/shared/dependency v1.2.0
    )

請注意,此解決方案僅有效,因為我們強制對共享依賴項的引用使用支持 go-module 的版本(v1.2.0+)。


查看完整回答
反對 回復 2023-07-26
?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

為了以后參考,我發現當上面的方法不起作用時,運行go get github.com/shared/dependency/api@latest似乎起作用了。replace

據推測,這會強制兩個模塊使用最新版本的依賴項。


查看完整回答
反對 回復 2023-07-26
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

我簡單地解決了以下問題ambiguous import: found package time in multiple modules:

只需刪除mod文件,然后使用與任何現有模塊名稱匹配的go mod init不同名稱再次刪除即可。

go mod init time-不正確

go mod init gotime-正確


查看完整回答
反對 回復 2023-07-26
  • 3 回答
  • 0 關注
  • 278 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號