Storing paths to commands breaks #MG5

Hello,

I was trying to store the paths to non-builtin commands in a map and use them for faster lookup but implementing this is breaking #MG5.

Can someone help with why this might happen?

Hey @sangeetsourav, I totally see why you want to cache paths for performance, but it might not be reliable since PATH can change at runtime. Resolving the command each time helps ensure correctness.

Let me know if you’d like further clarification or help debugging!

Thanks for the super fast reply!

When you say that PATH can change at runtime, do you mean that PATH can change after my program has started running? Right now I only read PATH once at the beginning. Do you suggest reloading the PATH every time before searching for a command?

When you say that PATH can change at runtime, do you mean that PATH can change after my program has started running?

Yes, exactly. A shell should allow and handle changes to environment variables during execution.

For example, a user of your shell might do something like this:

Right now I only read PATH once at the beginning. Do you suggest reloading the PATH every time before searching for a command?

Yes, reloading it each time ensures the shell respects any updates to PATH.

Just want to add to this – it’s not only that PATH might change, it’s also that new programs might get added to PATH when the shell is running. Imagine running an installation script and then trying to execute the installed program from PATH soon after - unless your program walks through the PATH dirs every time it isn’t going to pick it up.

2 Likes

Thank you for informative replies everyone. I decided to drop the path caching idea and I also decided to reload PATH before every search.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.