hyperledger/fabric · error

failed to calculate relative path for %s

Error message

failed to calculate relative path for %s

What it means

findSource computes each discovered file's archive name as a path relative to the source or metadata root; if filepath.Rel cannot make the path relative to that root, the error is wrapped with 'failed to calculate relative path for %s'.

Source

Thrown at core/chaincode/platforms/golang/platform.go:492

			}

			// include everything except hidden dirs when we're not vendoring
			if cd.Module && !strings.HasPrefix(info.Name(), ".") {
				return nil
			}

			// Do not import any other directories into chaincode code package
			return filepath.SkipDir
		}

		relativeRoot := cd.Source
		if cd.isMetadata(path) {
			relativeRoot = cd.MetadataRoot
		}

		name, err := filepath.Rel(relativeRoot, path)
		if err != nil {
			return errors.Wrapf(err, "failed to calculate relative path for %s", path)
		}

		switch {
		case cd.isMetadata(path):
			// Skip hidden files in metadata
			if strings.HasPrefix(info.Name(), ".") {
				return nil
			}
			name = filepath.Join("META-INF", name)
			err := validateMetadata(name, path)
			if err != nil {
				return err
			}
		case cd.Module:
			name = filepath.Join("src", name)
		default:
			// skip top level go.mod and go.sum when not in module mode
			if name == "go.mod" || name == "go.sum" {

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Ensure the chaincode path and all sources are canonical absolute paths with no symlinks escaping the root.
  2. Keep sources physically inside the chaincode directory tree.
  3. Compare the logged %s path against the roots to find the escaping path and remove/relocate it.
Defensive patterns

Strategy: validation

Validate before calling

root, _ := filepath.Abs(ccPath)
filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
	if info != nil && info.Mode()&os.ModeSymlink != 0 {
		return fmt.Errorf("symlink escapes root: %s", p)
	}
	return nil
})

Try / catch

if err != nil {
	if strings.Contains(err.Error(), "failed to calculate relative path") {
		// locate path from the message; remove escaping symlink or relocate file
	}
}

Prevention

When it happens

Trigger: A walked file path that is not underneath cd.Source or cd.MetadataRoot (e.g. after symlink resolution or mixed absolute/relative roots), causing filepath.Rel to fail.

Common situations: Symlinked source directories resolving outside the declared chaincode path, inconsistent path separators, roots computed via different absolutization than the walked paths.

Related errors


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/8f809034307da9a8. Report an issue: GitHub.