From 4c2f8dd452dcd8dcabe7d9dcd51a43b10408ee1d Mon Sep 17 00:00:00 2001 From: Evan Goode Date: Sun, 21 Apr 2024 16:20:45 -0400 Subject: [PATCH] Handle case of empty GOROOT (#1798) In some situations, such as when using the go-swag Nix package, runtime.GOROOT() will be empty, and RangeFiles will skip all source paths since technically, all paths are prefixed with the empty string. See also https://github.com/NixOS/nixpkgs/issues/224701 May resolve some cases of https://github.com/swaggo/swag/issues/1622. --- packages.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages.go b/packages.go index 69a1b0529..466db8629 100644 --- a/packages.go +++ b/packages.go @@ -93,7 +93,7 @@ func (pkgDefs *PackagesDefinitions) RangeFiles(handle func(info *AstFileInfo) er for _, info := range pkgDefs.files { // ignore package path prefix with 'vendor' or $GOROOT, // because the router info of api will not be included these files. - if strings.HasPrefix(info.PackagePath, "vendor") || strings.HasPrefix(info.Path, runtime.GOROOT()) { + if strings.HasPrefix(info.PackagePath, "vendor") || (runtime.GOROOT() != "" && strings.HasPrefix(info.Path, runtime.GOROOT())) { continue } sortedFiles = append(sortedFiles, info)