'no such file or directory' running coverage

A common problem I come across when I run go tool cover, are coverage failures due to coverage location not being present. I have found that to resolve this issue, I need to create coverage directories first.

I am using Taskfile to run my tasks so here is an example coverage task:

  cover:
    cmds:
      - mkdir -p ./coverage
      - mkdir -p ./internal/third/ants/coverage
      - mkdir -p ./locale/coverage
      - ginkgo --json-report
        ./ginkgo.report
        -coverpkg=./...
        -coverprofile=./coverage/coverage.out -r
      - go tool cover -html=./coverage/coverage.out -o ./coverage/coverage.html
      - open ./coverage/coverage.html

This does fix my issue, but it is ‘unnecessarily’ verbose. Is there a way to configure coverage to create the coverage directory automatically if it doesn’t exist, so that I can remove all the ‘mkdir -p’ that are currently required in my setup, to avoid ‘no such file or directory’ errors.

For larger projects, with many directories, I baulk at the prospect of having litter my task file with these mkdir commands, so I hope that I am indeed missing something that I have so far not found a solution for.

PS: an example output showing the error is:

 λ  task cover
[1725513806] Traverse Suite - 20/20 specs •••••••••••••••••••• SUCCESS! 772.136431ms PASS
error generating coverage report: internal error: opening coverage data output file "/Users/plastikfan/dev/github/snivilised/traverse/coverage/coverage.out": open /Users/plastikfan/dev/github/snivilised/traverse/coverage/coverage.out: no such file or directory
ginkgo run failed