Fix GetProjectRoot in GO (#16520)

### Summary

Fix GetProjectRoot in GO
This commit is contained in:
qinling0210
2026-07-01 18:17:53 +08:00
committed by GitHub
parent 81cfcdf2d3
commit 5ba25a5267
2 changed files with 14 additions and 36 deletions

View File

@@ -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, " ")

View File

@@ -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 <project_root>/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))
}