@@ -1028,7 +1028,43 @@ func (di *spdxDefaultImplementation) PackageFromDirectory(opts *Options, dirPath
10281028
10291029 pkg = NewPackage ()
10301030 pkg .FilesAnalyzed = true
1031+ // Default package name is the directory base name. If this directory
1032+ // (or one of its top-level children created during extraction) contains
1033+ // a go.mod, prefer the module path defined there so the root package
1034+ // reflects the module import path instead of a temp dirname.
10311035 pkg .Name = filepath .Base (dirPath )
1036+
1037+ // Look for a go.mod either at the root dirPath or in the scanned file
1038+ // tree (which may contain a nested module after extraction). Prefer an
1039+ // explicit go.mod at dirPath if present, otherwise use the first
1040+ // occurrence from fileList.
1041+ goModDir := ""
1042+ if helpers .Exists (filepath .Join (dirPath , GoModFileName )) {
1043+ goModDir = dirPath
1044+ } else {
1045+ for _ , p := range fileList {
1046+ if filepath .Base (p ) == GoModFileName {
1047+ // p is a relative path; derive the directory containing go.mod
1048+ goModDir = filepath .Join (dirPath , filepath .Dir (p ))
1049+ break
1050+ }
1051+ }
1052+ }
1053+
1054+ if goModDir != "" {
1055+ // Try to read the module path from go.mod without doing full module
1056+ // processing. NewGoModuleFromPath provides a GoModule with the
1057+ // GoModDefaultImpl which implements OpenModule for parsing only.
1058+ if gm , err := NewGoModuleFromPath (goModDir ); err == nil {
1059+ if gomod , err := gm .impl .OpenModule (gm .Options ()); err == nil {
1060+ if gomod != nil && gomod .Module != nil && gomod .Module .Mod .Path != "" {
1061+ pkg .Name = gomod .Module .Mod .Path
1062+ }
1063+ } else {
1064+ logrus .Debugf ("unable to parse go.mod for %s: %v" , goModDir , err )
1065+ }
1066+ }
1067+ }
10321068 if pkg .Name == "" {
10331069 pkg .Name = uuid .NewString ()
10341070 }
0 commit comments