markbates / pkger

Embed static files in Go binaries (replacement for gobuffalo/packr)
MIT License
1.19k stars 60 forks source link

pkger -include fails when using modules versioned with tags + path #143

Open ideasculptor opened 3 years ago

ideasculptor commented 3 years ago

If you are using go modules with versions specified via path and tag, pkger -include fails.

go.mod:

module github.com/thisisme/somerepo/v10

go 1.14

require (
        ...
    github.com/markbates/pkger v0.17.1
    github.com/thisisme/soimeotherrepo/v2 v2.0.2
        ...
)

some/nested/path/main.go:

package main

import (
    "fmt"
    "io"
    "os"

    "github.com/tunein/go-logging/v3"

    "github.com/markbates/pkger"
)

func main() {
    f, err := pkger.Open("/some/nested/path/file.txt")
    if err != nil {
        logging.Error(err, "error opening file")
        os.Exit(-1)
    }
    defer f.Close()

    info, err := f.Stat()
    if err != nil {
        logging.Error(err, "error stating file")
        os.Exit(-1)
    }

    fmt.Println("Name: ", info.Name())
    fmt.Println("Size: ", info.Size())
    fmt.Println("Mode: ", info.Mode())
    fmt.Println("ModTime: ", info.ModTime())

    if _, err := io.Copy(os.Stdout, f); err != nil {
        logging.Error(err, "error copying file to stdout")
        os.Exit(-1)
    }
    os.Exit(0)
}

some/nested/path/file.txt:

this is some text
and this is some more

I can run pkger -o some/nested/path and everything works just fine, generating some/nested/path/pkged.go with package main. However, if I run pkger -include /some/nested/path -o some/nested/path, I get errors because it doesn't correctly resolve that the /v10 suffix on the module declared in the go.mod file is the current directory, so it tries to go to github to get it.

2021/02/04 15:06:08 exit status 1: go: finding module for package github.com/thisisme/somerepo/v10
can't load package: package github.com/thisisme/somerepo/v10: module github.com/thisisme/somerepo@latest found (v7.0.23+incompatible), but does not contain package github.com/thisisme/somerepo/v10

From version 8, we have been explicitly declaring the module with the path element containing the major version, so the most recent version it can find without a /vN suffix is v7.0.23, which is tagged, but still uses the bare name for the module declaration. Since this module has a lot of directories and many files, running pkger without -include in order to include a single text file takes a remarkably long time. It's not the end of the world, but it would be better if it worked correctly with path-based go module versioning.