Skip to content

Commit 30aa313

Browse files
committed
specify module's disk path to NewPkg
1 parent 9efcc15 commit 30aa313

2 files changed

Lines changed: 8 additions & 9 deletions

File tree

src/go/cmd/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func NewModule(dir string) (*Module, error) {
8989
return filepath.SkipDir
9090
}
9191
}
92-
p, err := NewPkg(path, m.ModFile.Module.Mod.Path)
92+
p, err := NewPkg(path, m.ModFile.Module.Mod.Path, dir)
9393
if err == nil {
9494
m.Packages[baseImportPath+p.Name()] = p
9595
} else if !errors.Is(err, ErrNoPackages) {

src/go/cmd/pkg.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ type Pkg struct {
5151
}
5252

5353
// NewPkg loads the package in the specified directory.
54-
// It's required there is only one package in the directory.
55-
func NewPkg(dir, modulePath string) (*Pkg, error) {
54+
//
55+
// - dir is the directory containing the package
56+
// - modulePath is the import path of the module containing the package
57+
// - moduleRoot is the root directory of the module on disk (used to compute the package's relative name e.g. "azcore/log")
58+
func NewPkg(dir, modulePath, moduleRoot string) (*Pkg, error) {
5659
pk := &Pkg{
5760
modulePath: modulePath,
5861
c: newContent(),
@@ -61,12 +64,8 @@ func NewPkg(dir, modulePath string) (*Pkg, error) {
6164
}
6265
modulePathWithoutVersion := strings.TrimSuffix(versionReg.ReplaceAllString(modulePath, "/"), "/")
6366
moduleName := filepath.Base(modulePathWithoutVersion)
64-
if _, after, found := strings.Cut(dir, moduleName); found {
65-
pk.relName = moduleName
66-
if after != "" && after[0] != '@' {
67-
pk.relName += after
68-
}
69-
pk.relName = strings.ReplaceAll(pk.relName, "\\", "/")
67+
if _, after, found := strings.Cut(dir, moduleRoot); found {
68+
pk.relName = strings.ReplaceAll(moduleName+after, "\\", "/")
7069
} else {
7170
return nil, errors.New(dir + " isn't part of module " + moduleName)
7271
}

0 commit comments

Comments
 (0)