diff --git a/Documentation/usage/dlv.md b/Documentation/usage/dlv.md index 9dd8c538..1ecac545 100644 --- a/Documentation/usage/dlv.md +++ b/Documentation/usage/dlv.md @@ -31,7 +31,7 @@ Pass flags to the program you are debugging using `--`, for example: --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_attach.md b/Documentation/usage/dlv_attach.md index a5828ee0..18c3a0f8 100644 --- a/Documentation/usage/dlv_attach.md +++ b/Documentation/usage/dlv_attach.md @@ -37,7 +37,7 @@ dlv attach pid [executable] --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_backend.md b/Documentation/usage/dlv_backend.md index 579817dc..35cebba7 100644 --- a/Documentation/usage/dlv_backend.md +++ b/Documentation/usage/dlv_backend.md @@ -30,7 +30,7 @@ are: --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_connect.md b/Documentation/usage/dlv_connect.md index 3690ecee..22854725 100644 --- a/Documentation/usage/dlv_connect.md +++ b/Documentation/usage/dlv_connect.md @@ -26,7 +26,7 @@ dlv connect addr --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_core.md b/Documentation/usage/dlv_core.md index 46ce2d2d..6bf3e5d5 100644 --- a/Documentation/usage/dlv_core.md +++ b/Documentation/usage/dlv_core.md @@ -32,7 +32,7 @@ dlv core --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_dap.md b/Documentation/usage/dlv_dap.md index f538c781..a3144a69 100644 --- a/Documentation/usage/dlv_dap.md +++ b/Documentation/usage/dlv_dap.md @@ -33,7 +33,7 @@ dlv dap --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_debug.md b/Documentation/usage/dlv_debug.md index b951a3ca..f007ff72 100644 --- a/Documentation/usage/dlv_debug.md +++ b/Documentation/usage/dlv_debug.md @@ -39,7 +39,7 @@ dlv debug [package] --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_exec.md b/Documentation/usage/dlv_exec.md index b275a02c..aad2582c 100644 --- a/Documentation/usage/dlv_exec.md +++ b/Documentation/usage/dlv_exec.md @@ -39,7 +39,7 @@ dlv exec --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_log.md b/Documentation/usage/dlv_log.md index f5b22aba..0b6985e4 100644 --- a/Documentation/usage/dlv_log.md +++ b/Documentation/usage/dlv_log.md @@ -45,7 +45,7 @@ and dap modes. --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_replay.md b/Documentation/usage/dlv_replay.md index 1ddf98eb..40713e83 100644 --- a/Documentation/usage/dlv_replay.md +++ b/Documentation/usage/dlv_replay.md @@ -30,7 +30,7 @@ dlv replay [trace directory] --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_run.md b/Documentation/usage/dlv_run.md index f0e7693d..e729f099 100644 --- a/Documentation/usage/dlv_run.md +++ b/Documentation/usage/dlv_run.md @@ -26,7 +26,7 @@ dlv run --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_test.md b/Documentation/usage/dlv_test.md index 35fc3c8d..931b85bf 100644 --- a/Documentation/usage/dlv_test.md +++ b/Documentation/usage/dlv_test.md @@ -37,7 +37,7 @@ dlv test [package] --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_trace.md b/Documentation/usage/dlv_trace.md index d6b053df..ae237c18 100644 --- a/Documentation/usage/dlv_trace.md +++ b/Documentation/usage/dlv_trace.md @@ -44,7 +44,7 @@ dlv trace [package] regexp --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/Documentation/usage/dlv_version.md b/Documentation/usage/dlv_version.md index 6dbf66e7..1cee5938 100644 --- a/Documentation/usage/dlv_version.md +++ b/Documentation/usage/dlv_version.md @@ -26,7 +26,7 @@ dlv version --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) - --wd string Working directory for running the program. (default ".") + --wd string Working directory for running the program. ``` ### SEE ALSO diff --git a/_fixtures/buildtest/main_test.go b/_fixtures/buildtest/main_test.go index 3b7d16cc..739ae508 100644 --- a/_fixtures/buildtest/main_test.go +++ b/_fixtures/buildtest/main_test.go @@ -8,3 +8,8 @@ import ( func TestMain(m *testing.M) { os.Exit(m.Run()) } + +func TestCurrentDirectory(t *testing.T) { + wd, _ := os.Getwd() + t.Logf("current directory: %s", wd) +} diff --git a/cmd/dlv/cmds/commands.go b/cmd/dlv/cmds/commands.go index 0799aa54..e911cf0b 100644 --- a/cmd/dlv/cmds/commands.go +++ b/cmd/dlv/cmds/commands.go @@ -1,6 +1,7 @@ package cmds import ( + "encoding/json" "errors" "fmt" "net" @@ -118,7 +119,7 @@ func New(docCall bool) *cobra.Command { rootCommand.PersistentFlags().IntVar(&apiVersion, "api-version", 1, "Selects API version when headless. New clients should use v2. Can be reset via RPCServer.SetApiVersion. See Documentation/api/json-rpc/README.md.") rootCommand.PersistentFlags().StringVar(&initFile, "init", "", "Init file, executed by the terminal client.") rootCommand.PersistentFlags().StringVar(&buildFlags, "build-flags", buildFlagsDefault, "Build flags, to be passed to the compiler.") - rootCommand.PersistentFlags().StringVar(&workingDir, "wd", ".", "Working directory for running the program.") + rootCommand.PersistentFlags().StringVar(&workingDir, "wd", "", "Working directory for running the program.") rootCommand.PersistentFlags().BoolVarP(&checkGoVersion, "check-go-version", "", true, "Checks that the version of Go in use is compatible with Delve.") rootCommand.PersistentFlags().BoolVarP(&checkLocalConnUser, "only-same-user", "", true, "Only connections from the same user that started this instance of Delve are allowed to connect.") rootCommand.PersistentFlags().StringVar(&backend, "backend", "default", `Backend selection (see 'dlv help backend').`) @@ -509,6 +510,10 @@ func traceCmd(cmd *cobra.Command, args []string) { listener, clientConn := service.ListenerPipe() defer listener.Close() + if workingDir == "" { + workingDir = "." + } + // Create and start a debug server server := rpccommon.NewServer(&service.Config{ Listener: listener, @@ -592,11 +597,35 @@ func testCmd(cmd *cobra.Command, args []string) { defer gobuild.Remove(debugname) processArgs := append([]string{debugname}, targetArgs...) + if workingDir == "" { + if len(dlvArgs) == 1 { + workingDir = getPackageDir(dlvArgs[0]) + } else { + workingDir = "." + } + } + return execute(0, processArgs, conf, "", debugger.ExecutingGeneratedTest, dlvArgs, buildFlags) }() os.Exit(status) } +func getPackageDir(pkg string) string { + out, err := exec.Command("go", "list", "--json", pkg).CombinedOutput() + if err != nil { + return "." + } + type listOut struct { + Dir string `json:"Dir"` + } + var listout listOut + err = json.Unmarshal(out, &listout) + if err != nil { + return "." + } + return listout.Dir +} + func attachCmd(cmd *cobra.Command, args []string) { pid, err := strconv.Atoi(args[0]) if err != nil { @@ -736,6 +765,10 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile disconnectChan := make(chan struct{}) + if workingDir == "" { + workingDir = "." + } + // Create and start a debugger server switch apiVersion { case 1, 2: diff --git a/cmd/dlv/dlv_test.go b/cmd/dlv/dlv_test.go index f4725801..137e5c1a 100644 --- a/cmd/dlv/dlv_test.go +++ b/cmd/dlv/dlv_test.go @@ -728,3 +728,23 @@ func TestTracePrintStack(t *testing.T) { t.Fatal("stacktrace not printed") } } + +func TestDlvTestChdir(t *testing.T) { + dlvbin, tmpdir := getDlvBin(t) + defer os.RemoveAll(tmpdir) + + fixtures := protest.FindFixturesDir() + cmd := exec.Command(dlvbin, "test", filepath.Join(fixtures, "buildtest"), "--", "-test.v") + cmd.Stdin = strings.NewReader("continue\nexit\n") + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("error executing Delve: %v", err) + } + t.Logf("output: %q", out) + + p, _ := filepath.Abs(filepath.Join(fixtures, "buildtest")) + tgt := "current directory: " + p + if !strings.Contains(string(out), tgt) { + t.Errorf("output did not contain expected string %q", tgt) + } +}