nx-completion icon indicating copy to clipboard operation
nx-completion copied to clipboard

> NX Running global Nx CLI with PNPM may have issues.

Open kopach opened this issue 2 years ago • 5 comments

When running nx in pnpm managed repo, I get the following warning (which sometimes even results in not working command)

 >  NX   Running global Nx CLI with PNPM may have issues.

   Prefer to use "pnpm" (https://pnpm.io/cli/exec) to execute commands in this workspace.
    TIP  create a shortcut such as: alias pnx="pnpm nx --"

Using the alias from the TIP would break this autocompletion, so would be really great if the plugin could work with PNPM natively and run pnpm nx whenever nx is used.

Here is what I came up with as a solution for me:

In .zrhrc

function pnpm-nx() {
	local cmd=$BUFFER
	if [[ "$cmd" =~ ^nx ]]; then
		local new_cmd=$(echo $cmd | sed -E 's/^nx /pnpm nx /g')
		BUFFER="$new_cmd"
	fi
	zle accept-line
}

zle -N pnpm-nx
bindkey "^M" pnpm-nx

So, now whenever I type nx {command} it gets replaced by pnpm nx {command} automatically on Enter. This also doesn't break this autocompletion plugin

kopach avatar Jan 20 '23 18:01 kopach

I recently started to use pnpm and also noticed the warning but I just ignored it, I wonder what issues it can cause and why. I agree it would be great to handle this directly in the plugin, if you want to contribute you are welcome.

edbzn avatar Jan 20 '23 19:01 edbzn

Sometimes, some command will fail if used without pnpm prefix. Usually more complex commands like nx generate or nx migrate, so this is not only warning.

I might take a look on this, but not sure I'll have time. But feel free to reuse that piece of code I've shared.

btw, not sure if this is possible, but I would improve the behavior of that script a bit. The downside of script from above is that pnpm nx.. command gets written to history instead of nx .... So, I can't navigate back in history with arrows and re-use nx ... command with substitution. nx-completion plugin won't work. Probably the solution would be to alter terminal history somehow after execution and write there nx ... instead of pnpm nx ...

kopach avatar Jan 24 '23 12:01 kopach

I'm not sure if this helps in your case, but I have added this tiny script to my .zshrc to get autocomplete working whithout nx being globally installed (yarn in my case):

function nx() {
    yarn nx "$@"
}

Peeeep avatar Jan 24 '23 17:01 Peeeep

Just as an idea for potential solution. There is a package I use https://github.com/antfu/ni which can automatically detect package manager used in the project and install dependencies with proper manager. So, this plugin, potentially could do the same – detect package manager used and append yarn , pnpm or nothing in case of npm

kopach avatar Jan 25 '23 15:01 kopach

and @Peeeep , your solution works like a charm, thanks. In my case, I did the following for pnpm

# ~/.zshrc file
function nx() {
    pnpm nx "$@"
}

kopach avatar Jan 26 '23 13:01 kopach