From 5c4fbcf69867986334da680090491faee0a9de9b Mon Sep 17 00:00:00 2001 From: George Suntres Date: Mon, 13 Apr 2026 08:47:03 -0400 Subject: [PATCH] Project root of binary not file --- fs.go | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/fs.go b/fs.go index dd7dd64..2562da3 100644 --- a/fs.go +++ b/fs.go @@ -1,18 +1,41 @@ package sys import ( - "runtime" + "log" + "os" "path/filepath" ) func Rootify(path string) string { - return filepath.Join(GetProjectRoot(), path) + root, err := GetProjectRoot() + if err != nil { + log.Fatal(err) + } + + return filepath.Join(root, path) } -func GetProjectRoot() string { - _, b, _, _ := runtime.Caller(0) - // b is the absolute path to this specific .go file - // 1st Dir() gets the folder containing the file - // 2nd Dir() gets the parent of that folder - return filepath.Dir(filepath.Dir(b)) +// func GetProjectRoot() string { +// _, b, _, _ := runtime.Caller(0) +// // b is the absolute path to this specific .go file +// // 1st Dir() gets the folder containing the file +// // 2nd Dir() gets the parent of that folder +// return filepath.Dir(filepath.Dir(b)) +// } + +func GetProjectRoot() (string, error) { + exePath, err := os.Executable() + if err != nil { + return "", err + } + + exeDir := filepath.Dir(exePath) + + // adjust depending on your structure + // e.g. binary is in /project/bin/app → go up one level + root := filepath.Dir(exeDir) + + return root, nil } + +