Skip to content

Impossible to simply interoperate with distribution/reference uses of digest.Digest #2130

Closed

Description

@mtrmac

Consider

package main

import (
    "github.com/opencontainers/go-digest"
    "github.com/docker/distribution/reference"
)

func consumeDigest(_ digest.Digest) {
}

func produceDigest() digest.Digest {
    return digest.FromBytes([]byte{})
}

func main() {
    named, _ := reference.ParseNamed("busybox@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
    digested, _ := named.(reference.Digested)
    consumeDigest(digested.Digest())
    overridden, _ := reference.WithDigest(named, produceDigest())
}

where consumeDigest and produceDigest use a correctly typed digest.Digest.

This fails with

./x.go:18: cannot use digested.Digest() (type "github.com/docker/distribution/vendor/github.com/opencontainers/go-digest".Digest) as type "github.com/opencontainers/go-digest".Digest in argument to consumeDigest
./x.go:19: cannot use produceDigest() (type "github.com/opencontainers/go-digest".Digest) as type "github.com/docker/distribution/vendor/github.com/opencontainers/go-digest".Digest in argument to reference.WithDigest

The first failure can be worked around by using

    consumeDigest(digest.Digest(digested.Digest()))

instead; the second one can’t be worked around at all AFAICS, because we need to convert the digest.Digest value to a type acceptable to reference.WithDigest, and a basic string is insufficient and the expected type "github.com/docker/distribution/vendor/github.com/opencontainers/go-digest".Digest cannot be named:

	imports github.com/docker/distribution/vendor/github.com/opencontainers/go-digest: use of vendored package not allowed

AFAICS the only way to make this work with the existing docker/distribution is to give up on having a trivial go package, and to vendor a copy of github.com/docker/distribution with github.com/docker/distribution/vendor stripped. That forces a build / vendoring toolchain on a package which would otherwise be a simple stand-alone Go library built using a simple go build.

Would you consider dropping the vendor/opencontainers/go-digest subdirectory from your repo, and using the one from $GOPATH instead? Or, I don’t know, define type Digest digest.Digest in reference/reference.go so that the type has an accessible name that can be used in the workaround conversions?

Activity

  1. dmcgowan commented on Jan 9, 2017

    @dmcgowan
    Collaborator

    Every package has different strategies for vendoring code. Unfortunately there is no good convention for only vendoring internally used packages (as opposed to packages which are exposed through the interface). We have found the best method for vendoring involves flattening the vendor directories to prevent this. However I don't think it is the responsibility of this package to strip vendors because they are exposed on the interface or change the code to work around it, this is (as painful as it can be at times) the responsibility of the importers vendoring tool to solve.

    I do sympathize with this annoyance but I think the code and vendors are correct.

  2. aaronlehmann commented on Jan 9, 2017

    @aaronlehmann

    docker/docker recently moved from GOPATH munging to Go 1.6 vendoring and ran into similar issues: moby/moby#29362

    They attempted to move the vendor directory under cmd to work around this: moby/moby#28080. However, that approach has downsides, and it was abandoned.

  3. mtrmac commented on Aug 8, 2020

    @mtrmac
    Author

    With Go modules, which ignore the vendor subdirectories of libraries, being the default vendoring mechanism, this is no longer much of an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions