Skip to content

fix: use environment variables to prevent command_not_found from recursing - #11090

Merged
kubouch merged 6 commits into
nushell:mainfrom
lavafroth:fix-command-not-found-overflow
Mar 21, 2024
Merged

kubouch merged 6 commits into
nushell:mainfrom
lavafroth:fix-command-not-found-overflow

Conversation

@lavafroth

@lavafroth lavafroth commented Nov 18, 2023 •

Copy link
Copy Markdown
Contributor

Description

When the command_not_found hook is entered, we set an environment variable for context. If an unknown command is encountered and the command_not_found context environment variable is already present, it implies a command in the hook closure is also not found. We stop the recursion right there.

User-Facing Changes

Incorrect command_not_found hooks can be caught without panicking.

Tests + Formatting

Tests are passing.

After Submitting

When the command_not_found hook is entered, we add an overlay for context. If an unknown command is encountered and the command_not_found context overlay is already present, it implies a command in the hook closure is also not found.
@lavafroth
lavafroth marked this pull request as draft November 18, 2023 10:30
@lavafroth

Copy link
Copy Markdown
Contributor Author

I'm not a 100% satisfied with this. The user or a script could create an overlay called command_not_found which would prevent the hook from running at all. Is there any other way to internally persist a state on the stack or do I need to add a new property like called_command_not_found to some struct to make this invalid state unrepresentable?

@fdncred

fdncred commented Mar 18, 2024

Copy link
Copy Markdown
Contributor

@kubouch any ideas about this one? Seems like we've missed responding to it. Sorry @lavafroth!

@kubouch

kubouch commented Mar 18, 2024

Copy link
Copy Markdown
Contributor

Oops, sorry about the miss! The overlay solution is a hack, I would avoid it. How about adding an environment variable when running the hook, then checking for it?

@lavafroth

Copy link
Copy Markdown
Contributor Author

@kubouch, that's correct. The overlay solution is definitely a hack. The only proper ways I can see this done is either using an environment variable as you mentioned or having some global state.

@kubouch

kubouch commented Mar 19, 2024

Copy link
Copy Markdown
Contributor

I think environment variable is OK to use in this scenario, no need for a global state IMO.

@lavafroth lavafroth changed the title fix: use overlays to prevent command_not_found from recursing fix: use environment variables to prevent command_not_found from recursing Mar 19, 2024
@lavafroth
lavafroth marked this pull request as ready for review March 20, 2024 03:03
@lavafroth

lavafroth commented Mar 20, 2024 via email

Copy link
Copy Markdown
Contributor Author

@kubouch

kubouch commented Mar 21, 2024

Copy link
Copy Markdown
Contributor

OK, looks good, let's try it!

@kubouch
kubouch merged commit 02a9203 into nushell:main Mar 21, 2024
@kubouch kubouch added the notes:fixes Noted in "Bug fixes" section label Mar 21, 2024
@kubouch kubouch added this to the v0.92.0 milestone Mar 21, 2024
@lavafroth
lavafroth deleted the fix-command-not-found-overflow branch March 24, 2024 02:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

command-not-found hook can invoke itself, leading to infinite recursion

3 participants