crowdsecurity/crowdsec · error

failed to open

Error message

failed to open

What it means

ParserAssert.AssertFile opens the parser dump file configured at p.File and immediately returns the bare string error "failed to open" if os.Open fails, discarding the OS error. It signals the assertion harness could not read the dump file it is supposed to parse, so no assertions can be evaluated.

Source

Thrown at pkg/hubtest/parser_assert.go:76

	return ret, nil
}

func (p *ParserAssert) LoadTest(filename string) error {
	parserDump, err := dumps.LoadParserDump(filename)
	if err != nil {
		return fmt.Errorf("loading parser dump file: %w", err)
	}

	p.TestData = parserDump

	return nil
}

func (p *ParserAssert) AssertFile(testFile string) error {
	file, err := os.Open(p.File)
	if err != nil {
		return errors.New("failed to open")
	}

	if err := p.LoadTest(testFile); err != nil {
		return fmt.Errorf("unable to load parser dump file '%s': %w", testFile, err)
	}

	scanner := bufio.NewScanner(file)
	scanner.Split(bufio.ScanLines)

	nbLine := 0

	for scanner.Scan() {
		nbLine++

		if scanner.Text() == "" {
			continue
		}

View on GitHub (pinned to 909b515798)

Solutions

  1. Generate the parser dump first by running the test data through the parser before asserting
  2. Check that the dump file path (p.File) exists: ls <path> and correct the path in the test config
  3. Fix file permissions on the dump file/directory
  4. Note the error loses the OS detail: temporarily add %w wrapping of the os.Open error to see the exact cause
  5. Confirm RunWithLogFile passes the correct testFile so LoadTest is pointed at the right dump

Example fix

// before
file, err := os.Open(p.File)
if err != nil {
    return errors.New("failed to open")
}
// after
file, err := os.Open(p.File)
if err != nil {
    return fmt.Errorf("failed to open parser dump '%s': %w", p.File, err)
}
Defensive patterns

Strategy: validation

Validate before calling

func assertDumpReadable(path string) error {
    fi, err := os.Stat(path)
    if err != nil { return fmt.Errorf("dump file missing: %w", err) }
    if fi.IsDir() { return fmt.Errorf("%s is a directory", path) }
    f, err := os.Open(path); if err != nil { return err }; return f.Close()
}

Try / catch

if err := p.AssertFile(testFile); err != nil {
    if strings.Contains(err.Error(), "failed to open") {
        log.Printf("check parser dump path %q exists and is readable", p.File)
    }
    return err
}

Prevention

When it happens

Trigger: Calling ParserAssert.AssertFile (via RunWithLogFile) when p.File does not exist, has wrong permissions, or is a directory — any os.Open failure on the configured dump file path.

Common situations: Hubtest run in a fresh checkout where the parser dump was never generated (tests never ran before the assert step); wrong path in the test config; file removed by a clean step; running as a user without read permission on /tmp artifacts.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06). Data as JSON: /api/errors/f6ff0027929f1fbf. Report an issue: GitHub.