microsoft/typescript-go · error

failed to create CPU profile file: %w

Error message

failed to create CPU profile file: %w

What it means

StartCPUProfile could not os.Create the CPU profile file inside the (already created) profile directory. The file name is generated as <pid>-<millis>-cpuprofile.pb.gz. Directory creation succeeded, so failure is at file level: permission on the directory, disk full, name collisions, or filesystem-level I/O errors.

Source

Thrown at internal/pprof/pprof.go:90

}

// StartCPUProfile starts CPU profiling, writing to the specified directory when stopped.
func (c *CPUProfiler) StartCPUProfile(profileDir string) error {
	c.mu.Lock()
	defer c.mu.Unlock()

	if c.session != nil {
		return errors.New("CPU profiling already in progress")
	}

	if err := os.MkdirAll(profileDir, 0o755); err != nil {
		return fmt.Errorf("failed to create profile directory: %w", err)
	}

	cpuProfilePath := filepath.Join(profileDir, fmt.Sprintf("%d-%d-cpuprofile.pb.gz", os.Getpid(), time.Now().UnixMilli()))
	cpuFile, err := os.Create(cpuProfilePath)
	if err != nil {
		return fmt.Errorf("failed to create CPU profile file: %w", err)
	}

	if err := pprof.StartCPUProfile(cpuFile); err != nil {
		cpuFile.Close()
		os.Remove(cpuProfilePath)
		return fmt.Errorf("failed to start CPU profile: %w", err)
	}

	c.session = &ProfileSession{
		cpuFilePath: cpuProfilePath,
		cpuFile:     cpuFile,
		logWriter:   io.Discard,
	}
	return nil
}

// StopCPUProfile stops CPU profiling and returns the path to the profile file.
func (c *CPUProfiler) StopCPUProfile() (string, error) {

View on GitHub (pinned to 1bcfa18d79)

Solutions

  1. Verify with a manual touch of a file in the same directory as the server user
  2. Free disk space or move profileDir to a volume with capacity
  3. Adjust SELinux/AppArmor policy or pick a permitted path
Defensive patterns

Strategy: try-catch

Validate before calling

// verify file creation is possible in the target directory
probe, err := os.CreateTemp(profileDir, "probe-*")
if err != nil {
	return fmt.Errorf("profile dir not writable: %w", err)
}
probe.Close()
os.Remove(probe.Name())

Try / catch

if err := profiler.StartCPUProfile(dir); err != nil {
	if isFileCreationErr(err) { // EACCES / ENOSPC / EDQUOT
		return fmt.Errorf("cannot write profile file in %s: %w", dir, err)
	}
	return err
}

Prevention

When it happens

Trigger: Directory is writable for mkdir but the process lacks file-creation rights (sticky bit / different owner); ENOSPC; NFS/network volume refusing the create; SELinux/AppArmor denying writes in the profile location.

Common situations: Hardened servers where /tmp is noexec/nodev/nosticky-managed; disk exhaustion during long capture sessions; container volume quotas hit.

Related errors


AI-assisted analysis of microsoft/typescript-go@1bcfa18d79 (2026-08-16). Data as JSON: /api/errors/67a6e33017616b06. Report an issue: GitHub.