From 5f5bc89967cc1307132bea5ba40ceae65aa7547c Mon Sep 17 00:00:00 2001 From: jaron Date: Wed, 15 Apr 2026 14:46:20 +0800 Subject: [PATCH] chore(gen): fix working dir --- cmd/jzero/main.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/cmd/jzero/main.go b/cmd/jzero/main.go index 504c05c00..974063839 100644 --- a/cmd/jzero/main.go +++ b/cmd/jzero/main.go @@ -7,6 +7,9 @@ package main import ( "embed" + "errors" + "fmt" + "io/fs" "os" "path/filepath" "strconv" @@ -160,9 +163,23 @@ func InitConfig() { } } - if WorkingDir != "" { - if err := os.Chdir(WorkingDir); err != nil { - cobra.CheckErr(err) + if WorkingDir != "" && os.Getenv("JZERO_FORKED") != "true" { + // Convert relative path to absolute path + absPath, err := filepath.Abs(WorkingDir) + if err != nil { + cobra.CheckErr(fmt.Errorf("failed to get absolute path for working directory: %w", err)) + } + + // Verify the directory exists + if _, err := os.Stat(absPath); err != nil { + if errors.Is(err, fs.ErrNotExist) { + cobra.CheckErr(fmt.Errorf("working directory does not exist: %s", absPath)) + } + cobra.CheckErr(fmt.Errorf("failed to access working directory: %w", err)) + } + + if err := os.Chdir(absPath); err != nil { + cobra.CheckErr(fmt.Errorf("failed to change to working directory: %w", err)) } }