crowdsecurity/crowdsec · error

while creating data dir: %w

Error message

while creating data dir: %w

What it means

NewHubTest creates <hubPath>/.cache/data with os.MkdirAll (mode 0700) for shared hubtest data. If directory creation fails, it returns 'while creating data dir: <err>' — typically a filesystem permission or I/O problem at that exact path.

Source

Thrown at pkg/hubtest/hubtest.go:111

		return "", err
	}

	if f.IsDir() {
		return "", fmt.Errorf("%q is a directory, not a file", resolved)
	}

	return resolved, nil
}

func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecTest bool) (HubTest, error) {
	hubPath, err := filepath.Abs(hubPath)
	if err != nil {
		return HubTest{}, fmt.Errorf("can't get absolute path of hub: %w", err)
	}

	sharedDataDir := filepath.Join(hubPath, ".cache", "data")
	if err = os.MkdirAll(sharedDataDir, 0o700); err != nil {
		return HubTest{}, fmt.Errorf("while creating data dir: %w", err)
	}

	// we can't use hubtest without the hub
	if _, err = os.Stat(hubPath); os.IsNotExist(err) {
		return HubTest{}, fmt.Errorf("path to hub '%s' doesn't exist, can't run", hubPath)
	}
	// we can't use hubtest without crowdsec binary
	if crowdsecPath, err = resolveBinaryPath(crowdsecPath); err != nil {
		return HubTest{}, fmt.Errorf("can't find crowdsec binary: %w", err)
	}

	// we can't use hubtest without cscli binary
	if cscliPath, err = resolveBinaryPath(cscliPath); err != nil {
		return HubTest{}, fmt.Errorf("can't find cscli binary: %w", err)
	}

	if isAppsecTest {
		HubTestPath := filepath.Join(hubPath, ".appsec-tests")

View on GitHub (pinned to 909b515798)

Solutions

  1. Make the hub directory writable by the running user: chown -R $(id -u) <hubPath> or chmod u+w
  2. Check that <hubPath>/.cache is a directory, not a file; remove the stray file and retry
  3. Check the mount options / disk space (df -h, mount | grep hubPath)
  4. Run hubtest with a user that has write access to the hub checkout

Example fix

// before
.cache is a stale file -> mkdirall fails
// after
rm .cache && mkdir -p .cache/data
Defensive patterns

Strategy: validation

Validate before calling

dataDir := filepath.Join(hubPath, ".cache", "data")
if info, err := os.Stat(hubPath); err != nil || !info.IsDir() { return fmt.Errorf("hub path invalid") }
probe := filepath.Join(dataDir, ".probe")
if err := os.WriteFile(probe, nil, 0o600); err != nil { return fmt.Errorf("hub not writable: %w", err) }
os.Remove(probe)

Try / catch

ht, err := hubtest.NewHubTest(hubPath, ...)
if err != nil {
    var pe *fs.PathError
    if errors.As(err, &pe) && strings.Contains(err.Error(), "creating data dir") { log.Fatalf("cannot write %s: %v", pe.Path, pe.Err) }
}

Prevention

When it happens

Trigger: os.MkdirAll fails because the hub directory is read-only, owned by another user, an ancestor is a file (not a directory), or the disk is full / mounted noexec-read-only.

Common situations: Hub mounted read-only in CI; running hubtest as non-root in a root-owned checkout; a stray file named '.cache' where the directory should be; full disk.

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


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