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

interp: display all Bash's shopt option #883

Merged
merged 1 commit into from
Jun 29, 2022

Conversation

riacataquian
Copy link
Collaborator

@riacataquian riacataquian commented Jun 19, 2022

Trying to set an unsupported but valid Bash option leads to a
potentially confusing error message:

$ gosh -c "shopt -s extglob"
shopt: invalid option name "extglob"

Fix that by handling the unsupported options differently from the
invalid ones:

$ gosh -c "shopt -s extglob"
bash: line 1: shopt: extglob off ("on" not supported)
exit status 1

Additionally, this commit lists all of the Bash options when shopt
without arguments is called and explicitly identify the unsupported
options, for example:

$ gosh -c "shopt"
expand_aliases  off
globstar        off
nullglob        off
// .. cut for brevity
hostcomplete    on      ("off" not supported)
inherit_errexit on      ("off" not supported)
interactive_comments    on      ("off" not supported)

While at it, rewrite the bashOptsTable so that it can keep two option
states: 1) Bash's default options and 2) whether we support it

Fixes #877

@riacataquian riacataquian requested a review from mvdan June 19, 2022 10:52
interp/api.go Outdated Show resolved Hide resolved
@mvdan
Copy link
Owner

mvdan commented Jun 23, 2022

Rather than keeping two booleans for "supported" and "allow override", how about:

default bool // default of "off" or "on" like Bash
supported bool // whether we support the option's non-default state

I think that's a bit easier to understand, and it should be enough information to print what we need:

  1. If an option is supported, we simply print the name and its default, like globstar off.
  2. If an option is not supported and its default is off, we print globstar off ("on" not supported).
  3. If an option is not supported and its default is on, we print globstar on ("off" not supported).

This output is also clearer to the end user, I think.

@mvdan
Copy link
Owner

mvdan commented Jun 23, 2022

I also think we need to record the default values anyway, as I'm fairly sure there are a good number of Bash options that default to "on".

@riacataquian
Copy link
Collaborator Author

That sounds better, thanks! :)

Also, I agree on:

I also think we need to record the default values anyway, as I'm fairly sure there are a good number of Bash options that default to "on".

I briefly mentioned it here #883 (comment), will also include in this PR.

@riacataquian riacataquian marked this pull request as draft June 26, 2022 16:27
@riacataquian riacataquian force-pushed the fix-877 branch 2 times, most recently from 3db2bba to 85f4362 Compare June 26, 2022 17:27
@riacataquian riacataquian marked this pull request as ready for review June 26, 2022 17:38
@riacataquian
Copy link
Collaborator Author

@mvdan could you take a look please? I incorporated the feedbacks above. Thanks!

interp/api.go Outdated Show resolved Hide resolved
interp/builtin.go Outdated Show resolved Hide resolved
interp/builtin.go Outdated Show resolved Hide resolved
interp/api.go Outdated Show resolved Hide resolved
@riacataquian riacataquian force-pushed the fix-877 branch 3 times, most recently from f8b9ca3 to 7a93c0c Compare June 29, 2022 11:01
Trying to set an unsupported but valid Bash option leads to a
potentially confusing error message:
```
$ gosh -c "shopt -s extglob"
shopt: invalid option name "extglob"
```

Fix that by handling the unsupported options differently from the
invalid ones:
```
$ gosh -c "shopt -s extglob"
bash: line 1: shopt: extglob off ("on" not supported)
exit status 1
```

Additionally, this commit lists all of the Bash options when `shopt`
without arguments is called and explicitly identify the unsupported
options, for example:
```
$ gosh -c "shopt"
expand_aliases	off
globstar	off
nullglob	off
// .. cut for brevity
hostcomplete	on	("off" not supported)
inherit_errexit	on	("off" not supported)
interactive_comments	on	("off" not supported)
```

While at it, rewrite the `bashOptsTable` so that it can keep two option
states: 1) Bash's default options and 2) whether we support it

Fixes mvdan#877
@riacataquian riacataquian merged commit 678ce51 into mvdan:master Jun 29, 2022
scop pushed a commit to scop/sh that referenced this pull request Sep 24, 2022
A recent commit, 1eb295c, introduced a regression on Windows.
Programs spawned by interp no longer found other programs in PATH.

The reason turns out to be because those programs saw a Unix-like PATH,
with the list separator being ":" rather than ";",
which breaks Windows programs as they see one single mangled directory.

The actual source of the bug is much older, from eb75bb8f7d:

	For better compatibility with non-unix systems, convert $PATH to a
	unix path list when starting the interpreter. This should work with
	Windows, for example.

The recent commit surfaced the bug by tweaking the environment logic.
Before, converting PATH's list separators was an internal detail,
but now we also export this change and it affects spawned programs.

The added test case fails without the fix:

	--- FAIL: TestRunnerRun/mvdan#883 (0.19s)
		interp_test.go:3201: wrong output in "GOSH_CMD=lookpath $GOSH_PROG":
			want: "cmd found\n"
			got:  "exec: \"cmd\": executable file not found in %PATH%\nexit status 1"

The fix is relatively simple: stop converting PATH's list separators.
I believe the reason I originally added that code was for compatibility,
so that scripts using Unix-like path list separator, such as

	export PATH="${PATH}:${PWD}"

would work as expected on Windows.
However, I struggle to agree with my past self on that approach.
We had no tests for this behavior,
and any script using Unix-like paths in any non-trivial way
would likely need to be adjusted to be compatible with Windows anyway.

Fixes mvdan#768.
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.

interp: display all options for bash's shopt and explicitly state unsupported ones
2 participants