beads/cmd/bd/prompt.go
matt wilkie 9ab8a79534
fix: auto-import issues.jsonl on empty embedded database after upgrade (#2999)
* fix: auto-import issues.jsonl when embedded database is empty on upgrade (GH#2994)

When upgrading from pre-0.56 (which stored data in .beads/dolt/) to 1.0+
(which uses .beads/embeddeddolt/), the new embedded database starts empty
while the git-tracked issues.jsonl still has all the user's data. This
caused users to see "No issues found" after upgrading.

After the store is created in PersistentPreRun, check if:
1. The database has 0 issues (newly created/empty)
2. A non-empty issues.jsonl exists in the beads directory

When both conditions are true, auto-import from issues.jsonl using the
existing importFromLocalJSONLFull logic, then commit to Dolt history.
The import is best-effort — failures are logged but don't block the store.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: skip auto-import when running bd import; fix macOS context race

Two CI failures on PR #2999:

1. TestEmbeddedImport/from_default_path and dry_run failed because
   maybeAutoImportJSONL() fired in PersistentPreRun before the explicit
   bd import command ran. When the test writes .beads/issues.jsonl to an
   empty DB and then calls bd import, the auto-import triggered first and
   consumed the data, breaking the import command's expected output and
   the dry_run isolation check. Fix: skip auto-import when cmd.Name() ==
   "import".

2. TestReadLineWithContextCanceled failed on macOS with "expected
   context.Canceled, got EOF". When stub.cancel() and pw.Close() fire in
   rapid succession, the goroutine's EOF result could win the select race
   against sigCtx.Done(). Fix: add a non-blocking inner select to prefer
   the context cancellation when both channels are simultaneously ready.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 14:47:31 -07:00

68 lines
1.5 KiB
Go

package main
import (
"bufio"
"context"
"errors"
"io"
"os"
"os/signal"
"syscall"
)
// notifyContext is overridden in tests to avoid sending real signals.
var notifyContext = signal.NotifyContext
const exitCodeCanceled = 130
// readLineWithContext returns a line from reader or ctx.Err() if canceled.
// If closer is provided, it is closed on cancellation to unblock the read.
func readLineWithContext(ctx context.Context, reader *bufio.Reader, closer io.Closer) (string, error) {
if ctx == nil {
ctx = context.Background()
}
sigCtx, stop := notifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
if err := sigCtx.Err(); err != nil {
return "", err
}
type result struct {
line string
err error
}
resultCh := make(chan result, 1)
go func() {
line, err := reader.ReadString('\n')
resultCh <- result{line: line, err: err}
}()
select {
case <-sigCtx.Done():
if closer != nil {
_ = closer.Close() // Best effort cleanup of readline
}
return "", sigCtx.Err()
case res := <-resultCh:
// When both channels are ready simultaneously (e.g., context canceled
// at the same time the pipe closes with EOF), prefer the cancellation
// error. This prevents a race on macOS where EOF wins the select.
select {
case <-sigCtx.Done():
return "", sigCtx.Err()
default:
}
return res.line, res.err
}
}
func isCanceled(err error) bool {
return errors.Is(err, context.Canceled)
}
func exitCanceled() {
os.Exit(exitCodeCanceled)
}