fix: resolve desktop app crash on launch by fixing working directory and loading resources

This commit is contained in:
wizardchen
2026-04-11 01:03:51 +08:00
committed by lyingbug
parent 026c4c7662
commit 0a964ac0b7
2 changed files with 25 additions and 2 deletions
+19 -2
View File
@@ -7,6 +7,8 @@ import (
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
@@ -23,12 +25,24 @@ import (
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/runtime"
"github.com/Tencent/WeKnora/internal/tracing"
wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/joho/godotenv"
wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/sys/unix"
)
func main() {
// For macOS .app bundle, the working directory is usually "/" or the MacOS folder.
// We need to change the working directory to the Resources folder where our configs are.
execPath, errPath := os.Executable()
if errPath == nil && strings.Contains(execPath, ".app/Contents/MacOS") {
resPath := filepath.Join(filepath.Dir(filepath.Dir(execPath)), "Resources")
_ = os.Chdir(resPath)
}
// Load .env explicitly for the desktop app so DB_DRIVER gets loaded
_ = godotenv.Load()
// Set Gin mode
if os.Getenv("GIN_MODE") == "release" {
gin.SetMode(gin.ReleaseMode)
@@ -137,13 +151,16 @@ func main() {
EditMenu.AddText("Select All", keys.CmdOrCtrl("a"), func(_ *menu.CallbackData) {})
// Start Wails application
// We use a dummy handler for AssetServer to prevent wails from complaining
err := wails.Run(&options.App{
Title: "WeKnora Lite",
Width: 1280,
Height: 800,
Menu: AppMenu,
AssetServer: &assetserver.Options{
Assets: nil,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}),
},
StartHidden: false, // Show window on startup
OnStartup: app.startup,
+6
View File
@@ -72,6 +72,12 @@ fi
if [ -d migrations/sqlite ]; then
cp -r migrations/sqlite/* "${RESOURCES_DIR}/migrations/sqlite/"
fi
if [ -d config ]; then
cp -r config/* "${RESOURCES_DIR}/config/"
fi
if [ -d web ]; then
cp -r web "${RESOURCES_DIR}/"
fi
# 注意:Wails build 生成的二进制文件工作目录默认是 app 的 Contents/MacOS 目录
# 后续可能需要调整代码中对配置文件的路径读取逻辑。