Skip to content

Commit

Permalink
Avoid building image for buf export when --exclude-imports flag i…
Browse files Browse the repository at this point in the history
…s set (#3009)

This PR changes `buf export` so that when `--exclude-imports` is
passed, it does not require the input to be buildable as an image.
This allows a user to provide an input that may not be buildable, and
they can still get the source files back when `--exclude-imports` is
set, since we do not need the image to check for import files.

Fixes #3002
  • Loading branch information
doriable authored May 22, 2024
1 parent 09ff5ec commit 1ff6e69
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions private/buf/cmd/buf/command/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,6 @@ func run(
return err
}
moduleReadBucket := bufmodule.ModuleSetToModuleReadBucketWithOnlyProtoFiles(workspace)
image, err := controller.GetImageForWorkspace(
ctx,
workspace,
bufctl.WithImageExcludeSourceInfo(true),
bufctl.WithImageExcludeImports(flags.ExcludeImports),
)
if err != nil {
return err
}

if err := os.MkdirAll(flags.Output, 0755); err != nil {
return err
Expand All @@ -174,6 +165,40 @@ func run(
if err != nil {
return err
}

// In the case where we are excluding imports, we are allowing users to specify an input
// that may not have resolved imports (https://github.com/bufbuild/buf/issues/3002).
// Thus we do not need to build the image, and instead we can return the non-import files
// from the workspace.
if flags.ExcludeImports {
if err := moduleReadBucket.WalkFileInfos(
ctx,
func(fileInfo bufmodule.FileInfo) error {
moduleFile, err := moduleReadBucket.GetFile(ctx, fileInfo.Path())
if err != nil {
return syserror.Wrap(err)
}
if err := storage.CopyReadObject(ctx, readWriteBucket, moduleFile); err != nil {
return multierr.Append(err, moduleFile.Close())
}
return moduleFile.Close()
},
bufmodule.WalkFileInfosWithOnlyTargetFiles(),
); err != nil {
return err
}
return nil
}

image, err := controller.GetImageForWorkspace(
ctx,
workspace,
bufctl.WithImageExcludeSourceInfo(true),
bufctl.WithImageExcludeImports(flags.ExcludeImports),
)
if err != nil {
return err
}
imageFiles := image.Files()
if len(imageFiles) == 0 {
return errors.New("no .proto target files found")
Expand Down

0 comments on commit 1ff6e69

Please sign in to comment.