Skip to content
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
14 changes: 14 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,17 @@ To remove it and then force the upstream image on Docker Hub to be used again:
```sh
docker rmi sourcegraph/src-batch-change-volume-workspace
```

## Use `debug` build tag to debug batch changes functionality

Since `src batch apply` and `src batch preview` start up a TUI that gets updated repeatedly it's nearly impossible to do printf-debugging by printing debug information - the TUI would hide those or overwrite them.

To help with that you can compile your src binary (or run the tests) with the `debug` build flag:

```
go build -tags debug -o ~/src ./cmd/src
```

This will cause the `./internal/batches/debug.go` file to be included in the build. In that file the `log` default package logger is setup to log to `~/.sourcegraph/src-cli.debug.log`.

That allows you to `tail -f ~/.sourcegraph/src-cli.debug.log` and use `log.Println()` in your code to debug.
1 change: 0 additions & 1 deletion cmd/src/batch_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ type executeBatchSpecOpts struct {
// Sourcegraph, including execution as needed and applying the resulting batch
// spec if specified.
func executeBatchSpec(ctx context.Context, opts executeBatchSpecOpts) error {
batches.DebugOut = opts.out
svc := service.New(&service.Opts{
AllowUnsupported: opts.flags.allowUnsupported,
AllowIgnored: opts.flags.allowIgnored,
Expand Down
26 changes: 22 additions & 4 deletions internal/batches/debug.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
// +build debug

package batches

import (
"github.com/sourcegraph/sourcegraph/lib/output"
"log"
"os"
"path/filepath"
)

// DebugOut can be used to print debug messages in development to the TUI.
// For that it needs to be set to an actual *output.Output.
var DebugOut output.Writer = output.NoopWriter{}
// In builds with the debug flag (i.e. `go build -tags debug -o src ./cmd/src`)
// init() sets up the default logger to log to a file in ~/.sourcegraph.
func init() {
homedir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("getting user home directory: %s", err)
}

fullPath := filepath.Join(homedir, ".sourcegraph", "src-cli.debug.log")

f, err := os.OpenFile(fullPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("setting debug log file failed: %s", err)
}

log.SetOutput(f)
}