| 776 | } |
| 777 | |
| 778 | func (fs serveCommandFileSystem) serveSourceTree(xctx gbuild.XContext, reqPath string) (http.File, error) { |
| 779 | parts := strings.Split(path.Clean(reqPath), "/") |
| 780 | // Under Go Modules different packages can be located in different module |
| 781 | // directories, which no longer align with import paths. |
| 782 | // |
| 783 | // We don't know which part of the requested path is package import path and |
| 784 | // which is a path under the package directory, so we try different split |
| 785 | // points until the package is found successfully. |
| 786 | for i := len(parts); i > 0; i-- { |
| 787 | pkgPath := path.Clean(path.Join(parts[:i]...)) |
| 788 | filePath := path.Clean(path.Join(parts[i:]...)) |
| 789 | if pkg, err := xctx.Import(pkgPath, ".", build.FindOnly); err == nil { |
| 790 | return http.Dir(pkg.Dir).Open(filePath) |
| 791 | } |
| 792 | } |
| 793 | return nil, os.ErrNotExist |
| 794 | } |
| 795 | |
| 796 | type fakeFile struct { |
| 797 | name string |