Repository: grafana/k6 (Go)

Issue:
`k6 login cloud` panicking with 1.2.1 upgrade
### Brief summary

With the upgrade to `1.2.1` from `1.1.0` we started seeing panics from using the deprecated `k6 login cloud` command as `k6 login` now only accepts `influxdb`.

It's not a huge deal as we can update our scripts that use `k6 login cloud` to the new `k6 cloud login` command, but flagging that this wasn't mentioned in the release notes if it was intended to be removed, or whether it should fail more gracefully rather than panicking.

```
$ k6 login
Command "login" is deprecated, and will be removed in a future release. Please use the "k6 cloud login" command instead.

Usage:
  k6 login [flags]
  k6 login [command]

Available Commands:
  influxdb    Authenticate with InfluxDB

Flags:
  -h, --help   help for login
```


### k6 version

1.2.1

### OS

macOS and Ubuntu

### Docker version and image (if applicable)

_No response_

### Steps to reproduce the problem

1. Run `k6 login cloud`

### Expected behaviour

Based on the release notes, the warning to appear when running the command but the login still working.

### Actual behaviour

```
$ k6 login cloud -t foo
Command "cloud" is deprecated, and will be removed in a future release.
Please use the "k6 cloud login" command instead.

ERRO[0000] unexpected k6 panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
runtime/debug.Stack()
	runtime/debug/stack.go:26 +0x64
go.k6.io/k6/internal/cmd.(*rootCommand).execute.func2()
	go.k6.io/k6/internal/cmd/root.go:135 +0x54
panic({0x103c62080?, 0x140001b44f8?})
	runtime/panic.go:792 +0x124
go.k6.io/k6/internal/cmd.analyze(0x1400025d8c8, {0x1400019f440, 0x0, 0x105143d48?})
	go.k6.io/k6/internal/cmd/launcher.go:305 +0x394
go.k6.io/k6/internal/cmd.(*launcher).launch(0x140002a6b70, 0x14000327348, {0x1400019f440, 0x0, 0x2})
	go.k6.io/k6/internal/cmd/launcher.go:88 +0x144
go.k6.io/k6/internal/cmd.(*rootCommand).persistentPreRunE(0x140001cdd40, 0x14000327348, {0x1400019f440, 0x0, 0x2})
	go.k6.io/k6/internal/cmd/root.go:118 +0xe4
github.com/spf13/cobra.(*Command).execute(0x14000327348, {0x1400019f420, 0x2, 0x2})
	github.com/spf13/cobra@v1.4.0/command.go:835 +0x4c4
github.com/spf13/cobra.(*Command).ExecuteC(0x14000319348)
	github.com/spf13/cobra@v1.4.0/command.go:974 +0x318
github.com/spf13/cobra.(*Command).Execute(...)
	github.com/spf13/cobra@v1.4.0/command.go:902
go.k6.io/k6/internal/cmd.(*rootCommand).execute(0x140001cdd40)
	go.k6.io/k6/internal/cmd/root.go:143 +0xc8
go.k6.io/k6/internal/cmd.ExecuteWithGlobalState(0x1400025d8c8)
	go.k6.io/k6/internal/cmd/root.go:35 +0xac
go.k6.io/k6/cmd.Execute()
	go.k6.io/k6/cmd/execute.go:16 +0x30
main.main()
	go.k6.io/k6/main.go:9 +0x1c
```


Discussion:
Hi @indygriffiths, thank you for reporting this 🙇 

I can confirm this is happening for me as well. It seems some part of the auto extension resolution is not handling this particular case.

I can also say that you can run with `K6_AUTO_EXTENSION_RESOLUTION=false` and it works. As in 

`K6_AUTO_EXTENSION_RESOLUTION=false k6 cloud login ...` 

I will try to have see if we can make a fix tomorrow.



Patch:
diff --git a/internal/cmd/launcher.go b/internal/cmd/launcher.go
index 975a8cc2900..74da02d4b76 100644
--- a/internal/cmd/launcher.go
+++ b/internal/cmd/launcher.go
@@ -333,8 +333,21 @@ func analyze(gs *state.GlobalState, args []string) (k6deps.Dependencies, error)
 // isAnalysisRequired returns a boolean indicating if dependency analysis is required for the command
 func isAnalysisRequired(cmd *cobra.Command) bool {
 	switch cmd.Name() {
-	case "run", "archive", "inspect", "upload", "cloud":
-		return true
+	case "run":
+		if parent := cmd.Parent(); parent != nil {
+			switch parent.Name() {
+			case "k6", "cloud":
+				return true
+			}
+		}
+	case "upload":
+		if parent := cmd.Parent(); parent != nil {
+			return parent.Name() == "cloud"
+		}
+	case "cloud", "archive", "inspect":
+		if parent := cmd.Parent(); parent != nil {
+			return parent.Name() == "k6"
+		}
 	}
 
 	return false


Test patch:
diff --git a/internal/cmd/tests/tests_test.go b/internal/cmd/tests/tests_test.go
index b5fb83dc0fc..39aca03a458 100644
--- a/internal/cmd/tests/tests_test.go
+++ b/internal/cmd/tests/tests_test.go
@@ -5,6 +5,7 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+
 	"go.k6.io/k6/internal/cmd"
 )
 
@@ -32,3 +33,13 @@ func TestRootCommand(t *testing.T) {
 		})
 	}
 }
+
+func TestLoginCloudNotPanicking(t *testing.T) {
+	t.Parallel()
+
+	ts := NewGlobalTestState(t)
+	ts.CmdArgs = []string{"k6", "login", "cloud"}
+	ts.ExpectedExitCode = -1
+	cmd.ExecuteWithGlobalState(ts.GlobalState)
+	assert.Contains(t, ts.Stderr.String(), "Stdin is not a terminal, falling back to plain text input")
+}