github/copilot-sdk · error

failed to create temp dir

Error message

failed to create temp dir: %w

What it means

buildBundle could not create the temporary working directory via os.MkdirTemp("", "copilot-bundler-*") before downloading artifacts. This is an environment-level failure: os.MkdirTemp fails only when the system temp location is unusable, so this almost always indicates a machine/container problem rather than a code bug.

Solutions

  1. Check TMPDIR points to an existing writable directory, or unset it to use the default.
  2. Free disk space on the temp filesystem.
  3. Ensure /tmp (or $TMPDIR) is writable by the running user.
  4. Check SELinux/AppArmor denials if on a locked-down host.
  5. Mount a writable tmpfs in containerized CI if /tmp is read-only.

Example fix

// before
tempDir, err := os.MkdirTemp("", "copilot-bundler-*")
if err != nil {
	return bundleArtifacts{}, fmt.Errorf("failed to create temp dir: %w", err)
}
// after
base := os.TempDir()
if st, err := os.Stat(base); err != nil || !st.IsDir() {
	return bundleArtifacts{}, fmt.Errorf("temp dir %s unusable (set TMPDIR to a writable dir): %w", base, err)
}
tempDir, err := os.MkdirTemp("", "copilot-bundler-*")
if err != nil {
	return bundleArtifacts{}, fmt.Errorf("failed to create temp dir: %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

base := os.TempDir()
if st, err := os.Stat(base); err != nil || !st.IsDir() {
	return fmt.Errorf("TMPDIR %s is not a usable directory", base)
}
if err := unix.Access(base, unix.W_OK); err != nil {
	return fmt.Errorf("TMPDIR %s is not writable", base)
}

Try / catch

artifacts, err := buildBundle(...)
if err != nil && strings.Contains(err.Error(), "failed to create temp dir") {
	// fix TMPDIR / disk space in the environment, then rerun
	os.Setenv("TMPDIR", "/var/tmp")
}

Prevention

When it happens

Trigger: os.MkdirTemp fails — TMPDIR points to a nonexistent or read-only directory, the temp filesystem is full, or permissions/SELinux block directory creation in the temp location.

Common situations: Containers with read-only /tmp; TMPDIR set to a path that doesn't exist; disk-full on the temp volume; restrictive security policies (SELinux/AppArmor) blocking temp dir creation in CI.

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 github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/21db05142cc58f5a. Report an issue: GitHub.

Appendix: source

Thrown at go/cmd/bundler/main.go:436

		runtimeHash, err := sha256FileFromCompressed(runtimeArtifactPath)
		if err != nil {
			return bundleArtifacts{}, fmt.Errorf("failed to hash existing runtime.node: %w", err)
		}
		wrapperHash, err := sha256FileFromCompressed(wrapperArtifactPath)
		if err != nil {
			return bundleArtifacts{}, fmt.Errorf("failed to hash existing runtime wrapper: %w", err)
		}
		assetsHash, err := sha256File(assetsArtifactPath)
		if err != nil {
			return bundleArtifacts{}, fmt.Errorf("failed to hash existing runtime assets: %w", err)
		}
		return bundleArtifacts{outputPath, binaryHash, runtimeArtifactPath, runtimeHash, wrapperArtifactPath, wrapperHash, assetsArtifactPath, assetsHash}, nil
	}

	// Create temp directory for download
	tempDir, err := os.MkdirTemp("", "copilot-bundler-*")
	if err != nil {
		return bundleArtifacts{}, fmt.Errorf("failed to create temp dir: %w", err)
	}
	defer os.RemoveAll(tempDir)

	binaryPath, tarballPath, err := downloadCLIBinary(info.runtimePlatform, info.binaryName, cliVersion, tempDir)
	if err != nil {
		return bundleArtifacts{}, fmt.Errorf("failed to download CLI binary: %w", err)
	}

	if outputDir != "." {
		if err := os.MkdirAll(outputDir, 0755); err != nil {
			return bundleArtifacts{}, fmt.Errorf("failed to create output directory: %w", err)
		}
	}
	if includeLicense {
		if err := extractCLILicense(tarballPath, outputPath); err != nil {
			return bundleArtifacts{}, fmt.Errorf("failed to extract CLI license: %w", err)
		}
	}

View on GitHub (pinned to cd8cf15dc3)