Skip to content

Commit 7ec101d

Browse files
gabriel-vasilejba
authored andcommitted
internal/localdatasource: prevent panic for std modules
Previously when using the frontend with the local file system data source, a panic would occur when trying to view docs for stdlib modules. Now the panic is replaced by returning error 'not found' for missing stdlib modules. For golang/go#44704 Change-Id: I584e1663823c494d090237f80878a679ce8ce82f Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/297829 Reviewed-by: Jonathan Amsterdam <[email protected]> Run-TryBot: Julie Qiu <[email protected]> Trust: Julie Qiu <[email protected]>
1 parent c59598d commit 7ec101d

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

internal/localdatasource/datasource.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func getFullPath(modulePath string) string {
9898
}
9999

100100
// GetUnit returns information about a unit. Both the module path and package
101-
// path must both be known.
101+
// path must be known.
102102
func (ds *DataSource) GetUnit(ctx context.Context, pathInfo *internal.UnitMeta, fields internal.FieldSet) (_ *internal.Unit, err error) {
103103
defer derrors.Wrap(&err, "GetUnit(%q, %q)", pathInfo.Path, pathInfo.ModulePath)
104104

@@ -135,6 +135,9 @@ func (ds *DataSource) GetUnitMeta(ctx context.Context, path, requestedModulePath
135135
ds.mu.Lock()
136136
module := ds.loadedModules[requestedModulePath]
137137
ds.mu.Unlock()
138+
if module == nil {
139+
return nil, fmt.Errorf("%s not loaded: %w", requestedModulePath, derrors.NotFound)
140+
}
138141

139142
um := &internal.UnitMeta{
140143
Path: path,

internal/localdatasource/datasource_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"golang.org/x/pkgsite/internal/fetch"
1919
"golang.org/x/pkgsite/internal/godoc/dochtml"
2020
"golang.org/x/pkgsite/internal/licenses"
21+
"golang.org/x/pkgsite/internal/stdlib"
2122
"golang.org/x/pkgsite/internal/testing/testhelper"
2223
)
2324

@@ -175,6 +176,11 @@ func TestGetUnitMeta(t *testing.T) {
175176
modulePath: internal.UnknownModulePath,
176177
wantErr: derrors.NotFound,
177178
},
179+
{
180+
path: "net/http",
181+
modulePath: stdlib.ModulePath,
182+
wantErr: derrors.NotFound,
183+
},
178184
} {
179185
t.Run(test.path, func(t *testing.T) {
180186
got, err := ds.GetUnitMeta(ctx, test.path, test.modulePath, fetch.LocalVersion)

0 commit comments

Comments
 (0)