Skip to content

Introduce path variables - #5245

Closed
ridiculousfish wants to merge 3 commits into
fish-shell:masterfrom
ridiculousfish:splitenv_1.8
Closed

ridiculousfish wants to merge 3 commits into
fish-shell:masterfrom
ridiculousfish:splitenv_1.8

Conversation

@ridiculousfish

Copy link
Copy Markdown
Member

This PR is another go at #436, more ambitious than #5188.

This introduces a notion of "path variables." Path variables provide special support for colon-delimited variables including PATH, PYTHONPATH, GOPATH, etc.

Path variables act as normal variables except:

  1. They are joined by : instead of space inside quotes. echo "$PATH" will have colons.
  2. They are implicitly split by colons. That is, set PATH "/tmp/bin:$PATH" will no longer result in a bogus PATH. Path variables cannot contain colons themselves.

Variables that end in "PATH" are automatically path variables. Variables can be queried for their path status (though somewhat awkwardly) via set -q.

Users can mark a variable as path via set --path MYVAR SOMEVALUE, or set --unpath to mark it as not a path. This property is tied to a variable (not its name) similar to --export and --unexport.

I played around with a notion of having each variable track a settable delimiter but found it to be too complex; I think having specific support for path-style variable is just right and not overly constraining for the future.

Unanswered questions:

  1. Should non-path variables be exported separated by spaces or colons? (Spaces I suppose?)
  2. Do we still want splitenv with this version? (I think not).
  3. Universal variables will need to support these, which means we'll need to modify the universal variable file format. This is a good release to do so, since the file moved so there's no backwards compatibility concern. That will probably be a separate PR.

This commit begins to bake in a notion of path-style variables.

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. Introduces a notion of "path variable." A path variable will be
"colon-delimited" which means it gets colon-separated in quoted expansion,
and automatically splits on colons. In this commit we only do the exporting
part.

Colons are not escaped in exporting; 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).
This switches quoted expansion like "$foo" to use foo's delimiter instead of
space. The delimiter is space for normal variables and colonf or path variables.
Expansions like "$PATH" will now expand using ':'.
This adds flags --path and --unpath to builtin set, analogous to
--export and --unexport. These flags change whether a variable is
marked as a path variable.

Universal variables cannot yet be path variables.
@zanchey

zanchey commented Oct 10, 2018

Copy link
Copy Markdown
Member

How does this work across sessions? For example, if I have set SOMEPATH /foo/:/blah, because I like putting silly characters in my directory names, should I use set --unpath in my config.fish? Will that automatically recongeal it?

@ridiculousfish

Copy link
Copy Markdown
Member Author

If you want to set a variable to a fixed value without inferring its path status, you can do it:

 set --unpath SOMEPATH /foo/:/blah

To "recongeal" a path variable to a single string with colons:

set --unpath SOMEPATH "$SOMEPATH"

This is a little sneaky:

  1. First $SOMEPATH is expanded. It's still a path variable so it gets colons.
  2. set receives --unpath so it doesn't split on those colons.

@faho

faho commented Oct 11, 2018

Copy link
Copy Markdown
Member

This property is tied to a variable (not its name) similar to --export and --unexport.

I'm not entirely sure what that means. Do you mean it needs to be specified every time, otherwise we fall back to the default?

I.e.

set --path foo bar:baz # results in two elements
set foo bingo:bongo # results in one element because it reverts to not a path

set --unpath FOOPATH bingo:bongo # one element
set FOOPATH bar:baz # two elements because it reverts to a path

That seems like it would make non-default variables a tad annoying. What's the rationale here?

@faho

faho commented Oct 11, 2018 •

Copy link
Copy Markdown
Member

Should non-path variables be exported separated by spaces or colons? (Spaces I suppose?)

Space is the most generally useful. I suppose that means we've given up on inheriting lists?

Do we still want splitenv with this version? (I think not).

Doesn't seem all that useful, no.

@ridiculousfish

Copy link
Copy Markdown
Member Author

I'm not entirely sure what that means. Do you mean it needs to be specified every time, otherwise we fall back to the default?

No, if you don't specify path or unpath, it uses the existing status (if any), or inherits from a parent (if any), or lastly falls back to the default name-based heuristic.

More precisely, the behavior is:

  1. New variables inherit the path status of variables in parent scopes. So if you set a path variable in the global scope, and override in it in local scopes, the local gets the path status of the global.

  2. If you modify an existing variable, and don't specify --path or --unpath, it retains its old path status.

  3. If you do explicitly specify --path or --unpath, that's scoped the same as the variable. So for example you can have a local path variable shadow a global non-path variable.

This matches the behavior of --export and --unexport.

@faho

faho commented Oct 12, 2018

Copy link
Copy Markdown
Member

No, if you don't specify path or unpath, it uses the existing status (if any), or inherits from a parent (if any), or lastly falls back to the default name-based heuristic.

That'll work!

@faho faho added enhancement release notes Something that is or should be mentioned in the release notes RFC labels Oct 12, 2018
@faho faho added this to the fish-3.0 milestone Oct 12, 2018
ridiculousfish added a commit to ridiculousfish/fish-shell that referenced this pull request Nov 24, 2018
This merges support for PATH variables. Closes fish-shell#5245
razzolini added a commit to razzolini/dotfiles that referenced this pull request Jan 11, 2019
Since version 3.0, fish treats all variables with names ending in PATH
as colon-separated, and splits them automatically on assignment.
See fish-shell/fish-shell#5245
@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

enhancement release notes Something that is or should be mentioned in the release notes RFC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants