anomalyco/sst · error

failed to create output directory: %w

Error message

failed to create output directory: %w

What it means

flattenPackageToRoot copies all files out of an extracted Python package into a single flat output directory. Before copying it creates outputDir with os.MkdirAll; if that fails (bad path, permission denied, path is a file, disk full) the walk/copy is aborted and this wrapped error is returned up through moveExtractedPackage.

Source

Thrown at pkg/runtime/python/build.go:529

		if info.IsDir() {
			return nil
		}

		ext := filepath.Ext(path)
		if ext == ".py" || ext == ".pyi" || info.Name() == "py.typed" {
			pythonFiles = append(pythonFiles, path)
		}

		return nil
	})

	if err != nil {
		return fmt.Errorf("failed to walk extracted directory: %w", err)
	}

	// Create output directory
	if err := os.MkdirAll(outputDir, 0755); err != nil {
		return fmt.Errorf("failed to create output directory: %w", err)
	}

	for _, srcFile := range pythonFiles {
		relPath, _ := filepath.Rel(extractedDir, srcFile)
		destFile := filepath.Join(outputDir, relPath)

		if err := copyFile(srcFile, destFile); err != nil {
			return fmt.Errorf("failed to copy %s to %s: %w", srcFile, destFile, err)
		}
	}

	// Clean up the extracted directory
	os.RemoveAll(extractedDir)

	return nil
}

func installDependenciesForBuild(ctx context.Context, input *runtime.BuildInput, projectInfo *projectInfo) error {

View on GitHub (pinned to a0bd20f762)

Solutions

  1. Check permissions/ownership on the output directory's parent path (chown/chmod, avoid running some builds with sudo which leaves root-owned .sst dirs)
  2. Verify no file exists at the outputDir path; delete or rename it
  3. Ensure the parent directory exists and the volume is writable (df -h, mount flags)
  4. Free disk space if the filesystem is full

Example fix

// before
if err := os.MkdirAll(outputDir, 0755); err != nil {
	return fmt.Errorf("failed to create output directory: %w", err)
}
// after (clear a file blocking the path, then retry)
if fi, statErr := os.Stat(outputDir); statErr == nil && !fi.IsDir() {
	os.Remove(outputDir)
}
if err := os.MkdirAll(outputDir, 0755); err != nil {
	return fmt.Errorf("failed to create output directory %s: %w", outputDir, err)
}
Defensive patterns

Strategy: validation

Validate before calling

import os
if fi := statIfExists(outputDir); fi != nil && !fi.IsDir() { t.Fatalf("%s is a file", outputDir) }
if fi, err := os.Stat(filepath.Dir(outputDir)); err != nil || !fi.IsDir() { t.Fatalf("parent of %s missing", outputDir) }
if unix.Access(filepath.Dir(outputDir), unix.W_OK) != nil { t.Fatalf("no write permission on %s", filepath.Dir(outputDir)) }

Type guard

func isWritableDir(path string) bool {
	fi, err := os.Stat(path)
	if err == nil && !fi.IsDir() {
		return false
	}
	f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o755)
	if err != nil {
		return false
	}
	f.Close()
	return true
}

Try / catch

if err := runFlatten(); err != nil {
	var pe *fs.PathError
	if errors.As(err, &pe) && errors.Is(pe.Err, syscall.EACCES) {
		// fix ownership: sudo chown -R $(id -u) <parent dir>
	}
	return err
}

Prevention

When it happens

Trigger: os.MkdirAll(outputDir, 0755) fails because outputDir's parent doesn't exist and can't be created, a component of the path is a regular file, the process lacks write permission on the parent, or the filesystem is read-only/full.

Common situations: Building a Python Lambda into an output dir whose parent was removed or is read-only (e.g. .sst build cache dir owned by root after running with sudo); outputDir collides with an existing file; building on a mounted read-only volume or a full disk.

Related errors


AI-assisted analysis of anomalyco/sst@a0bd20f762 (2026-08-30). Data as JSON: /api/errors/5fb12e0f835d4684. Report an issue: GitHub.