anomalyco/sst · error

failed to flatten %s structure: %w

Error message

failed to flatten %s structure: %w

What it means

During workspace-layout flattening of a Python package (moving a src/ layout to the bundle root), any failure from copyDir or copyFile is wrapped as "failed to flatten %s structure" with the package name. The library flattens workspace packages so imports resolve at the bundle root; this error means one of the package's files/directories could not be copied during that step.

Source

Thrown at pkg/runtime/python/python.go:541

					continue
				}

				for _, innerEntry := range innerEntries {
					if isIgnored(innerEntry.Name()) {
						continue
					}

					srcPath := filepath.Join(innerPackageDir, innerEntry.Name())
					destPath := filepath.Join(packageDir, innerEntry.Name())

					if innerEntry.IsDir() {
						err = copyDir(srcPath, destPath, skipContent)
					} else {
						err = copyFile(srcPath, destPath)
					}

					if err != nil {
						return fmt.Errorf("failed to flatten %s structure: %w", packageName, err)
					}
				}

				// Remove the old src/ directory after flattening to avoid import confusion
				if err := os.RemoveAll(srcDir); err != nil {
					slog.Warn("failed to remove src/ after flattening", "package", packageName, "error", err)
				}

			}
		}
		return nil
	}

	// Check top-level directories
	if err := flattenDir(artifactDir); err != nil {
		return err
	}

View on GitHub (pinned to a0bd20f762)

Solutions

  1. Inspect the wrapped inner error to see which file failed; fix that specific path (permissions, missing dir)
  2. Remove stale files at the bundle root that conflict with the flattened layout
  3. Clean the .sst build output and redeploy for a fresh copy
  4. Check disk space and that source package files are all readable
Defensive patterns

Strategy: try-catch

Validate before calling

null

Try / catch

if err := sstBuild(); err != nil {
	if strings.Contains(err.Error(), "failed to flatten") {
		var pkg string
		fmt.Sscanf(err.Error(), "failed to flatten %s structure", &pkg)
		// inspect that package's src layout and permissions, clean output, rebuild
	}
}

Prevention

When it happens

Trigger: While flattening a workspace package, copyDir(srcPath, destPath, skipContent) or copyFile(srcPath, destPath) returns an error — destination directory missing/unwritable, source unreadable, or disk full. The package name is interpolated into the message.

Common situations: Monorepo Python package with a src/ layout and a conflicting file already at the bundle root; read-only bundle directory; package containing symlinks or unreadable files; insufficient disk space during a large package copy.

Related errors


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