Skip to content

Fail step context list when there are no contexts - #1693

Open
arpitjain099 wants to merge 1 commit into
smallstep:masterfrom
arpitjain099:fix/context-list-no-contexts
Open

Fail step context list when there are no contexts#1693
arpitjain099 wants to merge 1 commit into
smallstep:masterfrom
arpitjain099:fix/context-list-no-contexts

Conversation

@arpitjain099

Copy link
Copy Markdown

Fixes #879.

Still reproduces on master:

$ export STEPPATH=/tmp/steppath   # empty
$ step context list
$ echo $?
0
$ step context current
no context selected
$ echo $?
1

listAction returns nil unconditionally, so an empty STEPPATH looks exactly like a successful listing that happened to have nothing in it. step context current already errors in the equivalent situation, which is what @jdoss ended up using as the workaround, so this makes list behave the same way.

After:

$ step context list
no contexts present
$ echo $?
1

The message goes to stderr (stdout is empty with 2>/dev/null), which is what the issue asked for.

I checked the populated case too, since the listing loop now runs over a variable rather than calling ListAlphabetical() inline:

$ STEPPATH=/tmp/steppath2 step context list
▶ beta
alpha-one
$ echo $?
0

Byte-identical to master's output for the same STEPPATH.

No test here: listAction reads the package-level step.Contexts(), which needs step.Init() and a STEPPATH to be set up, and nothing under command/ currently does that, so a test for this one command would have meant introducing global-state setup that has no precedent in the tree. Happy to add one if you would like it, and if you have a preferred way of standing up a STEPPATH in tests I would rather follow that than invent one.

go build ./..., go vet ./command/context/ and go test ./command/... are clean.

step context list printed nothing and exited 0 when STEPPATH has no
contexts, which is indistinguishable from a successful listing of an
empty set and gives a caller no way to tell the two apart.

step context current already errors with "no context selected" and
exits 1 in the equivalent situation, so match it.

Fixes smallstep#879

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
@github-actions github-actions Bot added the needs triage Waiting for discussion / prioritization by team label Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs triage Waiting for discussion / prioritization by team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: step context list should print an error to stderr and exit 1 if there are no contexts present

2 participants