Skip to content
This repository was archived by the owner on Oct 9, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions serve/config/dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func CreateDotEnv(workingDirectory string, onChange func(variables map[string]*s
slog.Info(fmt.Sprintf("Detected .env file at %v. Reading variables and adding watch.", configEnvPath))
env = parseDotEnv(configEnvPath)
} else {
slog.Info(fmt.Sprintf("Detected .env file at %v. Reading variables.", configEnvPath))
env = parseDotEnv(filepath.Join(workingDirectory, ".env"))
localEnv := filepath.Join(workingDirectory, ".env")
slog.Info(fmt.Sprintf("Detected .env file at %v. Reading variables.", localEnv))
env = parseDotEnv(localEnv)
}

instance := DotEnv{
Expand Down
7 changes: 7 additions & 0 deletions serve/config/filewatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"log/slog"
"path"
"time"

"github.com/fsnotify/fsnotify"
)
Expand Down Expand Up @@ -44,6 +45,12 @@ func CreateFileWatcher() *FileWatcher {
if ok {
for _, watchable := range watchables {
if watchable.Name() == name {
// When e.g. using os.WriteFile, the truncation already triggers
// a change event, which results in the file being empty when
// calling HandleChange.
// Due to this, we wait for a millisecond, which should be enough for
// the write operation to finish.
time.Sleep(time.Millisecond)
watchable.HandleChange()
}
return
Expand Down
15 changes: 2 additions & 13 deletions serve/config/filewatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,12 @@ func TestShouldUpdateDotEnvOnChange(t *testing.T) {

test.AssertEqual(t, len(testEnv.env), 3)

f, err := os.OpenFile(envFilePath, os.O_WRONLY, 0666)
err = os.WriteFile(envFilePath, []byte("TEST = example"), 0666)
if err != nil {
t.Fatalf("failed to write to file: %s", err)
}

f.Sync()
time.Sleep(time.Millisecond)
f.WriteString("TEST = example")
f.Sync()
f.Close()

// This test is flaky on GitHub Actions, so we do this workaround
counter := 0
for counter < 200 && len(testEnv.env) != 1 {
time.Sleep(time.Millisecond * 50)
counter++
}
time.Sleep(time.Millisecond * 50)

test.AssertEqual(t, len(testEnv.env), 1)
test.AssertEqual(t, readValue(t, testEnv.env, "TEST"), "example")
Expand Down