Skip to content

Automatically create output directory for reports #3623

Closed
@james-johnston-thumbtack

Description

Your feature request related to a problem? Please describe.

We have a configuration that looks like this to write a JUnit report to a reports/ output directory that is normally ignored with a direction to ignore it in the top-level .gitignore file.

output:
  format: colored-line-number,junit-xml:reports/junit/lint.xml

If this reports/ directory does not exist, then golangci-lint will fail with:

level=error msg="Running error: can't create output for reports/junit/lint.xml: open reports/junit/lint.xml: no such file or directory"

Describe the solution you'd like.

I would have expected the output directory to be created, rather than failing with an error.

Describe alternatives you've considered.

We can work around the problem by committing some placeholder files in the reports directory, and then setting up .gitignore files to ignore the individual files in this output directory. This forces git to create that output directory upon repo checkout.

But this is less ideal because it means that cleaning the repo is more complicated; you can't just delete the reports/ directory any more because some of those files (the placeholder files) are committed & thus result in dirty repo status. It also makes for more work managing the .gitignore files.

Additional context.

The key location to change is probably here:

func (e *Executor) createWriter(path string) (io.Writer, bool, error) {
if path == "" || path == "stdout" {
return logutils.StdOut, false, nil
}
if path == "stderr" {
return logutils.StdErr, false, nil
}
f, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, defaultFileMode)
if err != nil {
return nil, false, err
}
return f, true, nil
}

If this function creates the directory hierarchy before attempting to open the file, then the problem can be avoided.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: outputRelated to issue outputenhancementNew feature or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions