internal/pkgdoc: fix synopsis pkg name == dir name matching

file.Name.Name should match the directory name not the filename.

In CL 296378, newDirTree's name parameter was removed and synopsis
matched against the filename of the source code, e.g. "*.go".

Fixes golang/go#45042
Fixes golang/go#45614

Change-Id: Ifa4c8a70429e277efb1f9f726de9249509cbcee0
Reviewed-on: https://go-review.googlesource.com/c/website/+/311089
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Cherry Zhang <cherryyz@google.com>
diff --git a/internal/pkgdoc/dir.go b/internal/pkgdoc/dir.go
index 6a10baf..e8b2c61 100644
--- a/internal/pkgdoc/dir.go
+++ b/internal/pkgdoc/dir.go
@@ -143,8 +143,7 @@
 	var dirs []*Dir
 
 	for _, d := range list {
-		name := d.Name()
-		filename := path.Join(abspath, name)
+		filename := path.Join(abspath, d.Name())
 		switch {
 		case isPkgDir(d):
 			dir := newDir(fsys, fset, filename)
@@ -169,7 +168,7 @@
 				// prioritize documentation
 				i := -1
 				switch file.Name.Name {
-				case name:
+				case path.Base(abspath):
 					i = 0 // normal case: directory name matches package name
 				case "main":
 					i = 1 // directory contains a main package
diff --git a/internal/pkgdoc/dir_test.go b/internal/pkgdoc/dir_test.go
index b7b56d5..9441274 100644
--- a/internal/pkgdoc/dir_test.go
+++ b/internal/pkgdoc/dir_test.go
@@ -13,6 +13,7 @@
 	"runtime"
 	"sort"
 	"testing"
+	"testing/fstest"
 )
 
 func TestNewDirTree(t *testing.T) {
@@ -33,6 +34,24 @@
 	}
 }
 
+func TestIssue45614(t *testing.T) {
+	fs := fstest.MapFS{
+		"src/index/suffixarray/gen.go": {
+			Data: []byte(`// P1: directory contains a main package
+package main
+`)},
+		"src/index/suffixarray/suffixarray.go": {
+			Data: []byte(`// P0: directory name matches package name
+package suffixarray
+`)},
+	}
+
+	dir := newDir(fs, token.NewFileSet(), "src/index/suffixarray")
+	if got, want := dir.Synopsis, "P0: directory name matches package name"; got != want {
+		t.Errorf("dir.Synopsis = %q; want %q", got, want)
+	}
+}
+
 func BenchmarkNewDirectory(b *testing.B) {
 	if testing.Short() {
 		b.Skip("not running tests requiring large file scan in short mode")