-
Notifications
You must be signed in to change notification settings - Fork 645
Add command to fetch import on current line #1222
Conversation
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. |
@juicemia Its just the linter failing Run |
77f9f7f
to
cb9d11c
Compare
package.json
Outdated
}, | ||
{ | ||
"command": "go.get.import", | ||
"title": "Go: Get Import on Curent Line", |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
src/goGetImport.ts
Outdated
return vscode.window.showErrorMessage('Could not locate Go binaries. Make sure you have Go installed'); | ||
} | ||
|
||
fs.exists(path.join(getCurrentGoPath(), 'src', importPath), (exists) => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
src/goGetImport.ts
Outdated
return; | ||
} | ||
|
||
vscode.window.showInformationMessage(`Successfully fetched package ${importPath}`); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
src/goGetImport.ts
Outdated
const selection = editor.selection; | ||
const selectedText = editor.document.lineAt(selection.active.line).text; | ||
|
||
const importPath = getImportPath(selectedText); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()', () => { |
There was a problem hiding this comment.
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 🥇
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
9ac522f
to
8473a3d
Compare
src/goGetPackage.ts
Outdated
return vscode.window.showErrorMessage('Could not locate Go binaries. Make sure you have Go installed'); | ||
} | ||
|
||
fs.exists(path.join(getCurrentGoPath(), 'src', importPath), (exists) => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
@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 :( |
It's okay. I'll figure it out, no worries. |
`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
Thanks @juicemia! |
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.