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

test and document var x = $(false) #942

Closed
andychu opened this issue May 15, 2021 · 12 comments
Closed

test and document var x = $(false) #942

andychu opened this issue May 15, 2021 · 12 comments

Comments

@andychu
Copy link
Contributor

andychu commented May 15, 2021

  • do var, const, setvar have exit codes?
    • maybe you should document it as not the LAST one, but as the GREATEST exit code?
  • should it temporarily turn on command_sub_errexit so it fails early?
@andychu
Copy link
Contributor Author

andychu commented May 31, 2021

Yeah var fails with bin/oil, but not bin/osh, which is not desirable.

$ bin/osh -c 'var x = $(false); echo $?'
0

I think it should exit with the last one/?

var x = "$(false)-$(exit 42)"

andychu pushed a commit that referenced this issue May 31, 2021
Addresses issue #942

Unrelated: notes on how to create an empty shell VM for config file
evaluation
@andychu
Copy link
Contributor Author

andychu commented Jun 28, 2021

Idea: what happens when myfunc(x) returns an error? The only way you can catch it is like this?

if ! var result = myfunc(x) {
  echo 'error'
}

This is invalid syntax!

$ bin/oil -c 'if var x = 42 { echo $x }'
  if var x = 42 { echo $x }
                ^
[ -c flag ]:1: Syntax error in expression (near Id.Op_LBrace)

@andychu
Copy link
Contributor Author

andychu commented Jul 5, 2021

Another syntactic issue with error handling: Checking the return value of procs that take blocks

I think you have to use

try --assign :status myproc {
  echo $x
}
echo $status

@andychu
Copy link
Contributor Author

andychu commented Jul 18, 2021

This is part of #477 , but it's worth having our own issue

@andychu
Copy link
Contributor Author

andychu commented Aug 28, 2021

Note on the design problem

$ bin/oil -c 'if { var x = 42 }; then echo hi; fi
 '
  if { var x = 42 }; then echo hi; fi
                   ^
[ -c flag ]:1: fatal: strict_errexit only allows simple commands (got command.Sentence). Hint: use 'run'.

TODO: also fix run, which was renamed to try

@andychu
Copy link
Contributor Author

andychu commented Aug 28, 2021

Crazy idea:

if try (var x = 42) {

Except this means that () has statements

if try { var x = f(x) } { echo ok }

hm this is weird, I think it runs into a parsing issue

@andychu
Copy link
Contributor Author

andychu commented Aug 28, 2021

Maybe, some kind of RHS assignment operator

_ f(42) => var status

var x = f(42) => var status  # feels a little weird

Or simply use $?

shopt --unset errexit {
  var x = f(42)
}
echo "status = $?"

Doesn't work with try --assign but that might be OK

@andychu
Copy link
Contributor Author

andychu commented Aug 29, 2021

try --assign :st {
  var x = f(42)
  var y = f(0)
}
if (st != 0) {
  echo "failed"
}

Hm this is not horrible ...

It's long but we want to discourage this style I think ... functions should mostly be "pure".

However it may be a challenge to people who want to import Python/JS knowledge ...

try {
  var x = f(42)
  var y = f(0)
}

This really isn't any different than

var x = f(42)
var y = f(0)

@andychu
Copy link
Contributor Author

andychu commented Apr 24, 2022

@andychu
Copy link
Contributor Author

andychu commented Apr 25, 2022

https://oilshell.zulipchat.com/#narrow/stream/121540-oil-discuss/topic/errexit.20issue.20and.20docs

I suggest

trycatch {
  var x = 1 / 0
}
echo "status = $?"

Not sure how common it will be ?

andychu pushed a commit that referenced this issue Apr 30, 2022
So then errexit can take over.  This fixes uncaught IndexError,
KeyError, ZeroDivisionError, etc.

We still don't have a good story for TypeError.  There is a one-off
around one EvalExpr.

Addresses #942.
@andychu
Copy link
Contributor Author

andychu commented Apr 30, 2022

I defined expression errors to be status 3.

var x = $(false) respects command_sub_errexit, so it won't fail if it's off. This is perhaps not ideal but it's consistent.

andychu pushed a commit that referenced this issue Apr 30, 2022
In other words, this is FATAL even in bin/osh.

    var x = $(false)

You must use the 'try' builtin to recover from it.

The 'var' keyword is a new construct that doesn't require compatibility,
so we it should have the right behavior in bin/osh.

This finishes #942.
@andychu
Copy link
Contributor Author

andychu commented May 6, 2022

@andychu andychu closed this as completed May 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant