Skip to content

Don't read configuration if not interactive #5394

Open

Description

@faho

One of the nice things of fish is that we don't have anything as complicated as the bash man page's "INVOCATION" section in our documentation.

"If it's a non-interactive shell, it does not read bashrc, except when it has been invoked by ssh or rshd, unless it is invoked as sh".

We always read config.fish, and that's nice.

However, as we see in e.g. #5246 and #4432, doing that even when non-interactive can cause some issues. Issues that we currently expect the user to expect, and guard against with if status is-interactive.

And I'm not really sure if configuration of non-interactive shells is ever really wanted? If you execute a script via fish /path/to/script (or directly via the shebang), you expect a consistent environment.

So, I would argue that we should possibly change this:

  • Don't source /etc/fish/config.fish and ~/.config/fish/config.fish (the one in /usr can stay, but that's not really "config")

  • Don't read snippets (which are technically in /usr/share/fish/config.fish)

  • Don't add the user's functions (in $HOME and /etc) to $fish_function_path

Now, obviously this complicates the logic a bit. It's "if fish is interactive, read config.fish, if not, don't" instead of just "read config.fish". I'd argue that it's still quite simple, and helps unbreak things that are currently quite easy to break - understanding that stty touches the terminal is advanced stuff.

Additonally, if configuration of non-interactive shells is wanted (and I don't see how, given that bash doesn't have it), we could add a "config-noninteractive.fish", or simply expect users to run fish -C 'source ~/.config/fish/config.fish.

Activity

  1. added this to the fish-future milestone on Dec 7, 2018
  2. zanchey commented on Dec 8, 2018

    @zanchey
    Member

    I'm against this. I do have stuff in my non-interactive startup config (path editing dependent on machine, for example), and I use my custom functions from non-interactive mode all the time.

  3. faho commented on Dec 8, 2018

    @faho
    MemberAuthor

    I do have stuff in my non-interactive startup config (path editing dependent on machine, for example)

    Could you provide some more detail?

    Like I said, it's possible that non-interactive configuration is needed, I just believe that the default is backwards. Most configuration should not apply to non-interactive shells, and the current solution is a footgun.

    path editing dependent on machine

    You mean $PATH? Shouldn't that be done once in the login shell and then passed down? Or in e.g. pam_env? (There's a bit of an annoyance here in that much of linux expects a shell to run in the startup path, and specifically one that can read /etc/profile* - but fish can't do that to begin with)

    I use my custom functions from non-interactive mode all the time.

    That's the one I was least sure about. Do users expect functions to be available? They aren't as much of an issue as the configuration is (autoloading prevents them from doing stuff to the terminal in the startup path, unless that calls them), so it might also be okay to leave $fish_function_path unchanged.

    Though that of course means it's different between interactive and non-interactive shells if your configuration changes $fish_function_path (mine does, but explicitly for interactive shells).

  4. ridiculousfish commented on Dec 11, 2018

    @ridiculousfish
    Member

    I'm against this too for the reason given in the intro. If we don't source config.fish, then we will get requests for how to configure fish in non-interactive mode, and there will be pressure to add a non-interactive config file, and then we're well on the road to bash.

    It's true that surprising consequences erupt when configs designed for interactive mode are sourced in non-interactive mode. I believe it's worth tolerating this to maintain the simple initialization story.

    Now there exists a function __fish_config_interactive, and it seems OK for it to invoke a function fish_user_config_interactive or similar. We can then document that interactive-only configs should go in this function. That seems better to me: when initialization happens in fish script then it is easier for users to inspect and understand.

  5. faho commented on Dec 11, 2018

    @faho
    MemberAuthor

    it seems OK for it to invoke a function fish_user_config_interactive or similar.

    That doesn't seem much better than if status is-interactive.

    Plus, the problem I see is really one of defaults. Currently, everything in the config is executed in non-interactive fish by default. You need to do things to stop that from happening. So you need to do things in the majority of cases, even if you don't even know that you need to do things.

    I'd like to figure out a way to turn that around.

  6. mqudsi commented on Dec 17, 2018

    @mqudsi
    Contributor

    See also #5417 which is at least gives the user the option of explicitly opting out of loading config.fish.

  7. faho commented on Dec 30, 2019

    @faho
    MemberAuthor

    So I'd like to get back to this.

    That's because this is necessary if we expect fish to be used via shebanged scripts.

    And in that case, a commandline option to fish doesn't work, because fish might be opened via /usr/bin/env, and linux doesn't split any arguments beyond the first - so #!/usr/bin/env fish --no-config would try to open a file called fish --no-config.

    If never reading config.fish for non-interactive shells is unacceptable, there are other ideas we could talk about, including:

    • Use config-noninteractive.fish if that exists instead of config.fish, so users can touch it to remove their customization from non-interactive mode
    • Allow config-interactive.fish so users can use that
    • Use config-noninteractive.fish in non-interactive mode, so users have to add source config.fish to that to enable the same customizations in non-interactive mode

    (these would also include setting $fish_function_path and reading snippets - so we'd have to add a bit to read the snippets to the third option)

    I realize that this complicates our startup logic a bit, but I don't think it's much, and I feel it's worth it. We're not adding 5 legacy paths to check, we're not differentiating login shells, but interactive and non-interactive shells are just quite different things.

  8. zanchey commented on Jan 2, 2020

    @zanchey
    Member

    I do have stuff in my non-interactive startup config (path editing dependent on machine, for example)

    Could you provide some more detail?

    For example:

    • keychain
    • path management, which I can't set up using the login mechanism (no root) or pam_env (not available)
  9. krobelus commented on Jan 2, 2020

    @krobelus
    Contributor

    I think it's a great default that fish -c foo behaves just like interactive foo.
    Sane config.fish/functions/snippets should not break reasonable scripts, but I guess script authors don't want to rely on that..

    And in that case, a commandline option to fish doesn't work, because fish might be opened via /usr/bin/env, and linux doesn't split any arguments beyond the first - so #!/usr/bin/env fish --no-config would try to open a file called fish --no-config.

    The -S (split-string) option to env should be available, right?

  10. faho commented on Jan 2, 2020

    @faho
    MemberAuthor

    The -S (split-string) option to env should be available, right?

    Not in Posix and... basically nobody uses it, or thinks of using it? Even if people figured that fish --norc should be used (they most likely won't), the chance of somebody finding env -S is not good.

    So for portable scripts it's out (discounting of course that /usr/bin/env is technically not even guaranteed to be the path to env, but I have zero sympathy for systems that don't have that).

    Sane config.fish/functions/snippets should not break reasonable scripts, but I guess script authors don't want to rely on that..

    Most people don't think of non-interactive scripts when configuring their shell for interactive use. We've had people make a test function and not notice it for a bit. Making e.g. a grep alias that does --color=always would be even harder to find.

    Also plenty of configurations just do some slow interactive setup, and that's just wasteful and makes fish for scripts appear worse.

    Basically it's needless footguns, is my position.


    (Also, weirdly enough keychain states "Currently, keychain is not compatible with Fish shell.")

  11. RealFerdinandDeSaussure commented on Mar 1, 2020

    @RealFerdinandDeSaussure

    I just stumbled across this and have to say, I would be in hard favour of not sourcing config.fish whenever fish is launched non-interactively. The way it is now, it is very hard to write scripts that can be shared in any way because you'd always have to accomodate for potential quirks in some user's config. This effectively makes fish script useless as a general scripting language for anything other than customising your own shell.
    If you really need one or two of your functions in your personal scripts, couldn't you just source those functions from within the script and then run them? Not very elegant, I admit, but that script is designed to be used by noone but yourself anyway.

  12. Korkman commented on May 10, 2020

    @Korkman

    How about a custom she-bang

    #! /usr/bin/fish_script
    

    typically symlinked to fish and if it is called like that, skip config?

    I think the current default loading the system config is fine. And I would always assume someone willing to write portable scripts to go an extra mile in the documentation to find such a quirk. Ensuring portability is hard anyways because shell scripts rely so heavily on external programs and their expected behavior.

  13. IlanCosman commented on Aug 14, 2020

    @IlanCosman
    Contributor

    I recently ran into this issue. I'm amazed I didn't think of status is-interactive until reading this thread, I was doing all sorts of contortions to avoid loading stuff.

    I think that non-interactive fish should still load the config by default, but that there should be a fish flag to load any directory or none at all.

  14. 8 remaining items

  15. realmazharhussain commented on Jun 13, 2021

    @realmazharhussain
  16. madprops commented on Nov 23, 2021

    @madprops
  17. faho commented on Nov 23, 2021

    @faho
    Author
  18. philn128 commented on Jan 7, 2022

    @philn128

    Just ran into this today. I had an environment variable in my ~/.config/fish/config.fish that was overriding an environment variable I had manually set; definitely unexpected behavior. IF someone actually wanted to source config.fish all they'd have to do is

    #!/usr/bin/env fish
    source $__fish_config/config.fish

    Nonetheless, I now have at the top of my config.fish

    if not status --is-interactive
    	exit
    end

    so I suppose this issue is benign.

  19. lgarron commented on Sep 28, 2023

    @lgarron
    Contributor

    I ran into this issue in a GitHub codespace with fish 3.6.1.

    #!/usr/bin/env fish does not load config.fish on macOS, and I don't recall it doing so in codespaces in the past. But it's doing so now!

    And in that case, a commandline option to fish doesn't work, because fish might be opened via /usr/bin/env, and linux doesn't split any arguments beyond the first - so #!/usr/bin/env fish --no-config would try to open a file called fish --no-config.

    The -S (split-string) option to env should be available, right?

    This workaround seems to work for me for now.

    I've written shell scripts for a long time now and never knew about that caveat, so this sure seems extremely non-obvious.

    But these scripts are not supposed to load config.fish, so the status --is-interactive workaround would be insufficient if anyone else ever used them.

    I guess I'm fine with this, but is there a good explanation for why /usr/bin/env fish doesn't seem to load config.fish on macOS? Any reason I should not expect this to be consistent across POSIX-like systems?

  20. lgarron commented on Sep 28, 2023

    @lgarron
    Contributor

    I've written shell scripts for a long time now and never knew about that caveat, so this sure seems extremely non-obvious.

    In fact, seems like this is so non-obvious that none of the fish shell scripts in this repo do it:

    https://github.com/search?q=repo%3Afish-shell%2Ffish-shell+%2F%23%21%5C%2Fusr%5C%2Fbin%5C%2Fenv.*fish%2F&type=code

  21. faho commented on Sep 28, 2023

    @faho
    MemberAuthor

    In fact, seems like this is so non-obvious that none of the fish shell scripts in this repo do it:

    They can't do it because env might not have a -S option.

    It's not just that it's non-obvious, it's that there is no portable way to do it.

    I guess I'm fine with this, but is there a good explanation for why /usr/bin/env fish doesn't seem to load config.fish on macOS?

    It should, I don't know why it wouldn't. Maybe $XDG_CONFIG_HOME is set to something in that context?

  22. lgarron commented on Sep 28, 2023

    @lgarron
    Contributor

    They can't do it because env might not have a -S option.

    It's not just that it's non-obvious, it's that there is no portable way to do it.

    Ah, I see. It would definitely be very useful to have a portable option.

    It should, I don't know why it wouldn't. Maybe $XDG_CONFIG_HOME is set to something in that context?

    Looks like it's empty. 🤷‍♀️

  23. faho commented on Sep 28, 2023

    @faho
    MemberAuthor

    Looks like it's empty. 🤷‍♀️

    Try setting $FISH_DEBUG to "config" and then run the file. E.g.

    FISH_DEBUG=config ./foo.fish

    This will print output like:

    config: exec_path: "/usr/local/bin/fish", argv[0]: "fish"
    config: Using compiled in paths:
    config: determine_config_directory_paths() results:
    paths.data: /usr/local/share/fish
    paths.sysconf: /usr/local/etc/fish
    paths.doc: /usr/local/share/doc/fish
    paths.bin: /usr/local/bin
    config: sourcing /usr/local/share/fish/config.fish
    config: sourcing /usr/local/etc/fish/config.fish
    config: sourcing /home/user/.config/fish/config.fish

    Those last three lines will tell you exactly which files it ran. If it did, check the files to see if you have a status is-interactive in there somewhere.

  24. lgarron commented on Sep 28, 2023

    @lgarron
    Contributor

    Aha, thanks!

    Looks like it was always (unintentionally) loading config.fish on macOS as well as codespaces. It's not until recently that something in Codespaces changed so that config.fish would print something for me! 😳

    > FISH_DEBUG=config git-distance origin/main
    config: exec_path: '/opt/homebrew/Cellar/fish/3.6.1/bin/fish', argv[0]: 'fish'
    config: Using compiled in paths:
    config: determine_config_directory_paths() results:
    paths.data: /opt/homebrew/Cellar/fish/3.6.1/share/fish
    paths.sysconf: /opt/homebrew/etc/fish
    paths.doc: /opt/homebrew/Cellar/fish/3.6.1/share/doc/fish
    paths.bin: /opt/homebrew/Cellar/fish/3.6.1/bin
    config: sourcing /opt/homebrew/Cellar/fish/3.6.1/share/fish/config.fish
    config: sourcing /opt/homebrew/etc/fish/config.fish
    config: sourcing /Users/lgarron/.config/fish/config.fish
    WARNING: Loaded `config.fish` outside an interactive shell.
    0 commit(s) on main
    0 commit(s) on origin/main
    
  25. Ilan-Sperber commented on Oct 5, 2026

    @Ilan-Sperber

    I wanted to ask if the discussion on this topic is ongoing? There are numerous proposed solutions here, but as it is a fish script isn't useful outside one's own setup unless they do weird complicated things to their configs or symlinks that make the current setup more complex than using a bash script. I have used bash scripts to make custom commands for a while, and used other people's, and I've greatly enjoyed being able to use a bash script as an executable. Currently, fish just can't work for a portable executable.

    I think this is a big loss also, that greatly limits what fish is as a shell. I'd like to further add that fish aims at being user friendly, but I cannot look at a fish script and know what it might do. I have personally found fish to be great for interactive use, but less user friendly as a scripting language only because of this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions