From e6d8c42680046722a500797e05baa094ff4f468d Mon Sep 17 00:00:00 2001 From: Joshua Humphries <2035234+jhump@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:32:03 -0500 Subject: [PATCH] desc/protoparse: fix recently identified regressions from v1.14 (#594) * ParseFilesButDoNotLink should not use ImportPaths field * remove constraint that files explicitly named by caller must be provided as source instead of as descriptors --- desc/protoparse/parser.go | 8 +------- desc/protoparse/parser_test.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/desc/protoparse/parser.go b/desc/protoparse/parser.go index 6a02bf15..0ab9d1b8 100644 --- a/desc/protoparse/parser.go +++ b/desc/protoparse/parser.go @@ -241,6 +241,7 @@ func (r noCloneParseResult) Clone() parser.Result { // ErrorReporter always returns nil, the parse fails with ErrInvalidSource. func (p Parser) ParseFilesButDoNotLink(filenames ...string) ([]*descriptorpb.FileDescriptorProto, error) { rep := newReporter(p.ErrorReporter, p.WarningReporter) + p.ImportPaths = nil // not used for this "do not link" operation. res, _ := p.getResolver(filenames) results, err := parseToProtos(res, filenames, reporter.NewHandler(rep), p.ValidateUnlinkedFiles) if err != nil { @@ -555,16 +556,9 @@ func (p Parser) getResolver(filenames []string) (protocompile.Resolver, *ast2.So })) } backupResolver := protocompile.WithStandardImports(importResolver) - mustBeSource := make(map[string]struct{}, len(filenames)) - for _, name := range filenames { - mustBeSource[name] = struct{}{} - } return protocompile.CompositeResolver{ sourceResolver, protocompile.ResolverFunc(func(path string) (protocompile.SearchResult, error) { - if _, ok := mustBeSource[path]; ok { - return protocompile.SearchResult{}, os.ErrNotExist - } return backupResolver.FindFileByPath(path) }), }, &srcSpan diff --git a/desc/protoparse/parser_test.go b/desc/protoparse/parser_test.go index 15e1eb73..3af1772a 100644 --- a/desc/protoparse/parser_test.go +++ b/desc/protoparse/parser_test.go @@ -525,3 +525,22 @@ func TestParseInferImportPaths_FixesNestedPaths(t *testing.T) { }) } } + +func TestParseFilesButDoNotLink_DoesNotUseImportPaths(t *testing.T) { + tempdir, err := os.MkdirTemp("", "protoparse") + testutil.Ok(t, err) + defer func() { + _ = os.RemoveAll(tempdir) + }() + err = os.WriteFile(filepath.Join(tempdir, "extra.proto"), []byte("package extra;"), 0644) + testutil.Ok(t, err) + mainPath := filepath.Join(tempdir, "main.proto") + err = os.WriteFile(mainPath, []byte("package main; import \"extra.proto\";"), 0644) + testutil.Ok(t, err) + p := Parser{ + ImportPaths: []string{tempdir}, + } + fds, err := p.ParseFilesButDoNotLink(mainPath) + testutil.Ok(t, err) + testutil.Eq(t, 1, len(fds)) +}