From c18c2d8c3c60aa3e5e4f5f75641f0db60b30c963 Mon Sep 17 00:00:00 2001 From: caixw Date: Sat, 18 May 2024 11:25:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=20PkgSourceDir=20?= =?UTF-8?q?=E5=A4=84=E7=90=86=20replace=20=E6=8C=87=E5=90=91=E9=9D=9E?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=8C=85=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mod.go | 23 ++++++++--------------- mod_test.go | 2 +- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/mod.go b/mod.go index e1952e7..525d6a9 100644 --- a/mod.go +++ b/mod.go @@ -25,7 +25,7 @@ var ( stdSource = filepath.Join(build.Default.GOROOT, "src") ) -// 查找包 pkgPath 的源码目录 +// PkgSourceDir 查找包 pkgPath 的源码目录 // // 如果 pkgPath 是标准库的名称,如 encoding/json 等,则返回当前使用的 Go 版本对应的标准库地址。 // 其它情况则从 modDir 指向的 go.mod 中查找 require 或是 replace 字段的定义, @@ -60,30 +60,23 @@ func PkgSourceDir(pkgPath, modDir string, replace bool) (dir string, err error) continue } - if !replace { - p, err := escapePath(pkg.Mod.Path, pkg.Mod.Version, suffix) - if err != nil { - println("1:", err.Error()) - return "", err - } - return filepath.Join(pkgSource, p), nil - } - index := slices.IndexFunc(mod.Replace, func(r *modfile.Replace) bool { return r.Old.Path == pkg.Mod.Path }) - if index < 0 { + if !replace || index < 0 { p, err := escapePath(pkg.Mod.Path, pkg.Mod.Version, suffix) if err != nil { - println("2:", err.Error()) return "", err } return filepath.Join(pkgSource, p), nil } p := mod.Replace[index].New.Path - if !filepath.IsAbs(p) { - p = filepath.Join(modDir, p) + if p != "" && (p[0] == '.' || p[0] == '/') { // 指向本地 + if !filepath.IsAbs(p) { + p = filepath.Join(modDir, p) + } + return filepath.Abs(p) } - return filepath.Abs(p) + return PkgSourceDir(p, modDir, false) } return "", fs.ErrNotExist diff --git a/mod_test.go b/mod_test.go index acdca16..1fbe64d 100644 --- a/mod_test.go +++ b/mod_test.go @@ -35,7 +35,7 @@ func TestPkgSourceDir(t *testing.T) { True(strings.HasSuffix(filepath.ToSlash(dir), "assert/v4@v4.3.0/rest")) dir, err = PkgSourceDir("github.com/issue9/assertxx", "./", false) - a.ErrorIs(err, fs.ErrNotExist) + a.ErrorIs(err, fs.ErrNotExist).Empty(dir) // replace