diff --git a/cmd/task/task.go b/cmd/task/task.go index b33d688e4e..43a8481e6d 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -58,6 +58,9 @@ func run() error { dir := flags.Dir entrypoint := flags.Entrypoint + // Initialise version + ver.Init() + if flags.Version { fmt.Printf("Task version: %s\n", ver.GetVersionWithSum()) return nil @@ -132,8 +135,9 @@ func run() error { Stdout: os.Stdout, Stderr: os.Stderr, - OutputStyle: flags.Output, - TaskSorter: taskSorter, + OutputStyle: flags.Output, + TaskSorter: taskSorter, + EnableVersionCheck: true, } listOptions := task.NewListOptions(flags.List, flags.ListAll, flags.ListJson, flags.NoStatus) if err := listOptions.Validate(); err != nil { diff --git a/internal/version/version.go b/internal/version/version.go index 9685b75c85..34a21d4a8b 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -10,7 +10,7 @@ var ( sum = "" ) -func init() { +func Init() { info, ok := debug.ReadBuildInfo() if !ok || info.Main.Version == "" { version = "unknown" diff --git a/setup.go b/setup.go index 6e2034716a..69131d3385 100644 --- a/setup.go +++ b/setup.go @@ -246,6 +246,9 @@ func (e *Executor) setupConcurrencyState() { } func (e *Executor) doVersionChecks() error { + if !e.EnableVersionCheck { + return nil + } // Copy the version to avoid modifying the original schemaVersion := &semver.Version{} *schemaVersion = *e.Taskfile.Version diff --git a/task.go b/task.go index b183014271..9b554ccf91 100644 --- a/task.go +++ b/task.go @@ -70,12 +70,13 @@ type Executor struct { Stdout io.Writer Stderr io.Writer - Logger *logger.Logger - Compiler *compiler.Compiler - Output output.Output - OutputStyle ast.Output - TaskSorter sort.TaskSorter - UserWorkingDir string + Logger *logger.Logger + Compiler *compiler.Compiler + Output output.Output + OutputStyle ast.Output + TaskSorter sort.TaskSorter + UserWorkingDir string + EnableVersionCheck bool fuzzyModel *fuzzy.Model @@ -383,7 +384,7 @@ func (e *Executor) runCommand(ctx context.Context, t *ast.Task, call *ast.Call, if err != nil { return fmt.Errorf("task: failed to get variables: %w", err) } - stdOut, stdErr, close := outputWrapper.WrapWriter(e.Stdout, e.Stderr, t.Prefix, outputTemplater) + stdOut, stdErr, closer := outputWrapper.WrapWriter(e.Stdout, e.Stderr, t.Prefix, outputTemplater) err = execext.RunCommand(ctx, &execext.RunCommandOptions{ Command: cmd.Cmd, @@ -395,7 +396,7 @@ func (e *Executor) runCommand(ctx context.Context, t *ast.Task, call *ast.Call, Stdout: stdOut, Stderr: stdErr, }) - if closeErr := close(err); closeErr != nil { + if closeErr := closer(err); closeErr != nil { e.Logger.Errf(logger.Red, "task: unable to close writer: %v\n", closeErr) } if _, isExitError := interp.IsExitStatus(err); isExitError && cmd.IgnoreError { diff --git a/task_test.go b/task_test.go index afb40fa19d..b0d6da7810 100644 --- a/task_test.go +++ b/task_test.go @@ -220,10 +220,11 @@ func TestSpecialVars(t *testing.T) { t.Run(test.target, func(t *testing.T) { var buff bytes.Buffer e := &task.Executor{ - Dir: dir, - Stdout: &buff, - Stderr: &buff, - Silent: true, + Dir: dir, + Stdout: &buff, + Stderr: &buff, + Silent: true, + EnableVersionCheck: true, } require.NoError(t, e.Setup()) require.NoError(t, e.Run(context.Background(), &ast.Call{Task: test.target})) @@ -945,9 +946,10 @@ func TestTaskVersion(t *testing.T) { for _, test := range tests { t.Run(test.Dir, func(t *testing.T) { e := task.Executor{ - Dir: test.Dir, - Stdout: io.Discard, - Stderr: io.Discard, + Dir: test.Dir, + Stdout: io.Discard, + Stderr: io.Discard, + EnableVersionCheck: true, } err := e.Setup() if test.wantErr { @@ -1767,9 +1769,10 @@ func TestDynamicVariablesShouldRunOnTheTaskDir(t *testing.T) { func TestDisplaysErrorOnVersion1Schema(t *testing.T) { e := task.Executor{ - Dir: "testdata/version/v1", - Stdout: io.Discard, - Stderr: io.Discard, + Dir: "testdata/version/v1", + Stdout: io.Discard, + Stderr: io.Discard, + EnableVersionCheck: true, } err := e.Setup() require.Error(t, err) @@ -1779,9 +1782,10 @@ func TestDisplaysErrorOnVersion1Schema(t *testing.T) { func TestDisplaysErrorOnVersion2Schema(t *testing.T) { var buff bytes.Buffer e := task.Executor{ - Dir: "testdata/version/v2", - Stdout: io.Discard, - Stderr: &buff, + Dir: "testdata/version/v2", + Stdout: io.Discard, + Stderr: &buff, + EnableVersionCheck: true, } err := e.Setup() require.Error(t, err)