2

I have a makefile that has the targets

  • start-prod
  • start-test
  • stop-prod
  • stop-test
  • restart-prod
  • restart-test

Autocompletion works in bash without problems but in zsh it will only complete start- etc. not start-prod.

The targets are defined with makros.

define commands =
$(eval
restart-$(1):   stop-$(1) start-$(1)
start-$(1):     ; docker-compose up -d --remove-orphans $(1)
stop-$(1):      ; docker-compose stop $(1)
)
endef


$(call commands,prod)
$(call commands,test)

My assumption is that bash evaluates the makefiles to get the targets while zsh try to parse it.

Is there any way to get this working in zsh.

I tried to use the bash command in zsh like mentioned in this question. But this only got me an error when I try to complete an makefile

_init_completion: command not found zsh

1 Answer 1

5

Bash calls make with options that make it print out the evaluated makefile in a normalized form. Zsh can do this as well, but by default it uses its own parser. Calling make gives more precise information, but since it evaluates things like $(shell) commands inside the makefile, it can be slower and is dangerous in the rare case where you don't trust the content of the makefile (that would be pretty rare though, since the reason you'd use completion is because you plan to run make anyway).

To make zsh call the make command, enable the call-command completion style. (To find out the exact style pattern to set, looking at the trace generated when set -x is active is easier than figuring out the documentation.)

zstyle ':completion::complete:make:*:targets' call-command true

Replace make by * for it to apply to other completions such as for ant.

Even with this feature enabled, zsh won't call make if it isn't GNU make, so you can enable it everywhere without loss of functionality. (Bash does, so bash's completion doesn't work with other make implementations.)

2
  • Thank you. that was exactly what I needed :) Commented Jul 7, 2021 at 8:38
  • FWIW, this may have worked, but I wound up using a more explicit solution as I worked through the following issue: I had made a typo in a fresh makefile, and with this solution zsh output all of the environment variables I might want to set while calling make. This behavior was different than bash completion under similar conditions and sent me looking for more solutions. Long story short: verify your makefile works prior to trying this.
    – Chris
    Commented Sep 2, 2022 at 13:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .