From 5ba25a5267a8179be3319de7a6693f15d5e54247 Mon Sep 17 00:00:00 2001 From: qinling0210 <88864212+qinling0210@users.noreply.github.com> Date: Wed, 1 Jul 2026 18:17:53 +0800 Subject: [PATCH] Fix GetProjectRoot in GO (#16520) ### Summary Fix GetProjectRoot in GO --- internal/engine/infinity/sql.go | 21 ++------------------- internal/utility/path.go | 29 ++++++++++++----------------- 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/internal/engine/infinity/sql.go b/internal/engine/infinity/sql.go index fedff93354..879b337f48 100644 --- a/internal/engine/infinity/sql.go +++ b/internal/engine/infinity/sql.go @@ -31,6 +31,7 @@ import ( "time" "ragflow/internal/common" + "ragflow/internal/utility" "go.uber.org/zap" ) @@ -58,7 +59,7 @@ func loadFieldMapping(mappingFileName string) (aliasToActual map[string]string, if mappingFileName == "" { mappingFileName = "infinity_mapping.json" } - confPath := filepath.Join(projectBaseDir(), "conf", mappingFileName) + confPath := filepath.Join(utility.GetProjectRoot(), "conf", mappingFileName) data, err := os.ReadFile(confPath) if err != nil { if errors.Is(err, os.ErrNotExist) { @@ -96,24 +97,6 @@ func loadFieldMapping(mappingFileName string) (aliasToActual map[string]string, return aliasToActual, actualToFirstAlias, nil } -// projectBaseDir returns the project root. Honors RAG_PROJECT_BASE and -// RAG_DEPLOY_BASE env vars; falls back to working directory. -func projectBaseDir() string { - if v := os.Getenv("RAG_PROJECT_BASE"); v != "" { - return v - } - if v := os.Getenv("RAG_DEPLOY_BASE"); v != "" { - return v - } - // Fall back to the repository root. The Go engine package lives at - // internal/engine/infinity/; the repo root is three levels up. - wd, err := os.Getwd() - if err != nil { - return "." - } - return wd -} - // preprocessSQL collapses spaces/backticks and strips '%'. func preprocessSQL(sql string) string { sql = whitespaceRe.ReplaceAllString(sql, " ") diff --git a/internal/utility/path.go b/internal/utility/path.go index fdeb68c8e5..b7baf93424 100644 --- a/internal/utility/path.go +++ b/internal/utility/path.go @@ -19,28 +19,23 @@ package utility import ( "os" "path/filepath" - "runtime" ) -// GetProjectRoot returns the project root directory by finding go.mod marker +// GetProjectRoot returns the project root directory func GetProjectRoot() string { // Try environment variable first - if confDir := os.Getenv("RAGFLOW_CONF_DIR"); confDir != "" { - return confDir + if d := os.Getenv("RAG_PROJECT_BASE"); d != "" { + return d + } + if d := os.Getenv("RAG_DEPLOY_BASE"); d != "" { + return d } - // Find project root by looking for go.mod - _, curFile, _, _ := runtime.Caller(0) - dir := filepath.Dir(curFile) - for { - if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { - return dir - } - parent := filepath.Dir(dir) - if parent == dir { - // Reached filesystem root, fallback to hardcoded path - return filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(curFile)))) - } - dir = parent + // The binary is always at /bin/, so going up 2 levels from + // the executable path gives the project root. + exe, err := os.Executable() + if err != nil { + return "." } + return filepath.Dir(filepath.Dir(exe)) }