From 069fe18b9d1a8a70def534ddf8ca2f222026a455 Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Wed, 6 Sep 2023 03:33:22 -0600 Subject: [PATCH] Move watcher init to doWatcherLoop and implement -reloaddirs (#2871) * remove random print statement * move watcher into loop and implement reloaddirs * Fixed -reloaddirs for issue #2829 --------- Co-authored-by: Lea Anthony --- v2/cmd/wails/internal/dev/dev.go | 46 ++++++++++++++++++---------- v2/cmd/wails/internal/dev/watcher.go | 1 - website/src/pages/changelog.mdx | 1 + 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/v2/cmd/wails/internal/dev/dev.go b/v2/cmd/wails/internal/dev/dev.go index 6ec801119..80d7a6d87 100644 --- a/v2/cmd/wails/internal/dev/dev.go +++ b/v2/cmd/wails/internal/dev/dev.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "log" "net/http" "net/url" "os" @@ -138,20 +139,6 @@ func Application(f *flags.Dev, logger *clilogger.CLILogger) error { } } - // create the project files watcher - watcher, err := initialiseWatcher(cwd) - if err != nil { - return err - } - - defer func(watcher *fsnotify.Watcher) { - err := watcher.Close() - if err != nil { - logger.Fatal(err.Error()) - } - }(watcher) - - logutils.LogGreen("Watching (sub)/directory: %s", cwd) logutils.LogGreen("Using DevServer URL: %s", f.DevServerURL()) if f.FrontendDevServerURL != "" { logutils.LogGreen("Using Frontend DevServer URL: %s", f.FrontendDevServerURL) @@ -165,7 +152,10 @@ func Application(f *flags.Dev, logger *clilogger.CLILogger) error { }() // Watch for changes and trigger restartApp() - debugBinaryProcess = doWatcherLoop(buildOptions, debugBinaryProcess, f, watcher, exitCodeChannel, quitChannel, f.DevServerURL(), legacyUseDevServerInsteadofCustomScheme) + debugBinaryProcess, err = doWatcherLoop(cwd, buildOptions, debugBinaryProcess, f, exitCodeChannel, quitChannel, f.DevServerURL(), legacyUseDevServerInsteadofCustomScheme) + if err != nil { + return err + } // Kill the current program if running and remove dev binary if err := killProcessAndCleanupBinary(debugBinaryProcess, appBinary); err != nil { @@ -337,7 +327,23 @@ func restartApp(buildOptions *build.Options, debugBinaryProcess *process.Process } // doWatcherLoop is the main watch loop that runs while dev is active -func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Process, f *flags.Dev, watcher *fsnotify.Watcher, exitCodeChannel chan int, quitChannel chan os.Signal, devServerURL *url.URL, legacyUseDevServerInsteadofCustomScheme bool) *process.Process { +func doWatcherLoop(cwd string, buildOptions *build.Options, debugBinaryProcess *process.Process, f *flags.Dev, exitCodeChannel chan int, quitChannel chan os.Signal, devServerURL *url.URL, legacyUseDevServerInsteadofCustomScheme bool) (*process.Process, error) { + // create the project files watcher + watcher, err := initialiseWatcher(cwd) + if err != nil { + logutils.LogRed("Unable to create filesystem watcher. Reloads will not occur.") + return nil, err + } + + defer func(watcher *fsnotify.Watcher) { + err := watcher.Close() + if err != nil { + log.Fatal(err.Error()) + } + }(watcher) + + logutils.LogGreen("Watching (sub)/directory: %s", cwd) + // Main Loop var extensionsThatTriggerARebuild = sliceToMap(strings.Split(f.Extensions, ",")) var dirsThatTriggerAReload []string @@ -351,6 +357,12 @@ func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Proc continue } dirsThatTriggerAReload = append(dirsThatTriggerAReload, thePath) + err = watcher.Add(thePath) + if err != nil { + logutils.LogRed("Unable to watch path: %s due to error %v", thePath, err) + } else { + logutils.LogGreen("Watching (sub)/directory: %s", thePath) + } } quit := false @@ -499,7 +511,7 @@ func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Proc quit = true } } - return debugBinaryProcess + return debugBinaryProcess, nil } func joinPath(url *url.URL, subPath string) string { diff --git a/v2/cmd/wails/internal/dev/watcher.go b/v2/cmd/wails/internal/dev/watcher.go index 19caf0df8..1406f0c1e 100644 --- a/v2/cmd/wails/internal/dev/watcher.go +++ b/v2/cmd/wails/internal/dev/watcher.go @@ -38,7 +38,6 @@ func initialiseWatcher(cwd string) (*fsnotify.Watcher, error) { if err != nil { return nil, err } - println("watching: " + dir) } return watcher, nil } diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index 755b2d99a..fb537014c 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed `SetBackgroundColour` so it sets the window's background color to reduce resize flickering on Linux. Added by @lyimmi in [PR](https://github.com/wailsapp/wails/pull/2853) - Fixed disable window resize option and wrong initial window size when its enabled. Added by @lyimmi in [PR](https://github.com/wailsapp/wails/pull/2863) - Fixed build hook command parsing. Added by @smac89 in [PR](https://github.com/wailsapp/wails/pull/2836) +- Fixed `-reloaddir` flag to watch additional directories (non-recursively). [@haukened](https://github.com/haukened) in [PR #2871](https://github.com/wailsapp/wails/pull/2871) - Fixed support for Go 1.21 `go.mod` files. Fixed by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2876) ### Added