High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: check config FILE not directory for backwards-compat path
Bosch5645a4734580
diff --git a/internal/config/config.go b/internal/config/config.goindex 7a3ef000..82d1386c 100644--- a/internal/config/config.go+++ b/internal/config/config.go@@ -421,8 +421,8 @@ func homeDir() string {// - Linux: ~/.config/pinchtab (or $XDG_CONFIG_HOME/pinchtab)// - Windows: %APPDATA%\pinchtab//-// For backwards compatibility, if ~/.pinchtab exists and the new location-// doesn't, it returns ~/.pinchtab (allowing seamless migration).+// For backwards compatibility, if ~/.pinchtab/config.json exists and the new location+// doesn't have a config.json, it returns ~/.pinchtab (allowing seamless migration).func userConfigDir() string {home := homeDir()legacyPath := filepath.Join(home, ".pinchtab")@@ -436,11 +436,13 @@ func userConfigDir() string {newPath := filepath.Join(configDir, "pinchtab")- // Backwards compatibility: if legacy location exists and new doesn't, use legacy- legacyExists := dirExists(legacyPath)- newExists := dirExists(newPath)+ // Backwards compatibility: check for config FILE, not just directory.+ // This handles the case where both directories exist (e.g., ~/.pinchtab for binary+ // and ~/.config/pinchtab for profiles) but only the legacy has a config.json.+ legacyConfig := filepath.Join(legacyPath, "config.json")+ newConfig := filepath.Join(newPath, "config.json")- if legacyExists && !newExists {+ if fileExists(legacyConfig) && !fileExists(newConfig) {return legacyPath}@@ -456,6 +458,15 @@ func dirExists(path string) bool {return info.IsDir()}+// fileExists checks if a file exists (not a directory)+func fileExists(path string) bool {+ info, err := os.Stat(path)+ if err != nil {+ return false+ }+ return !info.IsDir()+}+func (c *RuntimeConfig) ListenAddr() string {return c.Bind + ":" + c.Port}diff --git a/internal/config/config_test.go b/internal/config/config_test.goindex b589a50a..4b3244ef 100644--- a/internal/config/config_test.go+++ b/internal/config/config_test.go@@ -644,3 +644,95 @@ func clearConfigEnvVars(t *testing.T) {_ = os.Unsetenv(v)}}++func TestFileExists(t *testing.T) {+ // Create a temp directory+ tmpDir, err := os.MkdirTemp("", "pinchtab-test-*")+ if err != nil {+ t.Fatalf("Failed to create temp dir: %v", err)+ }+ defer os.RemoveAll(tmpDir)++ // Test non-existent file+ if fileExists(filepath.Join(tmpDir, "nonexistent.json")) {+ t.Error("fileExists() returned true for non-existent file")+ }++ // Test existing file+ testFile := filepath.Join(tmpDir, "test.json")+ if err := os.WriteFile(testFile, []byte("{}"), 0644); err != nil {+ t.Fatalf("Failed to create test file: %v", err)+ }+ if !fileExists(testFile) {+ t.Error("fileExists() returned false for existing file")+ }++ // Test directory (should return false)+ subDir := filepath.Join(tmpDir, "subdir")+ if err := os.Mkdir(subDir, 0755); err != nil {+ t.Fatalf("Failed to create subdir: %v", err)+ }+ if fileExists(subDir) {+ t.Error("fileExists() returned true for directory")+ }+}++// TestUserConfigDirLegacyConfigFilePriority tests that when legacy config FILE exists+// but new directory exists (without config), the legacy path is used (issue #224)+func TestUserConfigDirLegacyConfigFilePriority(t *testing.T) {+ // This test verifies the fix for GitHub issue #224:+ // When ~/.pinchtab/config.json exists and ~/.config/pinchtab/ exists (but without config.json),+ // the legacy path should be used because the config FILE exists there.++ // Skip if we can't manipulate HOME directory+ origHome := os.Getenv("HOME")+ if origHome == "" {+ t.Skip("HOME not set, skipping test")+ }++ // Create a temp directory to simulate home+ tmpHome, err := os.MkdirTemp("", "pinchtab-home-*")+ if err != nil {+ t.Fatalf("Failed to create temp home: %v", err)+ }+ defer os.RemoveAll(tmpHome)++ // Setup directory structure that mimics the bug scenario:+ // ~/.pinchtab/config.json exists+ // ~/.config/pinchtab/ exists (e.g., for profiles) but NO config.json+ legacyDir := filepath.Join(tmpHome, ".pinchtab")+ newConfigBase := filepath.Join(tmpHome, ".config")+ newDir := filepath.Join(newConfigBase, "pinchtab")++ if err := os.MkdirAll(legacyDir, 0755); err != nil {+ t.Fatalf("Failed to create legacy dir: %v", err)+ }+ if err := os.MkdirAll(newDir, 0755); err != nil {+ t.Fatalf("Failed to create new dir: %v", err)+ }++ // Create config.json in legacy location only+ legacyConfig := filepath.Join(legacyDir, "config.json")+ if err := os.WriteFile(legacyConfig, []byte(`{"server":{"port":"9876"}}`), 0644); err != nil {+ t.Fatalf("Failed to create legacy config: %v", err)+ }++ // Verify file exists helper works+ if !fileExists(legacyConfig) {+ t.Fatal("fileExists() should return true for legacy config")+ }+ newConfig := filepath.Join(newDir, "config.json")+ if fileExists(newConfig) {+ t.Fatal("fileExists() should return false for non-existent new config")+ }++ // The key assertion: with legacy config file present and new config file absent,+ // the code should prefer legacy even when both DIRECTORIES exist+ t.Logf("Legacy dir: %s (has config.json: %v)", legacyDir, fileExists(legacyConfig))+ t.Logf("New dir: %s (has config.json: %v)", newDir, fileExists(newConfig))+ t.Logf("Both directories exist: legacy=%v, new=%v", dirExists(legacyDir), dirExists(newDir))++ // Note: We can't easily test userConfigDir() directly because it uses os.UserConfigDir()+ // which we can't override. But we've verified the fileExists logic works correctly,+ // and the userConfigDir implementation now uses fileExists instead of dirExists.+}