internal/lsp: fix go generate command for subdirs

The go generate command was not honoring its dir argument, instead just
running in the workspace root.

Fixes golang/go#41566

Change-Id: I5fb96a765cf4fb2572e2a8a2e560e02f0760023f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/256818
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
diff --git a/gopls/internal/regtest/generate_test.go b/gopls/internal/regtest/generate_test.go
index 6dca166..87e64df 100644
--- a/gopls/internal/regtest/generate_test.go
+++ b/gopls/internal/regtest/generate_test.go
@@ -20,7 +20,7 @@
 module fake.test
 
 go 1.14
--- generate.go --
+-- lib/generate.go --
 // +build ignore
 
 package main
@@ -30,7 +30,7 @@
 func main() {
 	ioutil.WriteFile("generated.go", []byte("package lib\n\nconst answer = 42"), 0644)
 }
--- lib.go --
+-- lib/lib.go --
 package lib
 
 func GetAnswer() int {
@@ -42,13 +42,13 @@
 
 	runner.Run(t, generatedWorkspace, func(t *testing.T, env *Env) {
 		env.Await(
-			env.DiagnosticAtRegexp("lib.go", "answer"),
+			env.DiagnosticAtRegexp("lib/lib.go", "answer"),
 		)
-		env.RunGenerate(".")
+		env.RunGenerate("./lib")
 		env.Await(
 			OnceMet(
 				CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChangeWatchedFiles), 1),
-				EmptyDiagnostics("lib.go")),
+				EmptyDiagnostics("lib/lib.go")),
 		)
 	})
 }
diff --git a/internal/lsp/command.go b/internal/lsp/command.go
index 06ade14..2f0f4d5 100644
--- a/internal/lsp/command.go
+++ b/internal/lsp/command.go
@@ -347,9 +347,11 @@
 
 	er := &eventWriter{ctx: ctx, operation: "generate"}
 	args := []string{"-x"}
+	dir := uri.Filename()
 	if recursive {
-		args = append(args, "./...")
+		dir = filepath.Join(dir, "...")
 	}
+	args = append(args, dir)
 
 	stderr := io.MultiWriter(er, workDoneWriter{work})