Skip to content

Commit e272776

Browse files
committed
Fix Dockerfile path resolution using source file location
- Use runtime.Caller to find source file location as starting point - Walk up from mysql/ directory to find repo root - Convert to absolute path for consistency - Better error messages showing both dockerfile path and module root
1 parent b4f0f1b commit e272776

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

mysql/testcontainers_helper.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"os/exec"
1616
"path/filepath"
17+
"runtime"
1718
"strings"
1819
"sync"
1920
"testing"
@@ -547,25 +548,32 @@ func startSharedTiDBClusterWithTiUP(version string) (*TiDBTestCluster, error) {
547548

548549
// Build TiUP Playground image from Dockerfile
549550
// Get the git root directory (where Dockerfile.tiup-playground is located)
551+
// Use absolute path to avoid issues with working directory
550552
moduleRoot := os.Getenv("GITHUB_WORKSPACE")
551553
if moduleRoot == "" {
552554
// For local development, find git root using git rev-parse
553555
gitPath, err := exec.LookPath("git")
554556
if err != nil {
555557
// Git not found, try to find repo root by looking for .git directory
556-
cwd, err := os.Getwd()
557-
if err != nil {
558-
return nil, fmt.Errorf("failed to get current working directory: %v", err)
559-
}
560-
dir := cwd
558+
// Start from the directory where this source file is located
559+
_, sourceFile, _, _ := runtime.Caller(0)
560+
sourceDir := filepath.Dir(sourceFile)
561+
// Go up from mysql/ to repo root
562+
dir := filepath.Dir(sourceDir)
561563
for {
564+
dockerfilePath := filepath.Join(dir, "Dockerfile.tiup-playground")
565+
if _, err := os.Stat(dockerfilePath); err == nil {
566+
moduleRoot = dir
567+
break
568+
}
569+
// Also check for .git as fallback
562570
if _, err := os.Stat(filepath.Join(dir, ".git")); err == nil {
563571
moduleRoot = dir
564572
break
565573
}
566574
parent := filepath.Dir(dir)
567575
if parent == dir {
568-
return nil, fmt.Errorf("could not find git root (.git directory) in parent directories of %s", cwd)
576+
return nil, fmt.Errorf("could not find Dockerfile.tiup-playground or .git in parent directories of %s", sourceDir)
569577
}
570578
dir = parent
571579
}
@@ -579,10 +587,17 @@ func startSharedTiDBClusterWithTiUP(version string) (*TiDBTestCluster, error) {
579587
}
580588
}
581589

590+
// Convert to absolute path to ensure consistency
591+
absModuleRoot, err := filepath.Abs(moduleRoot)
592+
if err != nil {
593+
return nil, fmt.Errorf("failed to get absolute path for module root %s: %v", moduleRoot, err)
594+
}
595+
moduleRoot = absModuleRoot
596+
582597
// Verify Dockerfile exists
583598
dockerfilePath := filepath.Join(moduleRoot, "Dockerfile.tiup-playground")
584599
if _, err := os.Stat(dockerfilePath); err != nil {
585-
return nil, fmt.Errorf("Dockerfile.tiup-playground not found at %s: %v", dockerfilePath, err)
600+
return nil, fmt.Errorf("Dockerfile.tiup-playground not found at %s (moduleRoot=%s): %v", dockerfilePath, moduleRoot, err)
586601
}
587602

588603
// Use a consistent image tag for caching

0 commit comments

Comments
 (0)