Skip to content

Commit e6d8c42

Browse files
authored
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
1 parent 7000dd2 commit e6d8c42

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

desc/protoparse/parser.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ func (r noCloneParseResult) Clone() parser.Result {
241241
// ErrorReporter always returns nil, the parse fails with ErrInvalidSource.
242242
func (p Parser) ParseFilesButDoNotLink(filenames ...string) ([]*descriptorpb.FileDescriptorProto, error) {
243243
rep := newReporter(p.ErrorReporter, p.WarningReporter)
244+
p.ImportPaths = nil // not used for this "do not link" operation.
244245
res, _ := p.getResolver(filenames)
245246
results, err := parseToProtos(res, filenames, reporter.NewHandler(rep), p.ValidateUnlinkedFiles)
246247
if err != nil {
@@ -555,16 +556,9 @@ func (p Parser) getResolver(filenames []string) (protocompile.Resolver, *ast2.So
555556
}))
556557
}
557558
backupResolver := protocompile.WithStandardImports(importResolver)
558-
mustBeSource := make(map[string]struct{}, len(filenames))
559-
for _, name := range filenames {
560-
mustBeSource[name] = struct{}{}
561-
}
562559
return protocompile.CompositeResolver{
563560
sourceResolver,
564561
protocompile.ResolverFunc(func(path string) (protocompile.SearchResult, error) {
565-
if _, ok := mustBeSource[path]; ok {
566-
return protocompile.SearchResult{}, os.ErrNotExist
567-
}
568562
return backupResolver.FindFileByPath(path)
569563
}),
570564
}, &srcSpan

desc/protoparse/parser_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,22 @@ func TestParseInferImportPaths_FixesNestedPaths(t *testing.T) {
525525
})
526526
}
527527
}
528+
529+
func TestParseFilesButDoNotLink_DoesNotUseImportPaths(t *testing.T) {
530+
tempdir, err := os.MkdirTemp("", "protoparse")
531+
testutil.Ok(t, err)
532+
defer func() {
533+
_ = os.RemoveAll(tempdir)
534+
}()
535+
err = os.WriteFile(filepath.Join(tempdir, "extra.proto"), []byte("package extra;"), 0644)
536+
testutil.Ok(t, err)
537+
mainPath := filepath.Join(tempdir, "main.proto")
538+
err = os.WriteFile(mainPath, []byte("package main; import \"extra.proto\";"), 0644)
539+
testutil.Ok(t, err)
540+
p := Parser{
541+
ImportPaths: []string{tempdir},
542+
}
543+
fds, err := p.ParseFilesButDoNotLink(mainPath)
544+
testutil.Ok(t, err)
545+
testutil.Eq(t, 1, len(fds))
546+
}

0 commit comments

Comments
 (0)