crowdsecurity/crowdsec · error
failed to parse %s: %w
Error message
failed to parse %s: %w
What it means
After reading a local item file, newLocalItem unmarshals it as yaml into localItemName to pick up an optional 'name' key. If the file content is not valid yaml (or the top-level structure isn't a mapping), yaml.Unmarshal fails and the error is wrapped in 'failed to parse %s'. The item is skipped/aborted for that file.
Source
Thrown at pkg/cwhub/sync.go:202
FileName: fileName,
State: ItemState{
LocalPath: path,
local: true,
UpToDate: true,
},
}
// try to read the name from the file
itemName := localItemName{}
itemContent, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %w", path, err)
}
err = yaml.Unmarshal(itemContent, &itemName)
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %w", path, err)
}
if itemName.Name != "" {
item.Name = itemName.Name
}
return item, nil
}
// ErrSkipPath is a sentinel to skip regular files because "nil, nil" is ambiguous. Returning SkipDir with files would skip the rest of the directory.
var ErrSkipPath = errors.New("sentinel")
func (h *Hub) itemVisit(path string, f os.DirEntry, err error) (*itemSpec, error) {
if err != nil {
h.logger.Debugf("while syncing hub dir: %s", err)
// there is a path error, we ignore the file
return nil, ErrSkipPath
}View on GitHub (pinned to 909b515798)
Solutions
- Validate the file with a yaml linter/parser: `python3 -c 'import yaml,sys; yaml.safe_load(open(sys.argv[1]))' <file>`
- Fix the syntax error reported at the line indicated in the wrapped message (tabs, indentation, quotes)
- Remove or rename non-yaml files that end in .yaml out of the config directory
- If it's a hub item, reinstall it with cscli to restore pristine content
Example fix
// before: config/scenarios/my.yaml name: crowdsecurity/my # tab indentation, parse fails // after name: crowdsecurity/my # spaces only
Defensive patterns
Strategy: validation
Validate before calling
import (
"os"
"gopkg.in/yaml.v3"
)
func validYAML(path string) error {
b, err := os.ReadFile(path)
if err != nil {
return err
}
var m map[string]any
return yaml.Unmarshal(b, &m)
}
// call for each .yaml in the config dir before starting crowdsec Try / catch
if err := hub.Load(ctx); err != nil {
if strings.Contains(err.Error(), "failed to parse") {
var yamlErr *yaml.TypeError
log.Warnf("bad yaml in config dir: %v", err)
return nil
}
return err
} Prevention
- Validate every yaml with a linter (yamllint) before committing it to the config dir
- Use spaces, never tabs, in yaml files
- Don't rename non-yaml files to .yaml; keep editor swap/backup files out of the config directory
When it happens
Trigger: A yaml file in the scanned config directory contains syntax errors: tabs instead of spaces, unclosed quotes, duplicate keys rejected by the parser, or non-mapping content like a bare string or a binary file with a .yaml extension.
Common situations: Hand-edited config files with indentation mistakes; files renamed to .yaml that aren't yaml (e.g. copied logs, editor swap files); copy/paste of yaml with smart quotes or tabs.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- cannot parse: %s
- failed to parse api client credential configuration file '%s
- while parsing capi whitelist file '%s': %w
- parsing console config file '%s': %w
- document %d: %w
AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06).
Data as JSON: /api/errors/7a7191bad64dc3ea.
Report an issue: GitHub.