Skip to content

Commit

Permalink
fix: 修正 PkgSourceDir 处理 replace 指向非本地包的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed May 18, 2024
1 parent c4a8d7c commit c18c2d8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
23 changes: 8 additions & 15 deletions mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
stdSource = filepath.Join(build.Default.GOROOT, "src")
)

// 查找包 pkgPath 的源码目录
// PkgSourceDir 查找包 pkgPath 的源码目录
//
// 如果 pkgPath 是标准库的名称,如 encoding/json 等,则返回当前使用的 Go 版本对应的标准库地址。
// 其它情况则从 modDir 指向的 go.mod 中查找 require 或是 replace 字段的定义,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestPkgSourceDir(t *testing.T) {
True(strings.HasSuffix(filepath.ToSlash(dir), "assert/[email protected]/rest"))

dir, err = PkgSourceDir("github.com/issue9/assertxx", "./", false)
a.ErrorIs(err, fs.ErrNotExist)
a.ErrorIs(err, fs.ErrNotExist).Empty(dir)

// replace

Expand Down

0 comments on commit c18c2d8

Please sign in to comment.