Skip to content

Commit

Permalink
tweak the way ImageWithoutImports is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
jhump committed Apr 17, 2024
1 parent 1e12d5b commit c041516
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
4 changes: 3 additions & 1 deletion private/bufpkg/bufimage/bufimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,9 @@ func ImageWithoutImports(image Image) Image {
newImageFiles = append(newImageFiles, imageFile)
}
}
return newImageNoValidate(newImageFiles)
// We preserve the original resolver because a resolver can't
// be successfully created without the image's dependencies.
return newImageNoValidate(newImageFiles, image.Resolver())
}

// ImageWithOnlyPaths returns a copy of the Image that only includes the files
Expand Down
18 changes: 7 additions & 11 deletions private/bufpkg/bufimage/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ func newImage(files []ImageFile, reorder bool, resolver protoencoding.Resolver)
files = orderImageFiles(files, pathToImageFile)
}
if resolver == nil {
resolver = resolverForImageFiles(files)
fileDescriptorProtos := make([]*descriptorpb.FileDescriptorProto, len(files))
for i := range files {
fileDescriptorProtos[i] = files[i].FileDescriptorProto()
}
resolver = protoencoding.NewLazyResolver(fileDescriptorProtos...)
}
return &image{
files: files,
Expand All @@ -83,7 +87,7 @@ func newImage(files []ImageFile, reorder bool, resolver protoencoding.Resolver)
}, nil
}

func newImageNoValidate(files []ImageFile) *image {
func newImageNoValidate(files []ImageFile, resolver protoencoding.Resolver) *image {
pathToImageFile := make(map[string]ImageFile, len(files))
for _, file := range files {
path := file.Path()
Expand All @@ -92,7 +96,7 @@ func newImageNoValidate(files []ImageFile) *image {
return &image{
files: files,
pathToImageFile: pathToImageFile,
resolver: resolverForImageFiles(files),
resolver: resolver,
}
}

Expand Down Expand Up @@ -152,11 +156,3 @@ func orderImageFilesRec(
}
return append(outputImageFiles, inputImageFile)
}

func resolverForImageFiles(files []ImageFile) protoencoding.Resolver {
fileDescriptorProtos := make([]*descriptorpb.FileDescriptorProto, len(files))
for i := range files {
fileDescriptorProtos[i] = files[i].FileDescriptorProto()
}
return protoencoding.NewLazyResolver(fileDescriptorProtos...)
}

0 comments on commit c041516

Please sign in to comment.