Skip to content
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/PuerkitoBio/goquery v1.9.2
github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5
github.com/andybalholm/brotli v1.2.0
github.com/bufbuild/protocompile v0.14.1
github.com/chromedp/cdproto v0.0.0-20250509201441-70372ae9ef75
github.com/evanw/esbuild v0.25.9
github.com/fatih/color v1.18.0
Expand Down Expand Up @@ -73,7 +74,6 @@ require (
buf.build/gen/go/gogo/protobuf/protocolbuffers/go v1.36.5-20210810001428-4df00b267f94.1 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bufbuild/protocompile v0.14.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chromedp/sysutil v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
35 changes: 20 additions & 15 deletions internal/js/modules/k6/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import (
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"

"github.com/bufbuild/protocompile"
"github.com/grafana/sobek"
"github.com/jhump/protoreflect/desc" //nolint:staticcheck // FIXME: #4035
"github.com/jhump/protoreflect/desc/protoparse" //nolint:staticcheck // FIXME: #4035
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -65,22 +64,24 @@ func (c *Client) Load(importPaths []string, filenames ...string) ([]MethodInfo,
importPaths[i] = strings.TrimPrefix(s, "file://")
}

parser := protoparse.Parser{
ImportPaths: importPaths,
InferImportPaths: false,
Accessor: protoparse.FileAccessor(func(filename string) (io.ReadCloser, error) {
resolver := protocompile.WithStandardImports(&protocompile.SourceResolver{
ImportPaths: importPaths,
Accessor: func(filename string) (io.ReadCloser, error) {
absFilePath := initEnv.GetAbsFilePath(filename)
return initEnv.FileSystems["file"].Open(absFilePath)
}),
},
})

compiler := protocompile.Compiler{
Resolver: resolver,
}

fds, err := parser.ParseFiles(filenames...)
fds, err := compiler.Compile(c.vu.Context(), filenames...)
if err != nil {
return nil, err
}

fdset := &descriptorpb.FileDescriptorSet{}

seen := make(map[string]struct{})
for _, fd := range fds {
fdset.File = append(fdset.File, walkFileDescriptors(seen, fd)...)
Expand Down Expand Up @@ -571,20 +572,24 @@ func (c *Client) convertToMethodInfo(fdset *descriptorpb.FileDescriptorSet) ([]M
return rtn, nil
}

func walkFileDescriptors(seen map[string]struct{}, fd *desc.FileDescriptor) []*descriptorpb.FileDescriptorProto {
// walkFileDescriptors recursively walks through a file descriptor and its dependencies,
// converting them to FileDescriptorProto.
// This ensures that all dependencies are included in the FileDescriptorSet.
func walkFileDescriptors(seen map[string]struct{}, fd protoreflect.FileDescriptor) []*descriptorpb.FileDescriptorProto {
fds := []*descriptorpb.FileDescriptorProto{}

if _, ok := seen[fd.GetName()]; ok {
if _, ok := seen[fd.Path()]; ok {
return fds
}
seen[fd.GetName()] = struct{}{}
fds = append(fds, fd.AsFileDescriptorProto())
seen[fd.Path()] = struct{}{}
fds = append(fds, protodesc.ToFileDescriptorProto(fd))

for _, dep := range fd.GetDependencies() {
imports := fd.Imports()
for i := 0; i < imports.Len(); i++ {
dep := imports.Get(i).FileDescriptor
deps := walkFileDescriptors(seen, dep)
fds = append(fds, deps...)
}

return fds
}

Expand Down
23 changes: 13 additions & 10 deletions internal/lib/netext/grpcext/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
"io"
"testing"

"github.com/jhump/protoreflect/desc/protoparse" //nolint:staticcheck // FIXME: #4035
"github.com/bufbuild/protocompile"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/dynamicpb"

Expand Down Expand Up @@ -252,9 +251,9 @@ func TestConnInvokeInvalid(t *testing.T) {

func methodFromProto(method string) protoreflect.MethodDescriptor {
path := "any-path"
parser := protoparse.Parser{
InferImportPaths: false,
Accessor: protoparse.FileAccessor(func(filename string) (io.ReadCloser, error) {

resolver := &protocompile.SourceResolver{
Accessor: func(filename string) (io.ReadCloser, error) {
// a small hack to make sure we are parsing the right file
// otherwise the parser will try to parse "google/protobuf/descriptor.proto"
// with exactly the same name as the one we are trying to parse for testing
Expand Down Expand Up @@ -286,19 +285,23 @@ message HelloResponse {
message Empty {

}`

return io.NopCloser(bytes.NewBufferString(b)), nil
}),
},
}

fds, err := parser.ParseFiles(path)
if err != nil {
panic(err)
compiler := protocompile.Compiler{
Resolver: resolver,
}

fd, err := protodesc.NewFile(fds[0].AsFileDescriptorProto(), nil)
fds, err := compiler.Compile(context.Background(), path)
if err != nil {
panic(err)
}
if len(fds) != 1 {
panic("expected exactly one file descriptor")
}
fd := fds[0]

services := fd.Services()
if services.Len() == 0 {
Expand Down
Loading