You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The GOPATH environment variable can be a colon-separated list of paths, which makes the mapping from filename to go package non-trivial (furthermore, in one of my projects where this is an issue, one GOPATH element is a prefix of another (github.com/cockroachdb/cockroach). Ideally the longest match should be used instead of the first match, although I haven't run into a case where this matters). flycheck-go-package-name needs to be augmented to support this; here is my attempt:
(defun flycheck-go-package-name (&optional file-name gopath)
"Determine the package name for FILE-NAME and GOPATH.
FILE-NAME defaults to `buffer-file-name'. GOPATH defaults to
$GOPATH.
Return the package name for FILE-NAME, or nil if FILE-NAME is not
part of any package or if GOPATH is nil."
(-when-let* ((gopath (or gopath (getenv "GOPATH")))
(file-name (or file-name (buffer-file-name))))
(--min-by (> (length it) (length other))
(-non-nil
(--map
(let ((gosrc (file-name-as-directory (expand-file-name "src/" it)))
(file-name (expand-file-name file-name)))
(when (string-prefix-p gosrc file-name)
;; The file is part of a package, so determine the package name, as
;; relative file name to the GO source directory
(directory-file-name ; Remove trailing /
(file-relative-name (file-name-directory file-name) gosrc))))
(split-string gopath ":"))))))
Alternately, we could just call out to the go tool: go list $DIR prints the package name defined by the given directory. The error-filter also needs to be updated to reassemble the path based on the prefix that was chosen by flycheck-go-package-name.
See also kisielk/errcheck#45, which would hopefully make all of this obsolete by making errcheck work more like the other go tools.
The text was updated successfully, but these errors were encountered:
@bdarnell Feel free to open a pull request. I won't change the corresponding code by myself, though: The current implementation is just a best guess, which tries to handle the simple case well. I don't know enough about Go—nothing actually—to properly handle $GOPATH.
The -abspath flag was introduced in commit
8515d34a8746fc0ab1c652594faaeed138694c49 on the 25th of August, 2015. It
causes errcheck to print absolute file paths.
This change simplifies the code and makes it work correctly with
multiple GOPATH entries.
ClosesGH-906 and fixesGH-580.
The GOPATH environment variable can be a colon-separated list of paths, which makes the mapping from filename to go package non-trivial (furthermore, in one of my projects where this is an issue, one GOPATH element is a prefix of another (github.com/cockroachdb/cockroach). Ideally the longest match should be used instead of the first match, although I haven't run into a case where this matters).
flycheck-go-package-name
needs to be augmented to support this; here is my attempt:Alternately, we could just call out to the go tool:
go list $DIR
prints the package name defined by the given directory. Theerror-filter
also needs to be updated to reassemble the path based on the prefix that was chosen byflycheck-go-package-name
.See also kisielk/errcheck#45, which would hopefully make all of this obsolete by making errcheck work more like the other go tools.
The text was updated successfully, but these errors were encountered: