kopia/kopia · error

error building index shard

Error message

error building index shard

What it means

BuildShards writes each sorted content shard via buildSortedContents(s, buf, indexVersion). If serializing a shard's contents into its write buffer fails, the builder closes any already-open shard buffers via closeShards() and returns the error wrapped as "error building index shard". This indicates a failure while generating the binary index representation for one shard.

Solutions

  1. Check the wrapped cause and verify indexVersion is a supported value (e.g. the repo's allowed index versions v1/v2).
  2. Validate the content set being sharded has no malformed entries.
  3. Retry the build after fixing inputs; closeShards() already released partial buffers, so no manual cleanup is needed.

Example fix

// before
shards, cleanup, err := builder.BuildShards(99, stable, size) // unsupported version
// after
if indexVersion != 1 && indexVersion != 2 {
	return fmt.Errorf("unsupported index version %d", indexVersion)
}
shards, cleanup, err := builder.BuildShards(indexVersion, stable, size)
Defensive patterns

Strategy: validation

Validate before calling

supported := map[int]bool{1: true, 2: true}
if !supported[indexVersion] {
	return fmt.Errorf("unsupported index version %d", indexVersion)
}

Prevention

When it happens

Trigger: Calling BuildShards when buildSortedContents fails for a shard — typically an unsupported/invalid indexVersion or an internal error while encoding sorted contents into the buffer.

Common situations: Passing an index version the builder cannot serialize (e.g. version not registered/supported); contents with invalid entries; buffer allocation failures.

Related errors


AI-assisted analysis of kopia/kopia@82495e54b5 (2026-09-07). Data as JSON: /api/errors/974a8f313e9080e2. Report an issue: GitHub.

Appendix: source

Thrown at repo/content/index/one_use_index_builder.go:117

		dataShards      []gather.Bytes
		randomSuffix    [32]byte
	)

	closeShards := func() {
		for _, ds := range dataShardsBuf {
			ds.Close()
		}
	}

	for _, s := range shardedBuilders {
		buf := gather.NewWriteBuffer()

		dataShardsBuf = append(dataShardsBuf, buf)

		if err := buildSortedContents(s, buf, indexVersion); err != nil {
			closeShards()

			return nil, nil, errors.Wrap(err, "error building index shard")
		}

		if !stable {
			if _, err := rand.Read(randomSuffix[:]); err != nil {
				closeShards()

				return nil, nil, errors.Wrap(err, "error getting random bytes for suffix")
			}

			if _, err := buf.Write(randomSuffix[:]); err != nil {
				closeShards()

				return nil, nil, errors.Wrap(err, "error writing extra random suffix to ensure indexes are always globally unique")
			}
		}

		dataShards = append(dataShards, buf.Bytes())
	}

View on GitHub (pinned to 82495e54b5)