-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changing environment from inside a block #4771
Comments
Here is a workaround I used for something similar:
|
good point @jmoore34, as long as the |
I'm using that, but it only works when you are doing a
This works great, but throws an extra error if the current directory does not exist (i.e. has been deleted). |
@ajeetdsouza i'm not sure how applicable is but |
I've updated the title to reflect my more general use case. For example, I'd like to be able to change an environment variable from within |
@ajeetdsouza Can you give an example how you would use changing environment variables from inside In general, I think it again comes to the design limitations of the language where we don't allow modifying a global scope. For example, scoped environment variables are necessary to be able to safely implement However, we were thinking about implementing hooks when you enter a directory (and possibly other cases) so maybe that would help your use case? But that will be after 0.60. |
This is exactly what I wanted to do. If I have hooks, I probably wouldn't need this feature. However, it seems inconsistent to me that the language allows something like |
We're generally not keen on things modifying the outer scope, but you're right, it's not consistent. Theoretically, we could generalize it and have something like "env-block" so that |
I was bitten by the same issue ( I found a simple workaround (though not ideal, as it can significantly alter how a function looks, especially since variables are immutable): build a record of environment variable entries in the function, and then call let new-env = if $foo { {FOO: 1} } else { {} }
load-env $new-env |
@71 Currently, we follow a simple procedure: all blocks are scoped, except I'd personally lean towards removing |
@kubouch I understand this, but since this requires some design discussion I commented to ask for a temporary improvement (warning about why |
This should be quite a bit easier now. Blocks can update the environment (closures can not). Commands like
And the environment inside the function will update. Using that in combination with |
Related problem
I need a way to conditionally
cd
into a directory. I need this to implement thez
function in zoxide. I know that usingdef-env
allows us to set environment variables from within a function, but how can I do this inside an if-else block?Describe the solution you'd like
Perhaps an
if-env
keyword?Describe alternatives you've considered
No response
Additional context and details
No response
The text was updated successfully, but these errors were encountered: