Skip to content

Commit 7b5d15f

Browse files
author
Bruce Hill
committed
Remove --lint-first flag and always do a blocking loop on linter issues
1 parent 965a0c9 commit 7b5d15f

File tree

3 files changed

+75
-48
lines changed

3 files changed

+75
-48
lines changed

pkg/cmd/dev.go

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,16 @@ var devCommand = cli.Command{
271271
Value: false,
272272
Usage: "Only check for diagnostic errors without running a full build.",
273273
},
274-
&cli.BoolFlag{
275-
Name: "lint-first",
276-
Value: false,
277-
Usage: "Run linting before starting the build process.",
278-
},
279274
},
280275
Action: runPreview,
281276
}
282277

283278
func runPreview(ctx context.Context, cmd *cli.Command) error {
284279
cc := getAPICommandContext(cmd)
285280

281+
openapiSpecPath := cmd.String("openapi-spec")
282+
stainlessConfigPath := cmd.String("stainless-config")
283+
286284
// If only linting is requested, run in lint-only mode
287285
if cmd.Bool("lint") {
288286
return runLintLoop(ctx, cmd)
@@ -366,18 +364,29 @@ func runPreview(ctx context.Context, cmd *cli.Command) error {
366364

367365
// Phase 3: Start build and monitor progress in a loop
368366
for {
369-
// Check if we should run linting before building
370-
if cmd.Bool("lint-first") {
371-
diagnostics, err := getPreviewDiagnosticsJSON(ctx, cmd)
367+
// Keep checking diagnostics until they're all fixed
368+
for {
369+
diagnostics, err := getPreviewDiagnostics(ctx, cmd)
372370
if err != nil {
373371
if errors.Is(err, ErrUserCancelled) {
374372
return nil
375373
}
376374
return err
377375
}
378-
jsonObj := gjson.Parse(diagnostics.Raw)
379-
transform := cmd.String("transform")
380-
ShowJSON("Linting Results", jsonObj, cmd.String("format"), transform)
376+
377+
if len(diagnostics) > 0 {
378+
fmt.Println(ViewDiagnosticsPrint(diagnostics, 10))
379+
}
380+
381+
if hasBlockingDiagnostic(diagnostics) {
382+
fmt.Println("\nDiagnostic checks will re-run once you edit your configuration files...")
383+
if err := waitTillAnyFileChanged(ctx, []string{openapiSpecPath, stainlessConfigPath}); err != nil {
384+
return err
385+
}
386+
continue
387+
} else {
388+
break
389+
}
381390
}
382391

383392
// Start the build process
@@ -401,16 +410,6 @@ func runLintLoop(ctx context.Context, cmd *cli.Command) error {
401410
openapiSpecPath := cmd.String("openapi-spec")
402411
stainlessConfigPath := cmd.String("stainless-config")
403412

404-
var openapiSpecLastModified, stainlessConfigLastModified time.Time
405-
406-
if openapiSpecStat, err := os.Stat(openapiSpecPath); err == nil {
407-
openapiSpecLastModified = openapiSpecStat.ModTime()
408-
}
409-
410-
if stainlessConfigStat, err := os.Stat(stainlessConfigPath); err == nil {
411-
stainlessConfigLastModified = stainlessConfigStat.ModTime()
412-
}
413-
414413
for {
415414
diagnostics, err := getPreviewDiagnosticsJSON(ctx, cmd)
416415
if err != nil {
@@ -426,31 +425,37 @@ func runLintLoop(ctx context.Context, cmd *cli.Command) error {
426425
break
427426
}
428427

429-
for {
430-
time.Sleep(500 * time.Millisecond)
428+
if err := waitTillAnyFileChanged(ctx, []string{openapiSpecPath, stainlessConfigPath}); err != nil {
429+
return err
430+
}
431+
}
432+
return nil
433+
}
431434

432-
if openapiSpecStat, err := os.Stat(openapiSpecPath); err == nil {
433-
if openapiSpecStat.ModTime().After(openapiSpecLastModified) {
434-
openapiSpecLastModified = openapiSpecStat.ModTime()
435-
break // File changed, run linting again
436-
}
437-
}
435+
func waitTillAnyFileChanged(ctx context.Context, files []string) error {
436+
lastModified := make(map[string]time.Time)
437+
for _, file := range files {
438+
if stat, err := os.Stat(file); err == nil {
439+
lastModified[file] = stat.ModTime()
440+
}
441+
}
438442

439-
// Check Stainless config file
440-
if stainlessConfigStat, err := os.Stat(stainlessConfigPath); err == nil {
441-
if stainlessConfigStat.ModTime().After(stainlessConfigLastModified) {
442-
stainlessConfigLastModified = stainlessConfigStat.ModTime()
443-
break // File changed, run linting again
444-
}
445-
}
443+
for {
444+
time.Sleep(500 * time.Millisecond)
446445

447-
// Check for cancellation
448-
select {
449-
case <-ctx.Done():
450-
return ctx.Err()
451-
default:
446+
for _, file := range files {
447+
if stat, err := os.Stat(file); err == nil {
448+
if stat.ModTime().After(lastModified[file]) {
449+
return nil
450+
}
452451
}
453452
}
453+
// Check for cancellation
454+
select {
455+
case <-ctx.Done():
456+
return ctx.Err()
457+
default:
458+
}
454459
}
455460
return nil
456461
}
@@ -547,7 +552,7 @@ func getPreviewDiagnosticsJSON(ctx context.Context, cmd *cli.Command) (*gjson.Re
547552
return nil, err
548553
}
549554
json := gjson.ParseBytes(result)
550-
diagnostics := json.Get("spec.diagnostics.@values.@flatten.#.{code,level,ignored,endpoint,message,more,language,location,stainlessPath,oasRef,configRef}")
555+
diagnostics := json.Get("spec.diagnostics.@values.@flatten")
551556
return &diagnostics, nil
552557
}
553558

@@ -562,3 +567,19 @@ func getPreviewDiagnostics(ctx context.Context, cmd *cli.Command) ([]stainless.B
562567
}
563568
return diagnostics, err
564569
}
570+
571+
func hasBlockingDiagnostic(diagnostics []stainless.BuildDiagnostic) bool {
572+
for _, d := range diagnostics {
573+
if !d.Ignored {
574+
switch d.Level {
575+
case stainless.BuildDiagnosticLevelFatal:
576+
case stainless.BuildDiagnosticLevelError:
577+
case stainless.BuildDiagnosticLevelWarning:
578+
return true
579+
case stainless.BuildDiagnosticLevelNote:
580+
continue
581+
}
582+
}
583+
}
584+
return false
585+
}

pkg/cmd/dev_view.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ var parts = []struct {
108108
if m.diagnostics == nil {
109109
s.WriteString(SProperty(0, "diagnostics", "waiting for build to finish"))
110110
} else {
111-
s.WriteString(ViewDiagnosticsPrint(m.diagnostics))
111+
s.WriteString(ViewDiagnosticsPrint(m.diagnostics, 10))
112112
}
113113
},
114114
},
@@ -325,7 +325,7 @@ func countDiagnosticsBySeverity(diagnostics []stainless.BuildDiagnostic) (fatal,
325325
return
326326
}
327327

328-
func ViewDiagnosticsPrint(diagnostics []stainless.BuildDiagnostic) string {
328+
func ViewDiagnosticsPrint(diagnostics []stainless.BuildDiagnostic, maxDiagnostics int) string {
329329
var s strings.Builder
330330

331331
if len(diagnostics) > 0 {
@@ -353,14 +353,13 @@ func ViewDiagnosticsPrint(diagnostics []stainless.BuildDiagnostic) string {
353353
}
354354

355355
var sub strings.Builder
356-
maxDiagnostics := 10
357356

358-
if len(diagnostics) > maxDiagnostics {
357+
if maxDiagnostics >= 0 && len(diagnostics) > maxDiagnostics {
359358
sub.WriteString(fmt.Sprintf("Showing first %d of %d diagnostics:\n", maxDiagnostics, len(diagnostics)))
360359
}
361360

362361
for i, diag := range diagnostics {
363-
if i >= maxDiagnostics {
362+
if maxDiagnostics >= 0 && i >= maxDiagnostics {
364363
break
365364
}
366365

pkg/cmd/lint.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ func runLinter(ctx context.Context, cmd *cli.Command) error {
7272
jsonObj := gjson.Parse(string(result)).Get(transform)
7373
var diagnostics []stainless.BuildDiagnostic
7474
json.Unmarshal([]byte(jsonObj.Raw), &diagnostics)
75-
fmt.Println(ViewDiagnosticsPrint(diagnostics))
75+
if cmd.IsSet("format") {
76+
if err := ShowJSON("Diagnostics", jsonObj, cmd.String("format"), ""); err != nil {
77+
return err
78+
}
79+
} else {
80+
fmt.Println(ViewDiagnosticsPrint(diagnostics, -1))
81+
}
82+
7683
for _, d := range diagnostics {
7784
if !d.Ignored {
7885
switch d.Level {

0 commit comments

Comments
 (0)