Skip to content

Commit

Permalink
desc/protoparse: fix recently identified regressions from v1.14 (#594)
Browse files Browse the repository at this point in the history
* ParseFilesButDoNotLink should not use ImportPaths field
* remove constraint that files explicitly named by caller must be provided as source instead of as descriptors
  • Loading branch information
jhump authored Jan 22, 2024
1 parent 7000dd2 commit e6d8c42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 1 addition & 7 deletions desc/protoparse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions desc/protoparse/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit e6d8c42

Please sign in to comment.