aboutsummaryrefslogtreecommitdiff
path: root/internal/pkgdoc
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2021-02-26 01:53:00 -0500
committerRuss Cox <rsc@golang.org>2021-06-14 17:41:38 +0000
commit5ee419f2eadb4e9213f52ffc1f6fea62bb55fbb7 (patch)
tree16a235ec5ef361c53b1ca58034adc77a119882a9 /internal/pkgdoc
parent4b5486d0e5c44f3e96aea9d5d8a00185872c416f (diff)
downloadgo-x-website-5ee419f2eadb4e9213f52ffc1f6fea62bb55fbb7.tar.xz
all: remove toFS usage
The toFS calls were a stop-gap to convert from old code that wasn't strict about path forms to the io/fs routines that are more strict. Arrange to pass io/fs-compatible paths everywhere and remove toFS. Change-Id: Id69c0f23074ebd3a6dfef2255b2f8185ad1d1249 Reviewed-on: https://go-review.googlesource.com/c/website/+/317659 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Diffstat (limited to 'internal/pkgdoc')
-rw-r--r--internal/pkgdoc/dir.go45
-rw-r--r--internal/pkgdoc/dir_test.go4
-rw-r--r--internal/pkgdoc/doc.go29
-rw-r--r--internal/pkgdoc/doc_test.go4
4 files changed, 36 insertions, 46 deletions
diff --git a/internal/pkgdoc/dir.go b/internal/pkgdoc/dir.go
index e8b2c618..fe995e2f 100644
--- a/internal/pkgdoc/dir.go
+++ b/internal/pkgdoc/dir.go
@@ -22,14 +22,6 @@ import (
"strings"
)
-// toFS returns the io/fs name for path (no leading slash).
-func toFS(name string) string {
- if name == "/" {
- return "."
- }
- return path.Clean(strings.TrimPrefix(name, "/"))
-}
-
type Dir struct {
Path string // directory path
HasPkg bool // true if the directory contains at least one package
@@ -65,17 +57,15 @@ func (dir *Dir) Lookup(name string) *Dir {
if name == dir.Path {
return dir
}
- dirPathLen := len(dir.Path)
- if dir.Path == "/" {
- dirPathLen = 0 // so path[dirPathLen] is a slash
- }
- if !strings.HasPrefix(name, dir.Path) || name[dirPathLen] != '/' {
- println("NO", name, dir.Path)
- return nil
+ if dir.Path != "." {
+ if !strings.HasPrefix(name, dir.Path) || name[len(dir.Path)] != '/' {
+ return nil
+ }
+ name = name[len(dir.Path)+1:]
}
d := dir
Walk:
- for i := dirPathLen + 1; i <= len(name); i++ {
+ for i := 0; i <= len(name); i++ {
if i == len(name) || name[i] == '/' {
// Find next child along path.
for _, sub := range d.Dirs {
@@ -84,7 +74,6 @@ Walk:
continue Walk
}
}
- println("LOST", name[:i])
return nil
}
}
@@ -126,16 +115,16 @@ func (dir *Dir) List(filter func(string) bool) *DirList {
return &DirList{list}
}
-func newDir(fsys fs.FS, fset *token.FileSet, abspath string) *Dir {
+func newDir(fsys fs.FS, fset *token.FileSet, dirpath string) *Dir {
var synopses [3]string // prioritized package documentation (0 == highest priority)
hasPkgFiles := false
haveSummary := false
- list, err := fs.ReadDir(fsys, toFS(abspath))
+ list, err := fs.ReadDir(fsys, dirpath)
if err != nil {
// TODO: propagate more. See golang.org/issue/14252.
- log.Printf("newDirTree reading %s: %v", abspath, err)
+ log.Printf("newDirTree reading %s: %v", dirpath, err)
}
// determine number of subdirectories and if there are package files
@@ -143,7 +132,7 @@ func newDir(fsys fs.FS, fset *token.FileSet, abspath string) *Dir {
var dirs []*Dir
for _, d := range list {
- filename := path.Join(abspath, d.Name())
+ filename := path.Join(dirpath, d.Name())
switch {
case isPkgDir(d):
dir := newDir(fsys, fset, filename)
@@ -168,7 +157,7 @@ func newDir(fsys fs.FS, fset *token.FileSet, abspath string) *Dir {
// prioritize documentation
i := -1
switch file.Name.Name {
- case path.Base(abspath):
+ case path.Base(dirpath):
i = 0 // normal case: directory name matches package name
case "main":
i = 1 // directory contains a main package
@@ -211,7 +200,7 @@ func newDir(fsys fs.FS, fset *token.FileSet, abspath string) *Dir {
}
return &Dir{
- Path: abspath,
+ Path: dirpath,
HasPkg: hasPkgFiles,
Synopsis: synopsis,
Dirs: dirs,
@@ -247,7 +236,7 @@ func walkDirs(f func(d *Dir, depth int), d *Dir, depth int) {
}
func parseFile(fsys fs.FS, fset *token.FileSet, filename string, mode parser.Mode) (*ast.File, error) {
- src, err := fs.ReadFile(fsys, toFS(filename))
+ src, err := fs.ReadFile(fsys, filename)
if err != nil {
return nil, err
}
@@ -259,15 +248,15 @@ func parseFile(fsys fs.FS, fset *token.FileSet, filename string, mode parser.Mod
return parser.ParseFile(fset, filename, src, mode)
}
-func parseFiles(fsys fs.FS, fset *token.FileSet, relpath string, abspath string, localnames []string) (map[string]*ast.File, error) {
+func parseFiles(fsys fs.FS, fset *token.FileSet, dirname string, localnames []string) (map[string]*ast.File, error) {
files := make(map[string]*ast.File)
for _, f := range localnames {
- absname := path.Join(abspath, f)
- file, err := parseFile(fsys, fset, absname, parser.ParseComments)
+ filename := path.Join(dirname, f)
+ file, err := parseFile(fsys, fset, filename, parser.ParseComments)
if err != nil {
return nil, err
}
- files[path.Join(relpath, f)] = file
+ files[filename] = file
}
return files, nil
diff --git a/internal/pkgdoc/dir_test.go b/internal/pkgdoc/dir_test.go
index 94412746..db1c7821 100644
--- a/internal/pkgdoc/dir_test.go
+++ b/internal/pkgdoc/dir_test.go
@@ -17,7 +17,7 @@ import (
)
func TestNewDirTree(t *testing.T) {
- dir := newDir(os.DirFS(runtime.GOROOT()), token.NewFileSet(), "/src")
+ dir := newDir(os.DirFS(runtime.GOROOT()), token.NewFileSet(), "src")
processDir(t, dir)
}
@@ -62,6 +62,6 @@ func BenchmarkNewDirectory(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for tries := 0; tries < b.N; tries++ {
- newDir(fs, token.NewFileSet(), "/src")
+ newDir(fs, token.NewFileSet(), "src")
}
}
diff --git a/internal/pkgdoc/doc.go b/internal/pkgdoc/doc.go
index 02e7b5b3..218228ea 100644
--- a/internal/pkgdoc/doc.go
+++ b/internal/pkgdoc/doc.go
@@ -32,9 +32,9 @@ type Docs struct {
}
func NewDocs(fsys fs.FS) *Docs {
- src := newDir(fsys, token.NewFileSet(), "/src")
+ src := newDir(fsys, token.NewFileSet(), "src")
root := &Dir{
- Path: "/",
+ Path: ".",
Dirs: []*Dir{src},
}
return &Docs{
@@ -112,14 +112,15 @@ func ParseMode(text string) Mode {
return mode
}
-// Doc returns the Page for a package directory abspath.
+// Doc returns the Page for a package directory dir.
// Package documentation (Page.PDoc) is extracted from the AST.
// If there is no corresponding package in the
// directory, Page.PDoc is nil. If there are no sub-
// directories, Page.Dirs is nil. If an error occurred, PageInfo.Err is
// set to the respective error but the error is not logged.
-func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page {
- info := &Page{Dirname: abspath, Mode: mode}
+func Doc(d *Docs, dir string, mode Mode, goos, goarch string) *Page {
+ dir = path.Clean(dir)
+ info := &Page{Dirname: dir, Mode: mode}
// Restrict to the package files that would be used when building
// the package on this system. This makes sure that if there are
@@ -130,11 +131,11 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page
ctxt := build.Default
ctxt.IsAbsPath = path.IsAbs
ctxt.IsDir = func(path string) bool {
- fi, err := fs.Stat(d.fs, toFS(filepath.ToSlash(path)))
+ fi, err := fs.Stat(d.fs, filepath.ToSlash(path))
return err == nil && fi.IsDir()
}
ctxt.ReadDir = func(dir string) ([]os.FileInfo, error) {
- f, err := fs.ReadDir(d.fs, toFS(filepath.ToSlash(dir)))
+ f, err := fs.ReadDir(d.fs, filepath.ToSlash(dir))
filtered := make([]os.FileInfo, 0, len(f))
for _, i := range f {
if mode&ModeAll != 0 || i.Name() != "internal" {
@@ -147,7 +148,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page
return filtered, err
}
ctxt.OpenFile = func(name string) (r io.ReadCloser, err error) {
- data, err := fs.ReadFile(d.fs, toFS(filepath.ToSlash(name)))
+ data, err := fs.ReadFile(d.fs, filepath.ToSlash(name))
if err != nil {
return nil, err
}
@@ -159,7 +160,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page
// linux/amd64 means the wasm syscall/js package was blank.
// And you can't run godoc on js/wasm anyway, so host defaults
// don't make sense here.
- if goos == "" && goarch == "" && relpath == "syscall/js" {
+ if goos == "" && goarch == "" && dir == "syscall/js" {
goos, goarch = "js", "wasm"
}
if goos != "" {
@@ -169,7 +170,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page
ctxt.GOARCH = goarch
}
- pkginfo, err := ctxt.ImportDir(abspath, 0)
+ pkginfo, err := ctxt.ImportDir(dir, 0)
// continue if there are no Go source files; we still want the directory info
if _, nogo := err.(*build.NoGoError); err != nil && !nogo {
info.Err = err
@@ -193,7 +194,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page
if len(pkgfiles) > 0 {
// build package AST
fset := token.NewFileSet()
- files, err := parseFiles(d.fs, fset, relpath, abspath, pkgfiles)
+ files, err := parseFiles(d.fs, fset, dir, pkgfiles)
if err != nil {
info.Err = err
return info
@@ -213,7 +214,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page
if mode&ModeMethods != 0 {
m |= doc.AllMethods
}
- info.PDoc = doc.New(pkg, path.Clean(relpath), m) // no trailing '/' in importpath
+ info.PDoc = doc.New(pkg, strings.TrimPrefix(dir, "src/"), m)
if mode&ModeBuiltin != 0 {
for _, t := range info.PDoc.Types {
info.PDoc.Consts = append(info.PDoc.Consts, t.Consts...)
@@ -230,7 +231,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page
// collect examples
testfiles := append(pkginfo.TestGoFiles, pkginfo.XTestGoFiles...)
- files, err = parseFiles(d.fs, fset, relpath, abspath, testfiles)
+ files, err = parseFiles(d.fs, fset, dir, testfiles)
if err != nil {
log.Println("parsing examples:", err)
}
@@ -238,7 +239,7 @@ func Doc(d *Docs, abspath, relpath string, mode Mode, goos, goarch string) *Page
info.Bugs = info.PDoc.Notes["BUG"]
}
- info.Dirs = d.root.Lookup(abspath).List(func(path string) bool { return d.includePath(path, mode) })
+ info.Dirs = d.root.Lookup(dir).List(func(path string) bool { return d.includePath(path, mode) })
info.DirFlat = mode&ModeFlat != 0
return info
diff --git a/internal/pkgdoc/doc_test.go b/internal/pkgdoc/doc_test.go
index c82d042e..1ac4f0a3 100644
--- a/internal/pkgdoc/doc_test.go
+++ b/internal/pkgdoc/doc_test.go
@@ -25,7 +25,7 @@ func TestIgnoredGoFiles(t *testing.T) {
package main`)},
}
d := NewDocs(fs)
- pInfo := Doc(d, "/src/"+packagePath, packagePath, ModeAll, "linux", "amd64")
+ pInfo := Doc(d, "src/"+packagePath, ModeAll, "linux", "amd64")
if pInfo.PDoc == nil {
t.Error("pInfo.PDoc = nil; want non-nil.")
@@ -58,7 +58,7 @@ func F()
}
d := NewDocs(fs)
- pInfo := Doc(d, "/src/"+packagePath, packagePath, 0, "linux", "amd64")
+ pInfo := Doc(d, "src/"+packagePath, 0, "linux", "amd64")
if got, want := pInfo.PDoc.Funcs[0].Doc, "F doc //line 1 should appear\nline 2 should appear\n"; got != want {
t.Errorf("pInfo.PDoc.Funcs[0].Doc = %q; want %q", got, want)
}