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

Add command to fetch import on current line #1222

Merged
merged 11 commits into from
Sep 28, 2017

Conversation

juicemia
Copy link
Contributor

@juicemia juicemia commented Sep 18, 2017

This command allows users to just fetch a dependency from their editor. Under the hood, all it does is run go get for the import path on the current line.

NOTE: This is not a replacement for dependency management tools like dep.

@msftclas
Copy link

msftclas commented Sep 18, 2017

CLA assistant check
All CLA requirements met.

@juicemia
Copy link
Contributor Author

I see that Travis is failing but I don't know why. @ramya-rao-a can you see why? I have very little experience with Travis.

@ramya-rao-a
Copy link
Contributor

@juicemia Its just the linter failing
image

Run npm run lint on your branch and you will be able to see the same errors as well

package.json Outdated
},
{
"command": "go.get.import",
"title": "Go: Get Import on Curent Line",
Copy link
Contributor

Choose a reason for hiding this comment

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

How about Go: Import Package? Go: Install Package ? Go: Get Package ? Go: Install Imported Package
(naming is seriously the hardest thing to do)

And the command would be more intuitive if it had the word "package" like go.get.package or go.import.package

Also update the description. It should be clear that we run a go get -u on the package

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think Go: Get Package is the best one out of all of them, because it follows the semantics of using go get. I'll change that now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed

return vscode.window.showErrorMessage('Could not locate Go binaries. Make sure you have Go installed');
}

fs.exists(path.join(getCurrentGoPath(), 'src', importPath), (exists) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

we can skip this check and run go get -u regardless
If the package already exists then it will be updated.
Thoughts?

Copy link
Contributor Author

@juicemia juicemia Sep 23, 2017

Choose a reason for hiding this comment

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

I think it's a good idea, if we make sure to let the user know that their package will be updated. They might not want that for whatever reason.

EDIT: We can ask them to confirm if they want to update.

return;
}

vscode.window.showInformationMessage(`Successfully fetched package ${importPath}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think of showing the progress/success/failure messages in the output pane just like the current command Go: Install/Update Tools

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that's a great idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One thing I just thought of: go get -u doesn't output anything by default unless it fails. For this to be useful, we would have to run go get -u -v. We could also show the output pane only if the command fails. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed

const selection = editor.selection;
const selectedText = editor.document.lineAt(selection.active.line).text;

const importPath = getImportPath(selectedText);
Copy link
Contributor

Choose a reason for hiding this comment

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

If user runs this command on a line thats not an import statement, shouldnt we exit earlier? We know go get would fail then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup absolutely. Forgot that check.

@@ -738,4 +739,19 @@ It returns the number of bytes written and any write error encountered.
assert.ok(false, `error in OpenTextDocument ${err}`);
}).then(() => done(), done);
});

test('getImportPath()', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

may I just say that I absolutely love it that you have added tests 🥇

Hugo Torres added 7 commits September 25, 2017 14:08
This will allow users to easily fetch a dependency without having to
take their attention away from the code they're editing.

microsoft#1189
The plugin only fetches one import so having the file name plural is
misleading to anybody reading the code.

microsoft#1189
This consolidates the functionality found in two commands into one place
so that if it ever needs to be changed or removed it can be done more
easily.
Anybody else who comes along later and reads this is gonna have an
easier time if they only have to keep one name in their head vs. having
to keep two (goGetImport and goGetPackage).

microsoft#1189
If there's no import path to pass to `go get` then there's no point in
spinning up a child process and waiting for it to finish to error out.

This also fixes a bug in `getImportPath` that would cause it to return
invalid input paths for things like comments and function definitions.

microsoft#1189
return vscode.window.showErrorMessage('Could not locate Go binaries. Make sure you have Go installed');
}

fs.exists(path.join(getCurrentGoPath(), 'src', importPath), (exists) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

getCurrentGoPath() can return : or ; separated paths as well. This would fail then

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead you can run go list and pass the importpath to see if the package exists.
Or skip this check altogether.

I really feel that if a user has explicitly run this Go: Get Package command then they would know that this would get the package using go get

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm checking there because it runs go get -u. I can remove the check and make it just run go get without passing -u so that if the package already exists, nothing happens. It's basically the same behavior but it offloads the check to the go tooling.

@ramya-rao-a
Copy link
Contributor

@juicemia I went ahead and rebased your branch from master as there were some conflicts. You might have to delete your local branch and checkout from the remote again.

Sorry for the trouble, I usually work alone, so didnt think of the troubles rebasing might cause others :(

@juicemia
Copy link
Contributor Author

It's okay. I'll figure it out, no worries.

Hugo Torres and others added 3 commits September 27, 2017 20:59
`go get` won't download a package if it's already in `$GOPATH/src`.
There's no point in checking for the package to exist and then
downloading with `-u` because `go get -u` and `go get` are effectively
the same when a package hasn't been downloaded yet.

microsoft#1189
`go get -v` gives users more information on what's going on with the
command. This way a user can tell if the package was even downloaded,
because by default `go get` won't output anything.

`go get -v` will output nothing if the package isn't downloaded because
it already exists. To get around this, an information message is shown
saying that the package wasn't downloaded. This keeps the user informed
because they will be expecting feedback from the command.

In the event of an error (like if there's no internet connection), the
error gets shown to the output pane so the user knows what happened.

microsoft#1189
@ramya-rao-a ramya-rao-a merged commit 2e2e3cd into microsoft:master Sep 28, 2017
@ramya-rao-a
Copy link
Contributor

Thanks @juicemia!

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.

3 participants