Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/helpers: fix _command_exists() #2018

Merged
merged 2 commits into from
Jan 24, 2022

Conversation

gaelicWizard
Copy link
Contributor

@gaelicWizard gaelicWizard commented Jan 3, 2022

This is a tiny PR since I'm having trouble rebasing my helpers PR (#1934). This change was in the middle and just much easier to pull it aside 😃

Description

The weird subshell is weird AF. Just do a normal if. Ditto _binary_exists(), _completion_exists(), and _is_function()! Alsö, make all_groups() literally 100x faster.

Motivation and Context

Simplicity, consistency, and #1696! And git rebase is hard sometimes.

How Has This Been Tested?

Locale and all tests pass.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the code style of this project.
  • If my change requires a change to the documentation, I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • If I have added a new file, I also added it to clean_files.txt and formatted it using lint_clean_files.sh.
  • I have added tests to cover my changes, and all the new and existing tests pass.

@gaelicWizard
Copy link
Contributor Author

@NoahGorny, this one is tiny tiny if you could review & approve ♥

@gaelicWizard gaelicWizard marked this pull request as ready for review January 3, 2022 16:59
Copy link
Contributor

@davidpfarrell davidpfarrell left a comment

Choose a reason for hiding this comment

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

Greetings! Got some feedback on this one, but generally happy to see it get some attention ...

lib/helpers.bash Outdated Show resolved Hide resolved
lib/helpers.bash Outdated Show resolved Hide resolved
lib/helpers.bash Outdated Show resolved Hide resolved
lib/helpers.bash Show resolved Hide resolved
@gaelicWizard
Copy link
Contributor Author

This is ready for merge unless someone wants to OK removing the default message 👍

Copy link
Contributor

@davidpfarrell davidpfarrell left a comment

Choose a reason for hiding this comment

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

I'd rather remove the default message

Wondering what that would look like? i.e would we lose $2 altogether as well?

As these checks are often the core reason that components bail early and skip activation, I feel like its probably important for some form of logging to show up in the bash-it doctor logs.

I also feel like, in general, we are not passing $2 along when we invoke these (even in newly added code), nor are we adding our own logging statements.

So both removing the default message OR removing logging altogether feel like missed opportunities to give the user useful bash-it doctor messages.

lib/helpers.bash Show resolved Hide resolved
lib/helpers.bash Outdated Show resolved Hide resolved
lib/helpers.bash Show resolved Hide resolved
lib/helpers.bash Outdated Show resolved Hide resolved
@gaelicWizard
Copy link
Contributor Author

the core reason that components bail early and skip activation

It seems like only a few plugins/components actually do this, and it feels like logging about why bailing early should be in the plugin. If bash-it doctor prints Command 'git' does not exist! without mentioning plugin/git, it isn't super helpful for a user who isn't already very familiar with Bash and/or Bash It. The log message should be much clearer, like plugin/git: skipping plugin activation because 'git' cannot be found or similar. It would need to be different for every component, and sometimes only part of a component is skipped, e.g. the alias for t, the alias for fuck, &c.

I definitely agree there should be some message when a plugin/completion is skipped, but it should be logged from the component script not the _command_exists function. I can do a PR to update everywhere that this is needed if you agree that's the way to go.

@gaelicWizard gaelicWizard force-pushed the lib/helpers-base branch 3 times, most recently from 3efdb38 to 2b4eee3 Compare January 4, 2022 21:53
@gaelicWizard
Copy link
Contributor Author

gaelicWizard commented Jan 4, 2022

@davidpfarrell, here's an example I just made up for plugin/less-pretty-cat:

if ! _command_exists pygmentize; then
	_log_warning "${BASH_SOURCE[0]##*/}: unable to initialize without '$_' installed."
	return 1
fi

Since we're bailing early, we can't just return from a function; it needs to be in the file itself.

Compare to plugin/zoxide:

if _command_exists zoxide; then
	eval "$(zoxide init bash)"
else
	_log_error 'zoxide not found, please install it from https://github.com/ajeetdsouza/zoxide'
fi

EDIT: ok, well, it seems I've been nerd-sniped in to this being my next project. I do need at least one specific bit of assistance: should this be _log_warning or _log_error? My intuition is that ń̥o҉̙̻̖̲̮͓̩̀t̵̬̳͔ḩ̡̪͇͈i̼̦̮̙͝n̸̸̲̲͍̟g͏̭͖̠̩̭ is broken. It's just not there. An error would be if we didn't bail early and then tried to use the missing app and things went sideways, no?

@gaelicWizard
Copy link
Contributor Author

Ok, I've got a branch I'm working on for early-bail from plugins that require other commands but it's way to big for this PR simply due to the number of files. I think this branch should be good to merge, and then I'll open a new PR when I've got all the plugins ready and we can revisit how the logging should flow then. 👍

@NoahGorny
Copy link
Member

@davidpfarrell, here's an example I just made up for plugin/less-pretty-cat:

if ! _command_exists pygmentize; then
	_log_warning "${BASH_SOURCE[0]##*/}: unable to initialize without '$_' installed."
	return 1
fi

Since we're bailing early, we can't just return from a function; it needs to be in the file itself.

Compare to plugin/zoxide:

if _command_exists zoxide; then
	eval "$(zoxide init bash)"
else
	_log_error 'zoxide not found, please install it from https://github.com/ajeetdsouza/zoxide'
fi

EDIT: ok, well, it seems I've been nerd-sniped in to this being my next project. I do need at least one specific bit of assistance: should this be _log_warning or _log_error? My intuition is that ń̥o҉̙̻̖̲̮͓̩̀t̵̬̳͔ḩ̡̪͇͈i̼̦̮̙͝n̸̸̲̲͍̟g͏̭͖̠̩̭ is broken. It's just not there. An error would be if we didn't bail early and then tried to use the missing app and things went sideways, no?

I would say its a warning and not an error- error is something truly not working properly in Bash-it IMO

Copy link
Contributor

@davidpfarrell davidpfarrell left a comment

Choose a reason for hiding this comment

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

Approved !

- Don't write to disk, just pipe.
- Don't loop, just do all functions.

Performance of old implementation on my system:
```
real    0m9.996s
user    0m5.318s
sys     0m9.126s
```

Performance of new implementation on my system:
```
real    0m0.052s
user    0m0.069s
sys     0m0.025s
```
The weird subshell is weird AF. Just do a normal `if`.

Ditto `_binary_exists()`, `_completion_exists()`, and `_is_function()`!
@NoahGorny NoahGorny merged commit 4ba1ab9 into Bash-it:master Jan 24, 2022
@gaelicWizard gaelicWizard deleted the lib/helpers-base branch January 25, 2022 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants