dsifford / yarn-completion

Bash completion for Yarn
MIT License
277 stars 25 forks source link

Workspace package completion #39

Closed jgornick closed 5 years ago

jgornick commented 5 years ago

I'm slightly confused with the features in #30. Was this to support completion after the run for a workspace or to provide completion for the workspace, or both?

I'm trying to use completion for the workspace command: yarn workspace <TAB> and it doesn't seem to provide me names of packages.

However, when I use yarn workspace @foo/bar run <TAB>, is shows the scripts for that workspace package.

Should the workspace completion work as well?

Thanks!

dsifford commented 5 years ago

I'm trying to use completion for the workspace command: yarn workspace <TAB> and it doesn't seem to provide me names of packages.

That's strange. Do you have a link to the repository containing these workspaces?

I just tried this on my machine using the jest repository (since that one has workspaces and they're quite complex) and it seemed to work fine.

jgornick commented 5 years ago

@dsifford I just tried on that repository as well and was unable to perform the workspace <TAB> completion.

Are there other environment variables that might be affecting this?

I have exported:

export COMP_WORDBREAKS=${COMP_WORDBREAKS//:}

... which removes the : from the default value allowing completions with : in the names to work (e.g. yarn build:ts:watch). The new value evaluates to:

COMP_WORDBREAKS=
"'><=;|&(

I did remove the COMP_WORDBREAKS export back to default and still unable to complete workspace.

$ bash --version
GNU bash, version 5.0.7(1)-release (x86_64-apple-darwin17.7.0)

Any other ideas how to debug the issue?

jgornick commented 5 years ago

I turned on set -x to see all calls when I press <TAB> after workspace and here's a snippet when it gets into the yarn workspace code:

+ declare completions_func=_yarn_workspace
+ declare -F _yarn_workspace
+ _yarn_workspace
+ [[ workspace == workspace ]]
+ [[ 2 == 2 ]]
+ declare -a workspaces
+ declare module_path
++ __yarn_get_package_fields -t array workspaces
++ declare field_key
++ declare field_type=object
++ declare package_dot_json
++ declare cwd
+++ pwd
++ cwd=/Users/jgornick/Projects/jest
++ [[ /Users/jgornick/Projects/jest != \/ ]]
++ [[ -f /Users/jgornick/Projects/jest/package.json ]]
++ package_dot_json=/Users/jgornick/Projects/jest/package.json
++ break
++ declare OPTIND OPTARG opt
++ getopts :gt: opt
++ case $opt in
++ case "$OPTARG" in
++ field_type=array
++ getopts :gt: opt
++ shift 2
++ field_key='"workspaces"'
++ [[ ! -f /Users/jgornick/Projects/jest/package.json ]]
++ [[ ! -n "workspaces" ]]
++ case "$field_type" in
++ sed -n '/"workspaces":[[:space:]]*\[/,/^[[:space:]]*]/{
                # exclude start and end patterns
                //!{
                    # extract the text between the first pair of double quotes
                    s/^[[:space:]]*"\([^"]*\).*/\1/p
                }
            }' /Users/jgornick/Projects/jest/package.json
+ COMPREPLY=($(compgen -W "${workspaces[*]}" -- "$cur"))
++ compgen -W '' -- ''
+ return
+ declare -F _yarn_workspace
+ _yarn_workspace
+ [[ workspace == workspace ]]
+ [[ 2 == 2 ]]
+ declare -a workspaces
+ declare module_path
++ __yarn_get_package_fields -t array workspaces
++ declare field_key
++ declare field_type=object
++ declare package_dot_json
++ declare cwd
+++ pwd
++ cwd=/Users/jgornick/Projects/jest
++ [[ /Users/jgornick/Projects/jest != \/ ]]
++ [[ -f /Users/jgornick/Projects/jest/package.json ]]
++ package_dot_json=/Users/jgornick/Projects/jest/package.json
++ break
++ declare OPTIND OPTARG opt
++ getopts :gt: opt
++ case $opt in
++ case "$OPTARG" in
++ field_type=array
++ getopts :gt: opt
++ shift 2
++ field_key='"workspaces"'
++ [[ ! -f /Users/jgornick/Projects/jest/package.json ]]
++ [[ ! -n "workspaces" ]]
++ case "$field_type" in
++ sed -n '/"workspaces":[[:space:]]*\[/,/^[[:space:]]*]/{
                # exclude start and end patterns
                //!{
                    # extract the text between the first pair of double quotes
                    s/^[[:space:]]*"\([^"]*\).*/\1/p
                }
            }' /Users/jgornick/Projects/jest/package.json
+ COMPREPLY=($(compgen -W "${workspaces[*]}" -- "$cur"))
++ compgen -W '' -- ''
+ return
+ COMP_WORDBREAKS='
"'\''><=;|&('
+ return 0
dsifford commented 5 years ago

Just tried jest using my macbook and I'm able to complete workspace names there as well.

The only guess that comes to my mind is that it has something to do with the garbage BSD utilities that come built-in with mac os. Specifically, I think it might have to do with BSD sed.

To confirm this, and if you have homebrew, you could try installing GNU sed (being sure to make sure it's in your PATH after installing)....

$ brew install gnu-sed
# then follow the prompts
jgornick commented 5 years ago

I've updated sed and still same issue:

$ sed --version
sed (GNU sed) 4.7

Any other gnu utils that are being used? Is there anything w/r/t shopts that should be set?

dsifford commented 5 years ago

Very odd... Here's a few things direct from my macbook...

Machine info

$ uname -a
Darwin osx 19.0.0 Darwin Kernel Version 19.0.0: Thu Jul 11 18:37:36 PDT 2019; root:xnu-6153.0.59.141.4~1/RELEASE_X86_64 x86_64 i386 MacBookPro13,2 Darwin
$ bash --version
GNU bash, version 5.0.7(1)-release (x86_64-apple-darwin18.5.0)

Shell options

autocd                   off
assoc_expand_once        off
cdable_vars              off
cdspell                  on
checkhash                off
checkjobs                off
checkwinsize             on
cmdhist                  on
compat31                 off
compat32                 off
compat40                 off
compat41                 off
compat42                 off
compat43                 off
compat44                 off
complete_fullquote       on
direxpand                off
dirspell                 on
dotglob                  off
execfail                 off
expand_aliases           on
extdebug                 off
extglob                  on
extquote                 on
failglob                 off
force_fignore            on
globasciiranges          on
globstar                 on
gnu_errfmt               off
histappend               off
histreedit               off
histverify               off
hostcomplete             off
huponexit                off
inherit_errexit          off
interactive_comments     on
lastpipe                 off
lithist                  off
localvar_inherit         off
localvar_unset           off
login_shell              on
mailwarn                 off
no_empty_cmd_completion  off
nocaseglob               off
nocasematch              off
nullglob                 on
progcomp                 on
progcomp_alias           off
promptvars               on
restricted_shell         off
shift_verbose            off
sourcepath               on
xpg_echo                 off

Possibly relevant homebrew packages

bash-completion@2
coreutils
findutils
gawk
gnu-getopt
gnu-sed
gnu-tar
gnu-which
grep

And if all else fails.... my dotfiles: https://github.com/dsifford/.dotfiles

jgornick commented 5 years ago

@dsifford Thanks for helping out with this. I just tried on my macOS machine at home and it works on there. Not that we didn't know this was environmental thing specific to my other macOS machine, but this confirms it for me too. I'll keep this issue posted if I find a solution to why it's not working on my other machine.

Thanks again!

jgornick commented 4 years ago

@dsifford I think my issue was that my default shell wasn't set for my user on one of my machines.

After running chsh -s /usr/local/bin/bash, it seems as if my workspace completions work to get the different workspace packages.

However, when I do: yarn workspace @jgornick/my-package [tab], the tab seems to show available yarn commands and scripts for the root package.json. I would expect it to show scripts for the specific workspace package.

Am I seeing this right?

Thanks!

dsifford commented 4 years ago

Yes, unfortunately it's not easily possible to complete the correct set of "scripts" given a workspace just with how the completions are written. If you want to be sure that you have the right "run" scripts being completed for a given workspace, just type yarn workspace @jgornick/my-package run <tab> and you'll get the right set.

jgornick commented 4 years ago

Weird, I had tried that and all I get for a completion is: yarn workspace @jgornick/my-package run [tab] ->yarn workspace @jgornick/my-package run env.

Thoughts?

dsifford commented 4 years ago

Not sure. I just tried locally on a clone of jest and it seemed to work as expected.

I'd start there and make sure it works for you too.