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

feat: add global precondition #1993

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

vmaerten
Copy link
Member

@vmaerten vmaerten commented Jan 5, 2025

Fixes #294

It's a fairly popular feature

@vmaerten
Copy link
Member Author

vmaerten commented Jan 5, 2025

I was wondering how it should behave if an included Taskfile defines global preconditions:

  1. Should it produce an error (similar to dotenv)?
  2. Should all preconditions be merged and applied together?
  3. Should the global preconditions in the included Taskfile be ignored?

I started with the 2 in mind, but I am not sure anymore

Any thought ? cc @pd93 / @andreynering

@pd93
Copy link
Member

pd93 commented Jan 5, 2025

As a user, I would expect 2. This is also the safer option if we maintain the principle of "its safer to not run a task that was intended to run than it is to run one that wasn't."

@vmaerten vmaerten marked this pull request as ready for review January 7, 2025 20:25
@vmaerten
Copy link
Member Author

vmaerten commented Jan 7, 2025

I followed the same pattern we have with vars.
I am not a huge fan of naming preconditions inside preconditions, maybe values ?
I you have some naming ideas

@vmaerten vmaerten requested review from andreynering and pd93 January 7, 2025 20:26
Copy link
Member

@pd93 pd93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! It's definitely a key feature. A couple of bits that need discussion though.

Comment on lines +20 to +23
Precondition struct {
Sh string
Msg string
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved Vars and Var into separate files in another PR. Would be good to follow this pattern here.

@@ -14,7 +14,7 @@ import (
var ErrPreconditionFailed = errors.New("task: precondition not met")

func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *ast.Task) (bool, error) {
for _, p := range t.Preconditions {
for _, p := range append(t.Preconditions, e.Taskfile.Preconditions.Preconditions...) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect the global preconditions to run first. For example:

version: '3'

preconditions:
  - test -f foo.txt

tasks:
  default:
    preconditions:
      - test -f bar.txt
    cmds:
      - echo "default task"

Assuming that neither file exists, I would expect to see

task: `test -f foo.txt` failed
task: precondition not met

Also prefer slices.Concat over append and ... now that its in the std lib.

Suggested change
for _, p := range append(t.Preconditions, e.Taskfile.Preconditions.Preconditions...) {
for _, p := range slices.Concat(e.Taskfile.Preconditions.Preconditions, t.Preconditions) {

t1.Vars.Merge(t2.Vars, include)
t1.Env.Merge(t2.Env, include)
t1.Preconditions.Merge(t2.Preconditions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we don't have scoping, this is going to lead to issues where global preconditions in included Taskfiles are going to impact tasks in parent files. I think this is going to be pretty unintuitive to users at the moment. For example:

# Taskfile.yml
version: '3'

includes:
  include: include.yml

tasks:
  default:
    cmds:
      - echo "default task"
# include.yml
version: '3'

preconditions:
  - test -f foo.txt
task: `test -f foo.txt` failed
task: precondition not met

We could:

  1. Leave as it is, vars already behaves like this. We'll fix it later 🤷‍♂️
  2. Not allow global preconditions in included taskfiles for now
  3. Not merge this functionality until scoping is ready

I think 1 is pretty reckless. We're essentially intentionally adding behaviour that we know is confusing and will raise questions.

3 is very conservative while 2 is a good compromise. It depends how urgently we want to roll out this functionality. If we don't think its urgent then maybe we wait. The question is how long will scoping actually take? I've already taken a stab at it and I can tell you - it's significant piece of work.

@pd93
Copy link
Member

pd93 commented Feb 18, 2025

I completely missed the previous comments when reviewing this 😆 Including my own.

I followed the same pattern we have with vars. I am not a huge fan of naming preconditions inside preconditions, maybe values ? I you have some naming ideas

Yeah, Preconditions.Preconditions does stutter. I think Values is fine, This is what we do for Vars.

I was wondering how it should behave if an included Taskfile defines global preconditions:

  1. Should it produce an error (similar to dotenv)?
  2. Should all preconditions be merged and applied together?
  3. Should the global preconditions in the included Taskfile be ignored?

I started with the 2 in mind, but I am not sure anymore

Any thought ? cc @pd93 / @andreynering

As a user, I would expect 2. This is also the safer option if we maintain the principle of "its safer to not run a task that was intended to run than it is to run one that wasn't."

I didn't think about the scoping issues when writing this comment originally. I think option 2 is still the long-term goal here, but we might have to think about doing something like 1 or 3 in the meantime. See #1993 (comment)


P.s 10 years on this platform and I've only just noticed that if you highlight text in a comment before clicking "Quote Reply", it will only quote the selected text 👀 Amazing.

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.

Add global preconditions
2 participants