From 4dd9a27bc2e4262486fcafd2c54eb622d6b190b5 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Thu, 2 Nov 2023 11:47:09 -0700 Subject: [PATCH 1/2] cmd: fix a bunch of linter warnings from GoLand --- cmd/dlv/cmds/commands.go | 22 +++++++++++----------- cmd/dlv/cmds/helphelpers/help.go | 4 ++-- cmd/dlv/dlv_test.go | 14 +++++++------- cmd/dlv/main.go | 9 +++++++-- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/cmd/dlv/cmds/commands.go b/cmd/dlv/cmds/commands.go index e6af4c6255..2cda26e0d0 100644 --- a/cmd/dlv/cmds/commands.go +++ b/cmd/dlv/cmds/commands.go @@ -120,7 +120,7 @@ func New(docCall bool) *cobra.Command { conf, loadConfErr = config.LoadConfig() // Delay reporting errors about configuration loading delayed until after the // server is started so that the "server listening at" message is always - // the first thing emitted. Also logflags hasn't been setup yet at this point. + // the first thing emitted. Also, logflags hasn't been set up yet at this point. buildFlagsDefault := "" if runtime.GOOS == "windows" { ver, _ := goversion.Installed() @@ -501,7 +501,7 @@ func dapCmd(cmd *cobra.Command, args []string) { } disconnectChan := make(chan struct{}) - config := &service.Config{ + cfg := &service.Config{ DisconnectChan: disconnectChan, Debugger: debugger.Config{ Backend: backend, @@ -519,7 +519,7 @@ func dapCmd(cmd *cobra.Command, args []string) { fmt.Printf("couldn't start listener: %s\n", err) return 1 } - config.Listener = listener + cfg.Listener = listener } else { // with a predetermined client. var err error conn, err = net.Dial("tcp", dapClientAddr) @@ -529,7 +529,7 @@ func dapCmd(cmd *cobra.Command, args []string) { } } - server := dap.NewServer(config) + server := dap.NewServer(cfg) defer server.Stop() if conn == nil { server.Run() @@ -838,7 +838,7 @@ func getPackageDir(pkg []string) string { return listout.Dir } -func attachCmd(cmd *cobra.Command, args []string) { +func attachCmd(_ *cobra.Command, args []string) { var pid int if len(args) > 0 { var err error @@ -852,11 +852,11 @@ func attachCmd(cmd *cobra.Command, args []string) { os.Exit(execute(pid, args, conf, "", debugger.ExecutingOther, args, buildFlags)) } -func coreCmd(cmd *cobra.Command, args []string) { +func coreCmd(_ *cobra.Command, args []string) { os.Exit(execute(0, []string{args[0]}, conf, args[1], debugger.ExecutingOther, args, buildFlags)) } -func connectCmd(cmd *cobra.Command, args []string) { +func connectCmd(_ *cobra.Command, args []string) { if err := logflags.Setup(log, logOutput, logDest); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) @@ -871,7 +871,7 @@ func connectCmd(cmd *cobra.Command, args []string) { logflags.Close() os.Exit(1) } - ec := connect(addr, nil, conf, debugger.ExecutingOther) + ec := connect(addr, nil, conf) logflags.Close() os.Exit(ec) } @@ -909,7 +909,7 @@ func splitArgs(cmd *cobra.Command, args []string) ([]string, []string) { return args, []string{} } -func connect(addr string, clientConn net.Conn, conf *config.Config, kind debugger.ExecuteKind) int { +func connect(addr string, clientConn net.Conn, conf *config.Config) int { // Create and start a terminal - attach to running instance var client *rpc2.RPCClient if clientConn != nil { @@ -1058,7 +1058,7 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile } if err := server.Run(); err != nil { - if err == api.ErrNotExecutable { + if errors.Is(err, api.ErrNotExecutable) { switch kind { case debugger.ExecutingGeneratedFile: fmt.Fprintln(os.Stderr, "Can not debug non-main package") @@ -1089,7 +1089,7 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile return status } - return connect(listener.Addr().String(), clientConn, conf, kind) + return connect(listener.Addr().String(), clientConn, conf) } func parseRedirects(redirects []string) ([3]string, error) { diff --git a/cmd/dlv/cmds/helphelpers/help.go b/cmd/dlv/cmds/helphelpers/help.go index aff5e9258f..609aa89674 100644 --- a/cmd/dlv/cmds/helphelpers/help.go +++ b/cmd/dlv/cmds/helphelpers/help.go @@ -6,10 +6,10 @@ import ( ) // Prepare prepares cmd flag set for the invocation of its usage function by -// hiding flags that we want cobra to parse but we don't want to show to the +// hiding flags that we want cobra to parse, but we don't want to show to the // user. // We do this because not all flags associated with the root command are -// valid for all subcommands but we don't want to move them out of the root +// valid for all subcommands, but we don't want to move them out of the root // command and into subcommands, since that would change how cobra parses // the command line. // diff --git a/cmd/dlv/dlv_test.go b/cmd/dlv/dlv_test.go index 32cc2d51c3..1f9aab64aa 100644 --- a/cmd/dlv/dlv_test.go +++ b/cmd/dlv/dlv_test.go @@ -180,17 +180,17 @@ func testOutput(t *testing.T, dlvbin, output string, delveCmds []string) (stdout // ignore "dlv debug" command error, it returns // errors even after successful debug session. - cmd.Wait() + _ = cmd.Wait() stdout, stderr = stdoutBuf.Bytes(), stderrBuf.Bytes() _, err = os.Stat(debugbin) if err == nil { // Sometimes delve on Windows can't remove the built binary before // exiting and gets an "Access is denied" error when trying. - // See: https://travis-ci.com/go-delve/delve/jobs/296325131) + // See: https://travis-ci.com/go-delve/delve/jobs/296325131. // We have added a delay to gobuild.Remove, but to avoid any test // flakiness, we guard against this failure here as well. - if runtime.GOOS != "windows" || !strings.Contains(err.Error(), "Access is denied") { + if runtime.GOOS != "windows" { t.Errorf("running %q: file %v was not deleted\nstdout is %q, stderr is %q", delveCmds, debugbin, stdout, stderr) } return @@ -204,7 +204,7 @@ func testOutput(t *testing.T, dlvbin, output string, delveCmds []string) (stdout func getDlvBin(t *testing.T) string { // In case this was set in the environment - // from getDlvBinEBPF lets clear it here so + // from getDlvBinEBPF lets clear it here, so // we can ensure we don't get build errors // depending on the test ordering. t.Setenv("CGO_LDFLAGS", ldFlags) @@ -427,7 +427,7 @@ func TestExitInInit(t *testing.T) { cmd.Dir = buildtestdir out, err := cmd.CombinedOutput() t.Logf("%q %v\n", string(out), err) - // dlv will exit anyway because stdin is not a tty but it will print the + // dlv will exit anyway because stdin is not a tty, but it will print the // prompt once if the init file didn't call exit successfully. if strings.Contains(string(out), "(dlv)") { t.Fatal("init did not cause dlv to exit") @@ -1352,8 +1352,8 @@ func TestDefaultBinary(t *testing.T) { fmt.Fprintf(stdin1, "continue\nquit\n") fmt.Fprintf(stdin2, "continue\nquit\n") - wait1() - wait2() + _ = wait1() + _ = wait2() out1, out2 := stdoutBuf1.String(), stdoutBuf2.String() t.Logf("%q", out1) diff --git a/cmd/dlv/main.go b/cmd/dlv/main.go index 34df91db47..1685daf70e 100644 --- a/cmd/dlv/main.go +++ b/cmd/dlv/main.go @@ -15,11 +15,16 @@ func main() { if Build != "" { version.DelveVersion.Build = Build } + const cgoCflagsEnv = "CGO_CFLAGS" if os.Getenv(cgoCflagsEnv) == "" { - os.Setenv(cgoCflagsEnv, "-O0 -g") + err := os.Setenv(cgoCflagsEnv, "-O0 -g") + if err != nil { + logrus.WithFields(logrus.Fields{"layer": "dlv"}).Warnf("CGO_CFLAGS could not be set, Cgo code could be optimized: %v", err) + } } else { logrus.WithFields(logrus.Fields{"layer": "dlv"}).Warnln("CGO_CFLAGS already set, Cgo code could be optimized.") } - cmds.New(false).Execute() + + _ = cmds.New(false).Execute() } From bd5508fb6b82474cc261c9abdd2c675f475c78e5 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Fri, 3 Nov 2023 07:04:24 -0700 Subject: [PATCH 2/2] cleanup --- cmd/dlv/dlv_test.go | 8 ++++---- cmd/dlv/main.go | 7 ++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/cmd/dlv/dlv_test.go b/cmd/dlv/dlv_test.go index 1f9aab64aa..2fe55a9d0e 100644 --- a/cmd/dlv/dlv_test.go +++ b/cmd/dlv/dlv_test.go @@ -180,7 +180,7 @@ func testOutput(t *testing.T, dlvbin, output string, delveCmds []string) (stdout // ignore "dlv debug" command error, it returns // errors even after successful debug session. - _ = cmd.Wait() + cmd.Wait() stdout, stderr = stdoutBuf.Bytes(), stderrBuf.Bytes() _, err = os.Stat(debugbin) @@ -308,7 +308,7 @@ func TestRedirect(t *testing.T) { // and detach from and kill the headless instance client := rpc2.NewClient(listenAddr) - _ = client.Detach(true) + client.Detach(true) cmd.Wait() } @@ -1352,8 +1352,8 @@ func TestDefaultBinary(t *testing.T) { fmt.Fprintf(stdin1, "continue\nquit\n") fmt.Fprintf(stdin2, "continue\nquit\n") - _ = wait1() - _ = wait2() + wait1() + wait2() out1, out2 := stdoutBuf1.String(), stdoutBuf2.String() t.Logf("%q", out1) diff --git a/cmd/dlv/main.go b/cmd/dlv/main.go index 1685daf70e..dcb92c457b 100644 --- a/cmd/dlv/main.go +++ b/cmd/dlv/main.go @@ -18,13 +18,10 @@ func main() { const cgoCflagsEnv = "CGO_CFLAGS" if os.Getenv(cgoCflagsEnv) == "" { - err := os.Setenv(cgoCflagsEnv, "-O0 -g") - if err != nil { - logrus.WithFields(logrus.Fields{"layer": "dlv"}).Warnf("CGO_CFLAGS could not be set, Cgo code could be optimized: %v", err) - } + os.Setenv(cgoCflagsEnv, "-O0 -g") } else { logrus.WithFields(logrus.Fields{"layer": "dlv"}).Warnln("CGO_CFLAGS already set, Cgo code could be optimized.") } - _ = cmds.New(false).Execute() + cmds.New(false).Execute() }