Goimports fixes foo_test.go, but not sibling foo.go

I just started working with goimports directly today, instead of relying on editor (Zed) extensions/gopls. In the editor, an import is correctly resolved, but on the command line it’s not.

I loosely have this file structure, and I want goimports to examine rename.go (package rename) and add an import for package subcmd:

+ subcmd
  - inference.go
  - inference_test.go
  + rename
    - rename.go
    - rename_test.go
  - subcmd.go
  - subcmd_test.go

goimports -d rename.go gives me nothing, but goimports -d rename_test.go does what I’d expect:

diff -u ./subcmd/rename/rename_test.go.orig ./subcmd/rename/rename_test.go
--- ./subcmd/rename/rename_test.go.orig	2024-05-14 04:09:21
+++ ./subcmd/rename/rename_test.go	2024-05-14 04:09:21
@@ -5,7 +5,8 @@

-	)
+	"github.com/zacharysyoung/gocsv/subcmd"

 func TestRename(t *testing.T) {
 	type cg subcmd.ColGroup

rename_test.go is a part of package rename, also.

When I run goimports -d -v rename.go I can see a message about fixing rename.go, but it comes at the very beginning, and from what I can tell, before goimports has found anything to export:

% goimports -d -v ./subcmd/rename/rename.go
2024/05/14 04:10:57.587385 fixImports(filename="./subcmd/rename/rename.go"), abs="/Users/zyoung/develop/go/Personal/gocsv/subcmd/rename/rename.go", srcDir="/Users/zyoung/develop/go/Personal/gocsv/subcmd/rename" ...
2024/05/14 04:10:58.416946 subcmd candidate 1/1: github.com/zacharysyoung/gocsv/subcmd in /Users/zyoung/develop/go/Personal/gocsv/subcmd
2024/05/14 04:10:58.416955 loading exports in dir /Users/zyoung/develop/go/Personal/gocsv/subcmd (seeking package subcmd)
2024/05/14 04:10:58.417430 loaded exports in dir /Users/zyoung/develop/go/Personal/gocsv/subcmd (package subcmd): [{Base0Cols func go1.0} {Base1Cols func go1.0} {Bool const go1.0} {ColGroup type go1.0} {CompareBools func go1.0} {FinalizeCols func go1.0} {Infer func go1.0} {InferCols func go1.0} {InferredType type go1.0} {NewTestdataRunner func go1.0} {Number const go1.0} {Runner type go1.0} {String const go1.0} {TestdataRunner type go1.0} {Time const go1.0} {ToBool func go1.0} {ToNumber func go1.0} {ToTime func go1.0} {UnmarshalFunc type go1.0}]

Can anyone see what’s going on, what I might be doing wrong? Thanks.

I figured it out. My project is in a bad state after manually trying to shift some packages around.

rename.go needs subcmd.ErrNoData, but subcmd doesn’t export that var; it does have errNoData, though. So, need to go through and fix some exports.

1 Like