Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go-projectile-derive-gopath false positives #15

Open
peter-mogensen opened this issue Dec 1, 2016 · 2 comments
Open

go-projectile-derive-gopath false positives #15

peter-mogensen opened this issue Dec 1, 2016 · 2 comments

Comments

@peter-mogensen
Copy link

About half of my projects don't use a global GOPATH, but use GOPATH=pwd to contain everything in one directory.
go-projectile-set-gopath works OK for that purpose initially, but when switching from a buffer PROJ-ROOT1 (which has a main.go) to another buffer PROJ-ROOT2/main.go go-projectile-derive-gopath seems to fail.

The problem is that "rel" correctly ends up being "main.go", but since a main.go is also to be found in PROJ-ROOT1 (locate-file rel (split-string path path-separator t)) evaluated to "t" which causes GOPATH to be the old PROJ-ROOT1

@peter-mogensen
Copy link
Author

peter-mogensen commented Dec 2, 2016

A suggestion for an alternative implementation:

(defun go-projectile-better-derive-gopath (&optional path)
  "Attempt to derive GOPATH for the current buffer.
PATH defaults to GOPATH via getenv, used to determine if buffer is in current GOPATH already."
  (let* (
         (path (or path (getenv "GOPATH")))
         (buf (file-truename (or buffer-file-name default-directory)))
         (dir (locate-dominating-file buf "src"))
         )
    (message "P %S B %S D %S" path buf dir)
    (if dir
        (let* (
               (bufl (split-string dir "/" t))
               (pathl (split-string path "/" t))
               )
          (message "P %S B %S" pathl bufl)
          (while (string= (car pathl) (car bufl))
            (progn
              (setq pathl (cdr pathl))
              (setq bufl (cdr bufl))
              )
            )
          (if pathl
              (file-truename (directory-file-name dir))
            path
            )
          )
      )
    )
  )

@peter-mogensen
Copy link
Author

Ahh... no ... this is more complex.
You also don't want to derive a GOPATH if the file is in GOROOT. Else you'll switch GOPATH when using tags navigation to move to a definition of a stdlib function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant