GoogleContainerTools/skaffold · error

failed to clone repo %s: trouble creating cache directory: %

Error message

failed to clone repo %s: trouble creating cache directory: %w

What it means

Thrown by syncRepo in pkg/skaffold/git when os.MkdirAll fails to create the local git cache directory (from config.GetRemoteCacheDir). syncRepo clones remote git repos into a shared cache dir before using them in builds; without a writable cache dir the clone cannot proceed. The underlying os error is wrapped via %w.

Source

Thrown at pkg/skaffold/git/gitutil.go:106

func getRepoDir(g Config) (string, error) {
	inputs := []string{g.Repo, g.Ref}
	hasher := sha256.New()
	enc := json.NewEncoder(hasher)
	if err := enc.Encode(inputs); err != nil {
		return "", err
	}

	return base64.URLEncoding.EncodeToString(hasher.Sum(nil))[:32], nil
}

func syncRepo(ctx context.Context, g Config, opts config.SkaffoldOptions) (string, error) {
	skaffoldCacheDir, err := config.GetRemoteCacheDir(opts)
	r := gitCmd{Dir: skaffoldCacheDir}
	if err != nil {
		return "", fmt.Errorf("failed to clone repo %s: %w", g.Repo, err)
	}
	if err := os.MkdirAll(skaffoldCacheDir, 0700); err != nil {
		return "", fmt.Errorf(
			"failed to clone repo %s: trouble creating cache directory: %w", g.Repo, err)
	}

	ref := g.Ref
	if ref == "" {
		ref, err = defaultRef(ctx, g.RepoCloneURI, g.Repo)
		if err != nil {
			return "", fmt.Errorf("failed to clone repo %s: trouble getting default branch: %w", g.Repo, err)
		}
	}

	hash, err := getRepoDir(g)
	if err != nil {
		return "", fmt.Errorf("failed to clone git repo: unable to create directory name: %w", err)
	}
	repoCacheDir := filepath.Join(skaffoldCacheDir, hash)
	if _, err := os.Stat(repoCacheDir); os.IsNotExist(err) {
		if opts.SyncRemoteCache.CloneDisabled() {

View on GitHub (pinned to a1189de023)

Solutions

  1. Fix permissions or choose a writable location: run as a user that owns the cache dir, or set --remote-cache-dir/SKAFFOLD_REMOTE_CACHE_DIR to a writable path.
  2. Check whether the path exists as a regular file and remove it so a directory can be created.
  3. Verify disk space (df -h) and that the parent directory exists and is writable.
  4. If running in a container, mount an emptyDir/PV at the cache dir path with 0700 permissions.

Example fix

// before
skaffold run --remote-cache-dir /var/cache/skaffold   # read-only volume
// after
skaffold run --remote-cache-dir /tmp/skaffold-cache   # writable path
Defensive patterns

Strategy: validation

Validate before calling

const cacheDir = process.env.SKAFFOLD_REMOTE_CACHE_DIR || path.join(os.homedir(), '.skaffold', 'gitcache');
const st = fs.statSync(path.dirname(cacheDir));
if (!st.isDirectory()) throw new Error(`cache parent ${path.dirname(cacheDir)} is not a directory`);
fs.accessSync(cacheDir in st ? cacheDir : path.dirname(cacheDir), fs.constants.W_OK); // will throw if not writable

Type guard

function isWritableDir(p) {
  try { return fs.statSync(p).isDirectory() && (fs.accessSync(p, fs.constants.W_OK), true); }
  catch { return false; }
}

Try / catch

try {
  await syncRepo(g, ctx, opts);
} catch (err) {
  if (/trouble creating cache directory/.test(err.message)) {
    // fall back to a temp writable dir or abort with a clear message
    console.error('Git cache dir not writable:', err.cause ?? err);
  }
  throw err;
}

Prevention

When it happens

Trigger: os.MkdirAll(skaffoldCacheDir, 0700) returns an error: path not writable, parent missing and uncreatable, path exists as a file, or disk full.

Common situations: Running skaffold in a container/CI as a non-root user whose HOME is not writable; SKAFFOLD_REMOTE_CACHE_DIR or --remote-cache-dir pointing at a read-only mount or an existing regular file; read-only root filesystem in Kubernetes pods running skaffold.

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


AI-assisted analysis of GoogleContainerTools/skaffold@a1189de023 (2026-09-05). Data as JSON: /api/errors/c835f04c6524701d. Report an issue: GitHub.