Skip to content

goimports can't tell the complete filename when used with go-mode.el #146

@bradfitz

Description

@bradfitz

Most editors run goimports on the file directly.

go-mode.el's gofmt function on the other hand writes the buffer to a tempfile and then runs goimports on the temp filename. This is problematic because goimports needs to look around the user's $GOPATH and figure out which things can be used and not used (for "internal" and "vendor" directories). This is what motivated the addition of the -srcdir flag to goimports.

Unfortunately, -srcdir isn't enough. I also need to know the filename for some in-development feature additions to goimports. (see https://golang.org/cl/23444 from @briantkennedy)

I don't want to add new flags to goimprots.

I propose to change goimports to let -srcdir mean either a complete filename, or a directory. goimports can detect what it is.

Then the change I propose to go-mode.el is:

diff --git a/go-mode.el b/go-mode.el
index 8acc0e1..9705e76 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -1152,7 +1152,7 @@ with goflymake \(see URL `https://github.com/dougm/goflymake'), gocode
           (when (and (gofmt--is-goimports-p) buffer-file-name)
             (setq our-gofmt-args
                   (append our-gofmt-args
-                          (list "-srcdir" (file-name-directory (file-truename buffer-file-name))))))
+                          (list "-srcdir" (file-truename buffer-file-name))))) ; see https://github.com/dominikh/go-mode.el/issues/146
           (setq our-gofmt-args (append our-gofmt-args
                                        gofmt-args
                                        (list "-w" tmpfile)))

/cc @adonovan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions