markbates / pkger

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

[Bug] Doesn't work with `go install` #62

Closed koddr closed 4 years ago

koddr commented 4 years ago

Hi!

I'm using Go 1.13.5.

On my ./cmd/myapp/main.go:

package main

import "github.com/markbates/pkger"

func main() {
    pkger.Include("/configs")

    // ...
}

// CopyFolder function for copy all files from app config
func CopyFolder() error {
    return pkger.Walk("/configs/dotfiles", func(path string, file os.FileInfo, err error) error {
        // Define files paths
        folder := "." + string(os.PathSeparator) + file.Name()

        // Create files (skip directories)
        if !file.IsDir() {
            // Open file
            from, err := pkger.Open(path)
            ErrChecker(err)
            defer from.Close()

            // Create file
            to, err := os.Create(folder)
            ErrChecker(err)
            defer to.Close()

            // Copy file
            _, err = io.Copy(to, from)
            ErrChecker(err)

            // Show report for each file
            fmt.Printf("— File '%v' was copied!\n", folder)
        }

        // Default return
        return nil
    })
}

App structure:

$ tree .

myapp
├── cmd
│   └── myapp
│       └── main.go
├── configs
│   └── dotfiles
│       └── .env
├── go.mod
├── go.sum
└── pkged.go

I packed my app like this:

$ pkger && go build -o myapp ./cmd/myapp/*.go

If I run binary ./myapp from folder — all okay. Files from ./config was copied.

But, if I run $ go install ./cmd/myapp and call binary (who installed to $GOPATH/bin) as usual, by $ myapp [args], I got this error:

Error: lstat configs/dotfiles: no such file or directory

What's wrong? How to solve this? Help me please 😞

EvanBoyle commented 4 years ago

@markbates I'm seeing similar behavior. The app works fine if I run the binary from same root as my go.mod, but not from outside that path.

EvanBoyle commented 4 years ago

@koddr this is a dupe of https://github.com/markbates/pkger/issues/38

To summarize, adding pkger -o cmd/foo where cmd/foo is the path fed to go install did the trick for me.

EvanBoyle commented 4 years ago

For what it's worth, it's still a little unintuitive. I have to run pkger in the root directory first, and then run pkger -o cmd/foo to get the behavior I would expect. My repo is public if you care to take a look. Makefile here: https://github.com/EvanBoyle/pupiter/blob/master/Makefile

koddr commented 4 years ago

@EvanBoyle thanks for solution! It works fine :+1: