asticode/go-astilectron

Fails to start consistently after trying to bundle

Closed this issue · 8 comments

Hello! After attempting to bundle the project with the suggested bundler, project fails to start consistently. The project was starting fine with before the attempted bundle. Here is the logs:

INFO[0000] astikit: starting worker...                  
DEBU[0000] Starting...                                  
DEBU[0000] Provisioning...                              
DEBU[0000] Provisioning Astilectron...                  
DEBU[0000] Removing directory /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron 
DEBU[0000] Disembedding vendor_astilectron_bundler/astilectron.zip into /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron-v0.41.0.zip... 
DEBU[0000] /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron-v0.41.0.zip already exists, skipping disembed... 
DEBU[0000] Creating directory /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron 
DEBU[0000] Unzipping /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron-v0.41.0.zip/astilectron-0.41.0 into /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron 
DEBU[0000] Removing /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron... 
DEBU[0000] Closing...                                   
INFO[0000] astikit: stopping worker...                  
starting astilectron failed: provisioning failed: provisioning astilectron failed: unzipping /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron-v0.41.0.zip/astilectron-0.41.0 into /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron failed: unzipping /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron-v0.41.0.zip/astilectron-0.41.0 into /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron failed: astikit: opening overall zip reader on /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/vendor/astilectron-v0.41.0.zip failed: zip: not a valid zip file

Here is the code that I wrote to start the GUI:

package gui

import (
	"encoding/json"
	"fmt"
	"os/user"
	"time"

	"github.com/asticode/go-astikit"
	"github.com/asticode/go-astilectron"
	bootstrap "github.com/asticode/go-astilectron-bootstrap"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	"github.com/zalando/go-keyring"
)

const htmlAbout = `Welcome on <b>Astilectron</b> demo!<br>
This is using the bootstrap and the bundler.`

// MessageHandler is an interface to get the handlers for the gui message
type MessageHandler interface {
	GetHandler() (string, Handler)
}

// Run runs the window
func Run(handlers ...MessageHandler) error {
	m := MessageHandlers{}
	for _, handler := range handlers {
		handlerName, handlerFun := handler.GetHandler()
		m[handlerName] = handlerFun
	}
	var w *astilectron.Window
	l := logrus.StandardLogger()
	if viper.GetBool("application.debug") {
		l.SetLevel(logrus.TraceLevel)
	}
	if err := bootstrap.Run(bootstrap.Options{
		Debug: true,
		Asset: func(name string) ([]byte, error) {
			logrus.Infoln("Loading name", name)
			return []byte{}, nil
		},
		MenuOptions: []*astilectron.MenuItemOptions{{
			Label: astikit.StrPtr("File"),
			SubMenu: []*astilectron.MenuItemOptions{
				{
					Label: astikit.StrPtr("About"),
					OnClick: func(e astilectron.Event) (deleteListener bool) {
						if err := bootstrap.SendMessage(w, "about", htmlAbout, func(m *bootstrap.MessageIn) {
							// Unmarshal payload
							var s string
							if err := json.Unmarshal(m.Payload, &s); err != nil {
								logrus.Errorln(fmt.Errorf("unmarshaling payload failed: %w", err))
								return
							}
							logrus.Infoln("About modal has been displayed and payload is %s!\n", s)
						}); err != nil {
							logrus.Errorln(fmt.Errorf("sending about event failed: %w", err))
						}
						return
					},
				},
				{Role: astilectron.MenuItemRoleClose},
			},
		},
			{
				Label: astikit.StrPtr("Account"),
				SubMenu: []*astilectron.MenuItemOptions{
					{
						Label: astikit.StrPtr("Log Out"),
						OnClick: func(e astilectron.Event) (deleteListener bool) {
							if err := bootstrap.SendMessage(w, "logout", htmlAbout, func(m *bootstrap.MessageIn) {
								curUser, err := user.Current()
								if err != nil {
									logrus.Errorln("error getting user:", err)
								}
								appName := viper.GetString("application.name")
								keyring.Delete(fmt.Sprintf("%s.refreshToken", appName), curUser.Name)
								keyring.Delete(fmt.Sprintf("%s.accessToken", appName), curUser.Name)
								return

							}); err != nil {
								logrus.Errorln(fmt.Errorf("sending about event failed: %w", err))
							}
							return
						},
					},
				},
			},
		},
		OnWait: func(_ *astilectron.Astilectron, ws []*astilectron.Window, _ *astilectron.Menu, _ *astilectron.Tray, _ *astilectron.Menu) error {
			w = ws[0]
			go func() {
				time.Sleep(5 * time.Second)
				if err := bootstrap.SendMessage(w, "check.out.menu", "Don't forget to check out the menu!"); err != nil {
					logrus.Errorln(fmt.Errorf("sending check.out.menu event failed: %w", err))
				}
			}()
			return nil
		},
		Windows: []*bootstrap.Window{{
			Homepage: "http://localhost:8080",
			// MessageHandler: handleMessages,
			Options: &astilectron.WindowOptions{
				Center: astikit.BoolPtr(true),
				Height: astikit.IntPtr(700),
				Width:  astikit.IntPtr(700),
			},
			MessageHandler: m.handleMessages,
		}},
		AstilectronOptions: astilectron.Options{
			AppName:            "Courier",
			AppIconDarwinPath:  "resources/icon.icns",
			AppIconDefaultPath: "resources/icon.png",
			SingleInstance:     true,
		},
		Logger: l,
	}); err != nil {
		return err
	}
	return nil
}

I am using Ubuntu 20.04

Can you paste logs when running the bundler ?

Sorry about that! Here it is:

2020/10/29 21:44:19 Bundling for environment linux/amd64
2020/10/29 21:44:19 Binding data
2020/10/29 21:44:19 Removing /tmp/astibundler/bind
2020/10/29 21:44:19 Creating /tmp/astibundler/bind
2020/10/29 21:44:19 Creating /tmp/astibundler/bind/vendor_astilectron_bundler
2020/10/29 21:44:19 Creating /tmp/astibundler/cache
2020/10/29 21:44:19 /tmp/astibundler/cache/astilectron-0.41.0.zip already exists, skipping download of https://github.com/asticode/astilectron/archive/v0.41.0.zip
2020/10/29 21:44:19 Copying /tmp/astibundler/cache/astilectron-0.41.0.zip to /tmp/astibundler/bind/vendor_astilectron_bundler/astilectron.zip
2020/10/29 21:44:19 /tmp/astibundler/cache/electron-linux-amd64-7.1.10.zip already exists, skipping download of https://github.com/electron/electron/releases/download/v7.1.10/electron-v7.1.10-linux-x64.zip
2020/10/29 21:44:19 Copying /tmp/astibundler/cache/electron-linux-amd64-7.1.10.zip to /tmp/astibundler/bind/vendor_astilectron_bundler/electron.zip
2020/10/29 21:44:19 Creating /tmp/resources
2020/10/29 21:44:19 Copying /home/yoseph/shuttl/courier/courier-client/resources to /tmp/resources
2020/10/29 21:44:19 Generating /home/yoseph/shuttl/courier/courier-client/cmd/courier/bind_linux_amd64.go
2020/10/29 21:44:22 Removing /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64
2020/10/29 21:44:22 Creating /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64
2020/10/29 21:44:22 Building for os linux and arch amd64 astilectron: 0.41.0 electron: 7.1.10
2020/10/29 21:44:22 Executing go build -ldflags -X "main.AppName=Courier" -X "main.BuiltAt=2020-10-29 21:44:22.441268145 -0400 EDT m=+2.560090471" -X "main.VersionAstilectron=0.41.0" -X "main.VersionElectron=7.1.10" -o /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/binary .
2020/10/29 21:44:30 Moving /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/binary to /home/yoseph/shuttl/courier/courier-client/cmd/courier/output/linux-amd64/Courier

This also persists after I clean the cache for the bundler

Can you confirm that /tmp/astibundler/cache/astilectron-0.41.0.zip is a valid zip file on your computer ?

I have the same issue when using the bundler.
And the Zip file is not valid on my computer.

Using a Mac

I integrated the Bundler without Bootstrap, like suggested here: asticode/go-astilectron-bundler#82

@bogdanfinn if you have integrated the bundler without the bootstrap, chances are your restore resources logic is invalid

@asticode You were right in my case. I missed that point where you remove the bind.go and use the actuall generated bind_*.go files

Not sure how I fixed this. Perhaps a computer restart and clearing the cache, but its all good now.