@@ -38,7 +38,7 @@ type ModuleResolver struct {
38
38
mains []* gocommand.ModuleJSON
39
39
mainByDir map [string ]* gocommand.ModuleJSON
40
40
modsByModPath []* gocommand.ModuleJSON // All modules, ordered by # of path components in module Path...
41
- modsByDir []* gocommand.ModuleJSON // ...or Dir.
41
+ modsByDir []* gocommand.ModuleJSON // ...or number of path components in their Dir.
42
42
43
43
// moduleCacheCache stores information about the module cache.
44
44
moduleCacheCache * dirInfoCache
@@ -124,7 +124,7 @@ func (r *ModuleResolver) init() error {
124
124
})
125
125
sort .Slice (r .modsByDir , func (i , j int ) bool {
126
126
count := func (x int ) int {
127
- return strings .Count (r .modsByDir [x ].Dir , "/" )
127
+ return strings .Count (r .modsByDir [x ].Dir , string ( filepath . Separator ) )
128
128
}
129
129
return count (j ) < count (i ) // descending order
130
130
})
@@ -328,6 +328,10 @@ func (r *ModuleResolver) findModuleByDir(dir string) *gocommand.ModuleJSON {
328
328
// - in /vendor/ in -mod=vendor mode.
329
329
// - nested module? Dunno.
330
330
// Rumor has it that replace targets cannot contain other replace targets.
331
+ //
332
+ // Note that it is critical here that modsByDir is sorted to have deeper dirs
333
+ // first. This ensures that findModuleByDir finds the innermost module.
334
+ // See also golang/go#56291.
331
335
for _ , m := range r .modsByDir {
332
336
if ! strings .HasPrefix (dir , m .Dir ) {
333
337
continue
0 commit comments