Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore "go command required" error #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions getcomments/parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"errors"
"fmt"
"go/ast"
"go/types"
Expand All @@ -9,13 +10,18 @@ import (
"golang.org/x/tools/go/packages"
)

var ErrGoCommandRequired = errors.New("go command required, not found")

func Get(packageName string) (m map[string]string, err error) {
config := &packages.Config{
Mode: packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax,
Tests: true,
}
pkgs, err := packages.Load(config, packageName)
if err != nil {
if strings.HasPrefix(err.Error(), "err: go command required, not found: ") {
err = ErrGoCommandRequired
}
err = fmt.Errorf("error loading package %s: %w", packageName, err)
return
}
Expand Down
12 changes: 11 additions & 1 deletion getcomments/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package parser_test

import (
"encoding/json"
"errors"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/a-h/rest/getcomments/parser"
"github.com/a-h/rest/getcomments/parser/tests/anonymous"
"github.com/a-h/rest/getcomments/parser/tests/chans"
Expand All @@ -14,7 +17,6 @@ import (
"github.com/a-h/rest/getcomments/parser/tests/pointers"
"github.com/a-h/rest/getcomments/parser/tests/privatetypes"
"github.com/a-h/rest/getcomments/parser/tests/publictypes"
"github.com/google/go-cmp/cmp"
)

func TestGet(t *testing.T) {
Expand Down Expand Up @@ -90,3 +92,11 @@ func TestGet(t *testing.T) {
})
}
}

func TestGet_WithoutToolchain(t *testing.T) {
t.Setenv("PATH", "")
_, err := parser.Get("github.com/a-h/rest/getcomments/parser/tests/docs")
if !errors.Is(err, parser.ErrGoCommandRequired) {
t.Fatalf("expected ErrGoCommandRequired, got %v", err)
}
}
4 changes: 4 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rest

import (
"errors"
"fmt"
"reflect"
"slices"
Expand Down Expand Up @@ -394,6 +395,9 @@ func (api *API) getCommentsForPackage(pkg string) (pkgComments map[string]string
return pkgComments, nil
}
pkgComments, err = parser.Get(pkg)
if errors.Is(err, parser.ErrGoCommandRequired) {
err = nil
}
if err != nil {
return
}
Expand Down