Skip to content

Commit

Permalink
Fix yardl generate --watch closing on first error (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
naegelejd authored Mar 9, 2024
1 parent 28d1ca1 commit f890669
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
64 changes: 39 additions & 25 deletions tooling/internal/cmd/generatecommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ func newGenerateCommand() *cobra.Command {
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Run: func(*cobra.Command, []string) {
packageInfo, warnings, err := generateImpl()
if err != nil {
// avoiding returning the error here because
// cobra prefixes the error with "Error: "
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if !flags.watch {
packageInfo, warnings, err := generateImpl()
if err != nil {
// avoiding returning the error here because
// cobra prefixes the error with "Error: "
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

for _, warning := range warnings {
log.Warn().Msg(warning)
}
WriteSuccessfulSummary(packageInfo)
for _, warning := range warnings {
log.Warn().Msg(warning)
}
WriteSuccessfulSummary(packageInfo)

if !flags.watch {
return
}

Expand All @@ -64,16 +64,9 @@ func newGenerateCommand() *cobra.Command {
completedChannel := make(chan error)
go dedupLoop(watcher, completedChannel)

toWatch := []string{"."}
for _, ref := range packageInfo.GetAllReferencedPackages() {
toWatch = append(toWatch, ref.PackageDir())
}

for _, watched := range toWatch {
err = watcher.Add(watched)
if err != nil {
log.Fatal().Err(err).Msg("")
}
err = watcher.Add(".")
if err != nil {
log.Fatal().Err(err).Msg("")
}

err = <-completedChannel
Expand All @@ -90,10 +83,23 @@ func newGenerateCommand() *cobra.Command {

// dedup fsnotify events
func dedupLoop(w *fsnotify.Watcher, completedChannel chan<- error) {
generateInWatchMode()
regenerate := func() {
dirsToWatch := generateInWatchMode()
if dirsToWatch != nil && len(dirsToWatch) > len(w.WatchList()) {
for _, dir := range dirsToWatch {
if err := w.Add(dir); err != nil {
completedChannel <- err
return
}
}
}

}

regenerate()

const waitFor = 5 * time.Millisecond
timer := time.AfterFunc(math.MaxInt64, generateInWatchMode)
timer := time.AfterFunc(math.MaxInt64, regenerate)
timer.Stop()

for {
Expand All @@ -118,7 +124,8 @@ func dedupLoop(w *fsnotify.Watcher, completedChannel chan<- error) {
}
}

func generateInWatchMode() {
// Returns the directories to watch after parsing all package imports, or nil on error
func generateInWatchMode() []string {
defer func() {
if err := recover(); err != nil {
screen.Clear()
Expand All @@ -140,7 +147,14 @@ func generateInWatchMode() {
log.Warn().Msg(warning)
}
WriteSuccessfulSummary(packageInfo)

var dirsToWatch []string
for _, ref := range packageInfo.GetAllReferencedPackages() {
dirsToWatch = append(dirsToWatch, ref.PackageDir())
}
return dirsToWatch
}
return nil
}

func WriteSuccessfulSummary(packageInfo *packaging.PackageInfo) {
Expand Down
2 changes: 1 addition & 1 deletion tooling/pkg/packaging/packageinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ type PythonCodegenOptions struct {
func LoadPackage(dir string) (*PackageInfo, error) {
packageInfo, err := loadPackageVersion(dir)
if err != nil {
return nil, err
return packageInfo, err
}

vdirs, err := collectVersions(packageInfo)
Expand Down

0 comments on commit f890669

Please sign in to comment.