Skip to content

Commit 899959b

Browse files
committed
chore: use goimports as a lib instead of CLI
1 parent 3958acc commit 899959b

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/stretchr/testify v1.11.1
1212
golang.org/x/sync v0.17.0
1313
golang.org/x/term v0.35.0
14+
golang.org/x/tools v0.37.0
1415
)
1516

1617
require (
@@ -20,6 +21,5 @@ require (
2021
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
2122
golang.org/x/mod v0.28.0 // indirect
2223
golang.org/x/sys v0.36.0 // indirect
23-
golang.org/x/tools v0.37.0 // indirect
2424
gopkg.in/yaml.v3 v3.0.1 // indirect
2525
)

internal/formatter/executable.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"context"
66
"os/exec"
77
"strings"
8+
9+
"golang.org/x/tools/imports"
810
)
911

1012
const defaultFormatter = "goimports"
@@ -49,6 +51,14 @@ func (e *Executable) Format(ctx context.Context, src []byte) ([]byte, error) {
4951
return src, nil
5052
}
5153

54+
// This is a special case:
55+
// Instead of running the `goimports` as a separate process,
56+
// call the `imports` package directly.
57+
// For compatibility with the existing behavior.
58+
if e.cmd == defaultFormatter && len(e.args) == 0 {
59+
return imports.Process("", src, nil)
60+
}
61+
5262
return e.exec(ctx, src)
5363
}
5464

0 commit comments

Comments
 (0)