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

Add option nounset to default to strict parsing. #17

Merged
merged 1 commit into from
Jul 28, 2020

Conversation

sayanarijit
Copy link
Owner

nounset=True will behave like bash's set -u or set -o nounset
i.e. when any variable is undefined, it will raise UnboundVariable
exception.

This is useful where using the ${VAR:?} syntax is not an option.

@codecov-commenter
Copy link

codecov-commenter commented Jul 28, 2020

Codecov Report

Merging #17 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #17   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          196       208   +12     
=========================================
+ Hits           196       208   +12     
Impacted Files Coverage Δ
expandvars.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bf2d20b...2ebaf8e. Read the comment docs.

README.md Outdated
If you want to temporarily disable strict parsing both for `nounset=True` and the `${VAR:?}` syntax, set environment variable `RECOVER_NULL=somevalue`.

```bash
export RECOVER_NULL=foo
Copy link

Choose a reason for hiding this comment

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

Doing this with an environment variable seems very dangerous. If you have a pipeline that involves your own tool but also someone else's tool that happens to use expandvars, then this other tool can start behaving wrong.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Hi. Valid point too. We shouldn't use export. But this feature comes in handy so often that I'd still keep it.
I have added WARNINGs to remind the consequences of using export.

Copy link

Choose a reason for hiding this comment

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

The problem is that some of the people who would like to see the warning won't be able to see it. If Alice and Bob each create a tool, and I put them both in my stack, then Bob's tool can break when Alice's tool has a script that says "RECOVER_ALL=foo". I'm just an unsuspecting user who would be bitten by this (I probably don't even know that the tools I am relying on use expandvars internally).

Copy link
Owner Author

Choose a reason for hiding this comment

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

Sorry I misspelled it in the first PR (corrected it now). The name is actually EXPANDVARS_RECOVER_NULL. I doubt someone will use this name unintentionally.

@sayanarijit sayanarijit force-pushed the add/feature-nounset branch 3 times, most recently from 3eea4e2 to 65547a5 Compare July 28, 2020 07:02
`nounset=True` will behave like bash's `set -u` or `set -o nounset`
i.e. when any variable is undefined, it will raise `UnboundVariable`
exception.

This is useful where using the `${VAR:?}` syntax is not an option.
@mnieber
Copy link

mnieber commented Jul 28, 2020 via email

@sayanarijit
Copy link
Owner Author

In that case, I think it will be a fault on the tool developer side. Just like it's a common knowledge that we shouldn't update PATH and other critical environment variables in a script without knowing what we're doing, it's the same here. This variable must be used while running the whole toolchain. Not individual tools. For individual tools, if bypassing strictness is needed and using this variable seems to be the only way, that tool should set the variable in a temporary manner. Without exporting it. Just like we'd temporarily update PATH.

@sayanarijit
Copy link
Owner Author

If you think I misunderstood again, could you please give a real world example? Or a real world scenario? It'll be easier that way.

@mnieber
Copy link

mnieber commented Jul 28, 2020 via email

@sayanarijit
Copy link
Owner Author

I might add a new tool that sets EXPANDVARS_RECOVER_NULL

If that toos sets the variable locally, it shouldn't affect the other tools. If it exports the variable globally, that tool might also be exporting critical variables like PATH and USER globally. You probably should avoid using anything that depends on this tool.

@mnieber
Copy link

mnieber commented Jul 28, 2020

| You probably should avoid using anything that depends on this tool.

How can I find out what depends on this tool?

@sayanarijit
Copy link
Owner Author

If adding a tool breaks my system, it means it's "incompatible" with my system. Then I either find a compatible alternative, or investigate why is it incompatible. If after using a tool I see it's messing with my system global variables, I'd be uncomfortable using that tool. On the other hand, it's that tool has to set global environment variable like tmux sets "TMUX" and "TMUX_PANE" and python depends on "PYTHON_PATH" and "PYTHON_HOME", there's a best practice to prefix the variable name properly like "EXPANDVARS_*". If it still sets "EXPANDVARS_RECOVER_NULL" for some other purpose, then it's just an incompatibility issue I don't see ever happening.

And if is sets "EXPANDVARS_RECOVER_NULL" intentionally to influence other tools, Just like direnv and nix does, it should know what it's doing. It's the responsibility of that tool's developer to make it compatible with other tools.

@mnieber
Copy link

mnieber commented Jul 28, 2020

You said it yourself (not me!): You probably should avoid using anything that depends on this tool.
Then I asked "How can I find out what depends on this tool?" and (as you realize) this question is impossible to answer.
Sorry, I will leave it here. I do like the package though, very useful.

@sayanarijit
Copy link
Owner Author

Thanks 😄 ...

Sorry I can't give a better answer than that. This little feature adds a lot of convenience at the cost an incompatibility issue that's very unlikely to happen. Hence, I'm fine with the trade-off.

@mnieber
Copy link

mnieber commented Jul 28, 2020 via email

@sayanarijit
Copy link
Owner Author

Alright. I'll create a separate issue to see what other users of this library feel about this.

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