Repository: JanDeDobbeleer/oh-my-posh
Pull request #3442 (2023-02-03 06:53:46)

fix(config): log errors
### Prerequisites

- [x] I have read and understood the [contributing guide][CONTRIBUTING.md]
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added/updated (for bug fixes/features)

### Description

relates to #3441

<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/cli/config_migrate.go b/src/cli/config_migrate.go
index ed71cc48..0b6470b9 100644
--- a/src/cli/config_migrate.go
+++ b/src/cli/config_migrate.go
@@ -50,10 +50,10 @@ A backup of the current config can be found at ~/myconfig.omp.json.bak.`,
 		defer env.Close()
 		cfg := engine.LoadConfig(env)
 		if write {
-			cfg.BackupAndMigrate(env)
+			cfg.BackupAndMigrate()
 			return
 		}
-		cfg.Migrate(env)
+		cfg.Migrate()
 		fmt.Print(cfg.Export(format))
 	},
 }
diff --git a/src/engine/config.go b/src/engine/config.go
index fb0cbc4f..b7ae1041 100644
--- a/src/engine/config.go
+++ b/src/engine/config.go
@@ -89,10 +89,9 @@ func (cfg *Config) getPalette() ansi.Palette {
 // LoadConfig returns the default configuration including possible user overrides
 func LoadConfig(env platform.Environment) *Config {
 	cfg := loadConfig(env)
-	cfg.env = env
 	// only migrate automatically when the switch isn't set
 	if !env.Flags().Migrate && cfg.Version < configVersion {
-		cfg.BackupAndMigrate(env)
+		cfg.BackupAndMigrate()
 	}
 	return cfg
 }
@@ -102,12 +101,14 @@ func loadConfig(env platform.Environment) *Config {
 	configFile := env.Flags().Config
 
 	if len(configFile) == 0 {
-		return defaultConfig(false)
+		env.Debug("no config file specified, using default")
+		return defaultConfig(env, false)
 	}
 
 	var cfg Config
 	cfg.origin = configFile
 	cfg.format = strings.TrimPrefix(filepath.Ext(configFile), ".")
+	cfg.env = env
 	if cfg.format == "yml" {
 		cfg.format = YAML
 	}
@@ -126,12 +127,14 @@ func loadConfig(env platform.Environment) *Config {
 
 	err := config.LoadFiles(configFile)
 	if err != nil {
-		return defaultConfig(true)
+		env.Error(err)
+		return defaultConfig(env, true)
 	}
 
 	err = config.BindStruct("", &cfg)
 	if err != nil {
-		return defaultConfig(true)
+		env.Error(err)
+		return defaultConfig(env, true)
 	}
 
 	return &cfg
@@ -194,9 +197,9 @@ func (cfg *Config) Export(format string) string {
 	}
 }
 
-func (cfg *Config) BackupAndMigrate(env platform.Environment) {
+func (cfg *Config) BackupAndMigrate() {
 	cfg.Backup()
-	cfg.Migrate(env)
+	cfg.Migrate()
 	cfg.Write(cfg.format)
 }
 
@@ -301,7 +304,7 @@ func escapeGlyphs(s string, migrate bool) string {
 	return builder.String()
 }
 
-func defaultConfig(warning bool) *Config {
+func defaultConfig(env platform.Environment, warning bool) *Config {
 	exitBackgroundTemplate := "{{ if gt .Code 0 }}p:red{{ end }}"
 	exitTemplate := " {{ if gt .Code 0 }}\uf00d{{ else }}\uf00c{{ end }} "
 	if warning {
@@ -489,5 +492,6 @@ func defaultConfig(warning bool) *Config {
 			},
 		},
 	}
+	cfg.env = env
 	return cfg
 }
diff --git a/src/engine/migrate.go b/src/engine/migrate.go
index 2a707a9f..dc1b362e 100644
--- a/src/engine/migrate.go
+++ b/src/engine/migrate.go
@@ -17,14 +17,14 @@ const (
 	segmentTemplate = properties.Property("template")
 )
 
-func (cfg *Config) Migrate(env platform.Environment) {
+func (cfg *Config) Migrate() {
 	for _, block := range cfg.Blocks {
 		for _, segment := range block.Segments {
-			segment.migrate(env, cfg.Version)
+			segment.migrate(cfg.env, cfg.Version)
 		}
 	}
 	for _, segment := range cfg.Tooltips {
-		segment.migrate(env, cfg.Version)
+		segment.migrate(cfg.env, cfg.Version)
 	}
 	if strings.Contains(cfg.ConsoleTitleTemplate, ".Path") {
 		cfg.ConsoleTitleTemplate = strings.ReplaceAll(cfg.ConsoleTitleTemplate, ".Path", ".PWD")
diff --git a/src/platform/shell.go b/src/platform/shell.go
index 7ebaa43e..c6991e8a 100644
--- a/src/platform/shell.go
+++ b/src/platform/shell.go
@@ -275,11 +275,13 @@ func (env *Shell) resolveConfigPath() {
 		env.CmdFlags.Config = env.Getenv("POSH_THEME")
 	}
 	if len(env.CmdFlags.Config) == 0 {
+		env.Debug("No config set, fallback to default config")
 		return
 	}
 	if strings.HasPrefix(env.CmdFlags.Config, "https://") {
 		if err := env.downloadConfig(env.CmdFlags.Config); err != nil {
 			// make it use default config when download fails
+			env.Error(err)
 			env.CmdFlags.Config = ""
 			return
 		}
@@ -287,6 +289,7 @@ func (env *Shell) resolveConfigPath() {
 	// Cygwin path always needs the full path as we're on Windows but not really.
 	// Doing filepath actions will convert it to a Windows path and break the init script.
 	if env.Platform() == WINDOWS && env.Shell() == "bash" {
+		env.Debug("Cygwin detected, using full path for config")
 		return
 	}
 	configFile := env.CmdFlags.Config


Test patch:
diff --git a/src/engine/migrate_test.go b/src/engine/migrate_test.go
index 000a98c2..06f426ef 100644
--- a/src/engine/migrate_test.go
+++ b/src/engine/migrate_test.go
@@ -427,8 +427,9 @@ func TestMigrateConfig(t *testing.T) {
 	for _, tc := range cases {
 		cfg := &Config{
 			ConsoleTitleTemplate: tc.Template,
+			env:                  &mock.MockedEnvironment{},
 		}
-		cfg.Migrate(&mock.MockedEnvironment{})
+		cfg.Migrate()
 		assert.Equal(t, tc.Expected, cfg.ConsoleTitleTemplate, tc.Case)
 	}
 }