fix: prevent nil ptr

Carlos Alexandro Becker created

Change summary

go.mod                    |  3 ++-
internal/config/config.go |  2 +-
internal/config/init.go   | 19 ++++++++++---------
3 files changed, 13 insertions(+), 11 deletions(-)

Detailed changes

go.mod 🔗

@@ -42,11 +42,12 @@ require (
 	github.com/tidwall/sjson v1.2.5
 	github.com/u-root/u-root v0.14.1-0.20250722142936-bf4e78a90dfc
 	github.com/zeebo/xxh3 v1.0.2
-	golang.org/x/exp v0.0.0-20250305212735-054e65f0b394
 	gopkg.in/natefinch/lumberjack.v2 v2.2.1
 	mvdan.cc/sh/v3 v3.11.0
 )
 
+require golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
+
 require (
 	cloud.google.com/go v0.116.0 // indirect
 	cloud.google.com/go/auth v0.13.0 // indirect

internal/config/config.go 🔗

@@ -3,6 +3,7 @@ package config
 import (
 	"context"
 	"fmt"
+	"log/slog"
 	"net/http"
 	"os"
 	"slices"
@@ -13,7 +14,6 @@ import (
 	"github.com/charmbracelet/crush/internal/csync"
 	"github.com/charmbracelet/crush/internal/env"
 	"github.com/tidwall/sjson"
-	"golang.org/x/exp/slog"
 )
 
 const (

internal/config/init.go 🔗

@@ -6,12 +6,9 @@ import (
 	"path/filepath"
 	"strings"
 	"sync"
-	"sync/atomic"
 )
 
-const (
-	InitFlagFilename = "init"
-)
+const InitFlagFilename = "init"
 
 type ProjectInitFlag struct {
 	Initialized bool `json:"initialized"`
@@ -19,25 +16,29 @@ type ProjectInitFlag struct {
 
 // TODO: we need to remove the global config instance keeping it now just until everything is migrated
 var (
-	instance atomic.Pointer[Config]
+	instance *Config
 	cwd      string
-	once     sync.Once // Ensures the initialization happens only once
+	once     sync.Once
+	wg       sync.WaitGroup
 )
 
 func Init(workingDir string, debug bool) (*Config, error) {
 	var err error
+	wg.Add(1)
 	once.Do(func() {
 		cwd = workingDir
 		var cfg *Config
 		cfg, err = Load(cwd, debug)
-		instance.Store(cfg)
+		instance = cfg
+		wg.Done()
 	})
 
-	return instance.Load(), err
+	return instance, err
 }
 
 func Get() *Config {
-	return instance.Load()
+	wg.Wait()
+	return instance
 }
 
 func ProjectNeedsInitialization() (bool, error) {