Skip to content

Add support for working with shell options #908

Description

Problem

The double-star (**) glob syntax, meaning a deep recursive directory walk, is commonly used, and is available in Task in sources and generates directives by default.
However, like in other shells, the shell in Task requires the globstar option to be set for the syntax to work.

Because shells are isolated across cmds run by Task, creating a task that uses globstar will require to reset the option in every single command:

tasks:
  default:
    cmds:
      - shopt -s globstar && bin1 ./**
      - shopt -s globstar && bin2 ./**

This is hard to write and read.

Other useful shell options have to be handled in a similarly verbose way.

Request

Allow to set shell options per-task and apply such to all commands.

tasks:
  default:
    shopts:
      - globstar
    cmds:
      - bin1 ./**
      - bin2 ./**

To match generates and sources, the globstar shell option itself should be enabled by default, while any others are to be controlled by the new Taskfile options.

Activity

  1. andreynering commented on Oct 27, 2022

    @andreynering
    Member

    Original discussion: https://discord.com/channels/974121106208354339/1035164287695589457

    If we were going to support a shopts: option, it should certainly be available globally as well, and not only at task level.

    But I'm not sure it's really needed. I'm more inclined to just enable globstar for every command and that would likely be enough.

    Anyone disagrees?

  2. pd93 commented on Oct 27, 2022

    @pd93
    Member

    I'm more inclined to just enable globstar for every command

    I don't have any objection to this. My only concern is that for anyone using ** in their Taskfile without globstar set, this will be a breaking change. As this kind of globing is used for fetching a list of files, this could potentially be dangerous if anyone is passing the result into rm or similar.

    To be clear, I think this is a very uncommon scenario, but worth considering

  3. andreynering commented on Oct 27, 2022

    @andreynering
    Member

    Good point about backward compatibility. Maybe we have to have the option then, and delay the default change to the next major version only.

  4. pd93 commented on Oct 27, 2022

    @pd93
    Member

    @sernot agreed with everything you said. When I said

    I think this is a very uncommon scenario

    I was referring to the number of cases where this would be a breaking change, not the use of globstar itself. As you said, not many people (if any) will be using ** without globstar enabled, but on the slim chance that they are, I think the smart thing to do is to defer this to the next major version.

  5. pd93 commented on Oct 27, 2022

    @pd93
    Member

    I've opened another issue to track changing the default value. This issue can remain focussed on a shopts keyword or similar.

  6. voltangle commented on Oct 27, 2022

    @voltangle

    Gonna drop my two cents here as well
    I am thinking about something like this:

    - shopts:
        - globstar
        - shell: zsh
    

    Or (i like this one more):

    - task-name:
        - shell: zsh
        - shopts:
            - globstar
    

    So basically, adding an ability to include a shell that will be automatically used in every CLI call, will (possibly) eliminate the requirement of having a shebang on top of a script, bc it will not be required if ran from a shell, and also adds an ability to use features exclusive to a shell

    This probably requires a separate issue, but idk, maybe related to this one

  7. pinpox commented on Nov 9, 2022

    @pinpox

    While globbing/globstar is an important option it is not only one. If shell options are to be made configurable, please make it in a more general way, allowing to set the equivalent of set -e and set -o pipefail aswell.

    I stumbled across this issue while trying to find a workaround for #392

  8. linked a pull request that will close this issuefeat: set and shopts directives #929on Nov 10, 2022
  9. cdfa commented on Jan 7, 2023

    @cdfa

    I would love to see #929 merged. A task like:

    pass:
        cmds:
          - set -o pipefail && exit 1 | echo "Hello"

    as suggested in #392 (comment) did not work for me (platform: darwin-aarch64, go-task 3.18.0). Hello gets printed.

    Mysteriously, set -xo pipefail does properly enable pipefail, but it messes up the output (which might also be considered a bug?).
    I also cannot disable the x option afterwards with set +x.

  10. pd93 commented on Jan 7, 2023

    @pd93
    Member

    @cdfa I agree that it would be nice to get this merged. However, as it stands, the implementation isn't perfect. We'd like to see mvdan/sh#962 merged so that we can tidy it up a bit and I simply haven't had time to look into the upstream code yet.

    In the meantime, I've tidied up the PR a bit. Hopefully we can get it merged with the "workaround" in place and make some improvements later.

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

    dep: mvdan/shIssues related to the upstream interpreter used by Task.

    Fields

    No fields configured for feature.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions