crowdsecurity/crowdsec · error

unable to copy 'patterns' from '%s' to '%s': %w

Error message

unable to copy 'patterns' from '%s' to '%s': %w

What it means

This error is returned by HubTestItem.Run when CopyDir fails to recursively copy the crowdsec patterns folder (patternDir, e.g. the hub's data/patterns directory) into the runtime folder (t.RuntimePatternsPath). It wraps the error from walking/reading the source tree or creating destination files.

Source

Thrown at pkg/hubtest/hubtest_item.go:634

	// copy template config file to runtime folder
	if err = Copy(t.TemplateConfigPath, t.RuntimeConfigFilePath); err != nil {
		return fmt.Errorf("unable to copy '%s' to '%s': %w", t.TemplateConfigPath, t.RuntimeConfigFilePath, err)
	}

	// copy template profile file to runtime folder
	if err = Copy(t.TemplateProfilePath, t.RuntimeProfileFilePath); err != nil {
		return fmt.Errorf("unable to copy '%s' to '%s': %w", t.TemplateProfilePath, t.RuntimeProfileFilePath, err)
	}

	// copy template simulation file to runtime folder
	if err = Copy(t.TemplateSimulationPath, t.RuntimeSimulationFilePath); err != nil {
		return fmt.Errorf("unable to copy '%s' to '%s': %w", t.TemplateSimulationPath, t.RuntimeSimulationFilePath, err)
	}

	// copy template patterns folder to runtime folder
	if err = CopyDir(patternDir, t.RuntimePatternsPath); err != nil {
		return fmt.Errorf("unable to copy 'patterns' from '%s' to '%s': %w", patternDir, t.RuntimePatternsPath, err)
	}

	// create the appsec-configs dir
	if err = os.MkdirAll(filepath.Join(t.RuntimePath, "appsec-configs"), os.ModePerm); err != nil {
		return fmt.Errorf("unable to create folder '%s': %w", t.RuntimePath, err)
	}

	// if it's an appsec rule test, we need acquis and appsec profile
	if len(t.Config.AppsecRules) > 0 {
		// copy template acquis file to runtime folder
		log.Debugf("copying %s to %s", t.TemplateAcquisPath, t.RuntimeAcquisFilePath)

		if err = Copy(t.TemplateAcquisPath, t.RuntimeAcquisFilePath); err != nil {
			return fmt.Errorf("unable to copy '%s' to '%s': %w", t.TemplateAcquisPath, t.RuntimeAcquisFilePath, err)
		}

		log.Debugf("copying %s to %s", t.TemplateAppsecProfilePath, filepath.Join(t.RuntimePath, "appsec-configs", "config.yaml"))
		// copy template appsec-config file to runtime folder

View on GitHub (pinned to 909b515798)

Solutions

  1. Verify patternDir exists and contains pattern files; run `cscli hub update` / install data files if missing.
  2. Confirm the correct patternDir is passed to Run (usually the hub's installed data/patterns path).
  3. Delete t.RuntimePatternsPath from a previous failed run and retry.
  4. Check read permissions on the source patterns tree and write permissions on the runtime directory.

Example fix

// before
item.Run(ctx, "/missing/hub/data/patterns") // fails
// after
patternDir := filepath.Join(hubDataDir, "patterns")
if _, err := os.Stat(patternDir); err != nil {
    return fmt.Errorf("run 'cscli hub update' first: %w", err)
}
err := item.Run(ctx, patternDir)
Defensive patterns

Strategy: validation

Validate before calling

info, err := os.Stat(patternDir)
if err != nil {
    return fmt.Errorf("patterns dir missing, run 'cscli hub update': %w", err)
}
if !info.IsDir() {
    return fmt.Errorf("%s is not a directory", patternDir)
}

Try / catch

if err := item.Run(ctx, patternDir); err != nil {
    if os.IsNotExist(errors.Unwrap(err)) {
        return fmt.Errorf("patterns not installed; run cscli hub update")
    }
    return err
}

Prevention

When it happens

Trigger: Calling HubTestItem.Run when patternDir does not exist (patterns not downloaded/installed), a file inside patternDir is unreadable, or the runtime patterns destination cannot be created/written.

Common situations: Running hub tests before `cscli hub update`/install populated the patterns directory; pointing the tests at the wrong patternDir; permission issues on the hub data directory; interrupted previous copy leaving a corrupt destination.

Related errors


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