Skip to content

Fix shell execution in "zmk code" command#49

Merged
joelspadin merged 2 commits intozmkfirmware:mainfrom
joelspadin:shell-fix
Jan 5, 2026
Merged

Fix shell execution in "zmk code" command#49
joelspadin merged 2 commits intozmkfirmware:mainfrom
joelspadin:shell-fix

Conversation

@joelspadin
Copy link
Collaborator

While the Python documentation states

If shell is True, it is recommended to pass args as a string rather than as a sequence.

it seems to be more of a requirement than a recommendation on Linux. To fix this, instead of launching the editor using a shell command, we now use shutil.which() to get the full path to the executable as if it were running in a shell, then run it as a normal process (with the args remaining as a list) instead of in a shell.

This supersedes #30

Also fixed an issue with zmk code where it printed invalid zmk config ... commands.

Running a command in a POSIX shell requires it be a string instead of a
list, but quoting syntax differs between POSIX and non-POSIX shells, so
there is no shell-agnostic way to do that.

Instead, we now look up the first argument in PATH directories like a
shell would, then run the command without a shell.

Fixes zmkfirmware#27
To simplify parsing, the "zmk config" command was switched long ago from
expecting a single "variable=value" argument to having the variable and
value passed as separate arguments. Some of the "zmk code" command
output still referenced the old format. This fixes that.
@joelspadin joelspadin added the bug Something isn't working label Jan 5, 2026
from pathlib import Path


def get_subprocess_args(cmd: list[str | Path]) -> list[str | Path]:
Copy link
Contributor

@caksoylar caksoylar Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to be able to use executables from PATH (which shell=True did before)?

edit: seems so from the docstring below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. When you don't pass a path argument to shutil.which(), it searches PATH: https://docs.python.org/3/library/shutil.html#shutil.which

@joelspadin joelspadin merged commit 983818c into zmkfirmware:main Jan 5, 2026
1 check passed
@joelspadin joelspadin deleted the shell-fix branch January 5, 2026 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants