Skip to content

Export arrays as colon delimited, and splitenv PATH env vars - #5188

Closed
ridiculousfish wants to merge 2 commits into
fish-shell:masterfrom
ridiculousfish:splitenv
Closed

ridiculousfish wants to merge 2 commits into
fish-shell:masterfrom
ridiculousfish:splitenv

Conversation

@ridiculousfish

Copy link
Copy Markdown
Member

This PR is an attempt to partially solve #436

The big changes here:

  1. Arrays are now exported colon-separated, instead of ASCII record separated
  2. Variables whose names end in PATH are split on colons
  3. The new splitenv function makes it easy to split additional variables on colons; string join can go the other way

The advantage of this approach is that it will do the right thing for nearly all colon-delimited variables: PYTHONPATH, GOPATH, MANPATH, PATH, etc, while not mucking with undelimited variables that might contain colons (e.g. DISPLAY). It is also simple to implement, and provides a simple story for anything it gets wrong (splitenv / string join). It is also compatible with fish 2.x scripts in that PATH and MANPATH are retained as arrays. Overall it is a practical and pragmatic tradeoff, not perfection.

Crucially colons are not escaped. This is to support uses like set MANPATH "/foo:/bar" which ought to just work. Traditionally such paths cannot contain colons and escapes are not honored (e.g. there's no way to add a colon to a PATH segment) so this is not a loss.

One compatibility risk is that we will split some paths where previously we did not. For example, code like this:

set -x PYTHONPATH "$PYTHONPATH:/newpath"

will now fail if the incoming PYTHONPATH contained a colon. We'll have to see whether this is encountered by anyone; if so we might have to expand such variables differently inside quotes (yuck) or do something else more exotic.

A secondary disadvantage is that exported fish arrays will appear as colon delimited strings on recursive invocations of fish. The hypothesis is that this is a rare scenario and can be handled by splitenv.

Universal variables continue to use record separators internally and aren't affected by this.

@ridiculousfish ridiculousfish added this to the fish-3.0 milestone Sep 16, 2018
@mqudsi

mqudsi commented Sep 19, 2018

Copy link
Copy Markdown
Contributor

I'm fine with premise of exporting arrays as colon-delimited strings, but I think that the behavior needs to be consistent with regards to how fish treats them within a session.

i.e. I don't like that this currently happens:

mqudsi@QBook ~/R/fish-shell> env PYTHONPATH="/foo:/bar" fish
...
mqudsi@QBook ~/R/fish-shell> echo \"$PYTHONPATH\"
"/foo" "/bar"
mqudsi@QBook ~/R/fish-shell> set -x PYTHONPATH "/foo:/bar"
mqudsi@QBook ~/R/fish-shell> echo \"$PYTHONPATH\"
"/foo:/bar"
mqudsi@QBook ~/R/fish-shell> set -x PYTHONPATH /foo /bar
mqudsi@QBook ~/R/fish-shell> echo \"$PYTHONPATH\"
"/foo "/bar"

I understand that what I'm seeing here is fish preserving what I typed in as I typed it, but that's only going to cause problems when it comes to appending variables to existing arrays. If fish imports these variables split, then I would expect this to be the output:

mqudsi@QBook ~/R/fish-shell> set -x PYTHONPATH "/foo:/bar"
mqudsi@QBook ~/R/fish-shell> echo \"$PYTHONPATH\"
"/foo" "/bar"
mqudsi@QBook ~/R/fish-shell> set -x PYTHONPATH /foo /bar
mqudsi@QBook ~/R/fish-shell> echo \"$PYTHONPATH\"
"/foo" "/bar"

basically, the same logic applied to incoming variables is applied within a session, namely any variables that are exported should be split on : internally. I think the story there is a lot easier to grok, easier to explain, and easier to debug (output does not depend on how it was entered).

Especially as you mention you have no intention of allowing for escaping the colon (which I'm fine with), which means there can never literally be an array member foo:bar for an exported variable anyway. So there's no benefit in preserving that : in the output.

@ridiculousfish

Copy link
Copy Markdown
Member Author

Thanks. It sounds like there's interest from both mqudsi and faho to make the splitting behavior a property of the variable itself. That will require maintaining some more internal state but is certainly not hard.

splitenv takes a variable name, and sets that variable to its value
but split on colons.

Proposed in fish-shell#436
Prior to this fix, fish would export arrays as ASCII record separator
delimited, except for a whitelist (PATH, CDPATH, MANPATH). This is
surprising and awkward for other programs to deal with, and there's no way
to get similar behavior for other variables like GOPATH or LD_LIBRARY_PATH.

This commit does the following:

1. Exports all arrays as colon delimited strings, instead of RS.

2. When importing environment variables, if the variable ends in PATH,
split it on colons (i.e. splitenv); otherwise do not split it.

Colons are not escaped; this is deliberate to support uses like

   `set -x PYTHONPATH "/foo:/bar"`

which ought to work (and already do, we don't want  to make a compat break
here).

Fixes (or at least mitigates) fish-shell#436
@faho

faho commented Oct 14, 2018

Copy link
Copy Markdown
Member

#5245 sounds much nicer to me, and I think we're in agreement on that, so I'm closing this.

If anyone disagrees and wants to have this instead, speak up!

@faho faho closed this Oct 14, 2018
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants