Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

add support for the experimental version of gocode running on go/packages #1932

Merged
merged 24 commits into from
Sep 21, 2018
Merged

Conversation

stamblerre
Copy link
Contributor

This PR adds support for installing and switching between the regular version of certain Go tools and the versions that run on-top of go/packages (which works with Modules).

If the user is in a directory with a go.mod file, gocode-gomod will be executed, whereas if the user is in a normal $GOPATH workspace, the normal version will be executed. This allows users to test out their usual Go tools with Modules, while avoiding any bugs that may still be present, since these tools are still quite new.

For now, only gocode switches between gocode-gomod and gocode. godef-gomod can be downloaded, but it's not yet added in this change. Ideally, the version of the tool to run should be decided in a function like getBinPath, but I wanted to test one tool before using that approach.

cp.execFile(goRuntimePath, ['get', '-u', '-v', allTools[tool]], { env: envForTools }, (err, stdout, stderr) => {
let args = ['get', '-u', '-v'];
if (tool.endsWith('-gomod')) {
args.push('-d');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we choosing to not install and instead run a go build later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only "go build" has the -o flag that allows us to rename the binary to "gocode-gomod" instead of gocode, so I don't think there is a way to "go get" and rename the tool with one command.

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Sep 18, 2018

@stamblerre Thanks for the PR!

I have refactored the code in goSuggest.ts a bit and fixed linting errors that were breaking the build.
Also updated to include gocode-gomod in the list of tools to install only if Go version is 1.11. I am guessing we don't need this for Go 1.10.

Will test it out tomorrow and merge it.

@stamblerre
Copy link
Contributor Author

It seems like the broken build is a result of an error in "fillStruct", which I don't think is related to this change.

@stamblerre
Copy link
Contributor Author

@ramya-rao-a - just wanted to follow up, does this change seem ready to you?

@ramya-rao-a ramya-rao-a merged commit fda88f0 into microsoft:master Sep 21, 2018
@ramya-rao-a
Copy link
Contributor

Yes, this looks good.

I wanted to make general changes on how to identify whether mod is supported and cache the results as well, but I can do that in the master branch.

I'll have a test build ready soon.

Thanks!

fatih added a commit to fatih/vim-go that referenced this pull request Sep 21, 2018
@fatih
Copy link

fatih commented Sep 21, 2018

I'm currently working on adding gocode-gomod as well, however, it only returns the candidates 20% of the time. I'm not sure what's going. I don't have much time this week (took a week off), but here is a WIP PR for vim-go: fatih/vim-go#1988

I'll check it again once I'm back in town.

@stamblerre
Copy link
Contributor Author

Thanks for merging @ramya-rao-a. I'll probably have a similar PR soon for you for godef. Let me know if there are any problems with it.

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Sep 22, 2018

@stamblerre

I am seeing similar things as @fatih.

I just have a simple file with just 1 non std lib package dependency. It worked well for the past hour, but now I get "null" as the output from gocode-gomod all the time. It doesn't return results for std lib packages either at the moment

To test it out in VS Code:

@stamblerre
Copy link
Contributor Author

I'm not sure if this will help fully, but I just pushed stamblerre/gocode@b960ec2, which should allow regular gocode and gocode-gomod run simultaneously. I'll try to keep using it though to see if I can find more problems.

@ramya-rao-a
Copy link
Contributor

@stamblerre Anything we can do to help debug the issue where gocode returns the string "null"?

@stamblerre
Copy link
Contributor Author

I haven't been able to reproduce that yet, but I am using gocode-gomod right now and trying to see if I can get it to happen. If you run "gocode-gomod close" and then "gocode-gomod -s -debug", and then try a completion and see what it prints out, that might give some useful information. Thanks!

@ramya-rao-a
Copy link
Contributor

@stamblerre Below is the output from gocode-gomod -s -debug. I don't see anything that useful. It straight up says no candidates

2018/09/24 18:20:13 Got autocompletion request for '/Users/ramyar/Documents/go-modules/hello/hello.go'
2018/09/24 18:20:13 Cursor at: 92
2018/09/24 18:20:13 -------------------------------------------------------
package main

import (
        "rsc.io/quote"
        "fmt"
)

func main() {
        fmt.Println("hello")
        quote.#
}
2018/09/24 18:20:13 -------------------------------------------------------
2018/09/24 18:20:13 Elapsed duration: 91.238433ms
2018/09/24 18:20:13 Offset: 0
2018/09/24 18:20:13 Number of candidates found: 0
2018/09/24 18:20:13 Candidates are:
2018/09/24 18:20:13 =======================================================
2018/09/24 18:20:13 Got autocompletion request for '/Users/ramyar/Documents/go-modules/hello/hello.go'
2018/09/24 18:20:13 Cursor at: 114
2018/09/24 18:20:13 -------------------------------------------------------
package main
import "rsc.io/quote"

import (
        "rsc.io/quote"
        "fmt"
)

func main() {
        fmt.Println("hello")
        quote.#
}
2018/09/24 18:20:13 -------------------------------------------------------
2018/09/24 18:20:13 Elapsed duration: 90.077903ms
2018/09/24 18:20:13 Offset: 0
2018/09/24 18:20:13 Number of candidates found: 0
2018/09/24 18:20:13 Candidates are:
2018/09/24 18:20:13 =======================================================

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Sep 25, 2018

I know the repro steps for the failure now.

In the above example, I am using the quote package.

  1. Say I used quote.Hello() and save the file. Subsequent quote. will give me completions.
  2. Remove all quote related code and the import, save the file.
  3. Add the quote package back to the import block. Don't save. Type quote. No completions.

On every file save, we run go build. This might be updating the module cache?

@fatih Do you see something similar?

@stamblerre
Copy link
Contributor Author

Oh, so is the only problem you're seeing related to completing with unsaved imports?

@ramya-rao-a
Copy link
Contributor

Yes, looks like that

@stamblerre
Copy link
Contributor Author

Ok, I will try to investigate this error and see if I can resolve it. Though this doesn't seem like a huge deal to me--the pretty simple solution is to add the import back.

if (stderr.indexOf('unexpected directory layout:') > -1) {
outputChannel.appendLine(`Installing ${tool} failed with error "unexpected directory layout". Retrying...`);
cp.execFile(goRuntimePath, ['get', '-u', '-v', allTools[tool]], { env: envForTools }, callback);
cp.execFile(goRuntimePath, args, { env: envForTools }, callback);
} else if (tool.endsWith('-gomod')) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll never pass this in my tests. see #2024

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dexus Did you try the latest from master?

You need to update either of gocode-gomod or godef-gomod via the Go: Install/Update Tools command to hit this else block

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in #2024 already pointed out I fix it myself with a copy. Would be nice if the current fix will be published asap. That works for me.

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

Successfully merging this pull request may close these issues.

4 participants