Skip to content

Commit

Permalink
Skip over already-seen blobs when generating Bazel targets.
Browse files Browse the repository at this point in the history
When using the -shallow flag, multiple targets with the same name
were being generated, which Bazel does not like. This commit
ensures we only generate one Bazel rule per blob.
  • Loading branch information
captainreality committed Sep 16, 2024
1 parent 14fd546 commit 16c3763
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions go/pkg/ociutil/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"sync"

"github.com/bazelbuild/bazel-gazelle/rule"
Expand Down Expand Up @@ -51,10 +52,20 @@ func GenerateBuildFilesHandler(handler images.HandlerFunc, layoutRoot string, pr

layoutBuild.Save(filepath.Join(layoutRoot, "BUILD.bazel"))

// It's possible to encounter the same blob multiple times. We record the
// ones we've already encountered so we don't process them twice.
// Duplicate rules make bazel sad.
handledBlobs := make([]string, 0)

return func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
writemx.Lock()
defer writemx.Unlock()

if slices.Contains(handledBlobs, desc.Digest.String()) {
// We've already seen this blob; do nothing.
return handler(ctx, desc)
}

if !blobExists(layoutRoot, desc.Digest) {
return nil, images.ErrSkipDesc
}
Expand All @@ -78,15 +89,13 @@ func GenerateBuildFilesHandler(handler images.HandlerFunc, layoutRoot string, pr
}

imageManifestRule(desc, manifest).Insert(f)
break
case ocispec.MediaTypeImageIndex, images.MediaTypeDockerSchema2ManifestList:
index, err := ImageIndexFromProvider(ctx, provider, desc)
if err != nil {
return nil, err
}

imageIndexManifestRule(desc, index).Insert(f)
break
}

// Save all BUILD files
Expand All @@ -104,17 +113,14 @@ func GenerateBuildFilesHandler(handler images.HandlerFunc, layoutRoot string, pr
return nil, err
}

handledBlobs = append(handledBlobs, desc.Digest.String())
return handler(ctx, desc)
}
}

func blobExists(layoutRoot string, dgst digest.Digest) bool {
_, err := os.Stat(descToFilePath(layoutRoot, dgst))
if os.IsNotExist(err) {
return false
}

return true
return !os.IsNotExist(err)
}

func descToFilePath(root string, dgst digest.Digest) string {
Expand Down Expand Up @@ -188,7 +194,3 @@ func imageIndexManifestRule(desc ocispec.Descriptor, manifest ocispec.Index) *ru

return r
}

func blobPath(layoutRoot string, dgst digest.Digest) string {
return filepath.Join(layoutRoot, dgst.Algorithm().String())
}

0 comments on commit 16c3763

Please sign in to comment.