anomalyco/sst · error

failed to write file content for %s: %w

Error message

failed to write file content for %s: %w

What it means

This error is returned when writing the Worker script file bytes into the multipart form part fails. The Write targets an in-memory bytes.Buffer via the multipart part writer, so failure indicates the buffer write returned an error (e.g. allocation failure), not a network problem. It is raised inside handleUpdate during Create/Update of the WorkerScript resource.

Source

Thrown at pkg/server/resource/cloudflare-worker-script.go:176

	}

	contentType := "application/javascript"
	if input.MainModule != "" {
		input.MainModule = input.Content.Hash
		contentType = "application/javascript+module"
	}

	contentPart, err := writer.CreatePart(map[string][]string{
		"Content-Disposition": []string{fmt.Sprintf(`form-data; name="%s"; filename="%s"`, input.Content.Hash, input.Content.Hash)},
		"Content-Type":        []string{contentType},
	})
	if err != nil {
		return fmt.Errorf("failed to create form part %s: %w", input.Content.Hash, err)
	}

	_, err = contentPart.Write([]byte(fileContent))
	if err != nil {
		return fmt.Errorf("failed to write file content for %s: %w", input.Content.Hash, err)
	}

	// Add metadata to form data
	metadataPart, err := writer.CreatePart(map[string][]string{
		"Content-Disposition": []string{`form-data; name="metadata"`},
		"Content-Type":        []string{"application/json"},
	})
	if err != nil {
		return fmt.Errorf("failed to create form part metadata: %w", err)
	}

	metadata, err := json.Marshal(buildMetadata(input))
	if err != nil {
		return fmt.Errorf("failed to marshal metadata: %w", err)
	}

	_, err = metadataPart.Write(metadata)
	if err != nil {

View on GitHub (pinned to a0bd20f762)

Solutions

  1. Check the wrapped inner error for an allocation/OOM cause and reduce the worker bundle size
  2. Verify all writes happen before writer.Close()
  3. Re-run the deploy; retry transiently if memory pressure caused it

Example fix

null
Defensive patterns

Strategy: try-catch

Validate before calling

if _, err := os.Stat(input.Content.Filename); err != nil { return fmt.Errorf("worker file missing: %w", err) }

Try / catch

if err := handleUpdate(input); err != nil {
    if strings.Contains(err.Error(), "failed to write file content") {
        log.Printf("buffer write failed, check bundle size/memory: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: contentPart.Write([]byte(fileContent)) returns n < len(fileContent) or a non-nil error after the file was read from input.Content.Filename.

Common situations: Extremely large worker bundles exhausting memory; corrupted internal writer state after a prior partial failure; custom writer mutations.

Related errors


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