dagger/dagger · error

failed to reload context after setting source subpath: %w

Error message

failed to reload context after setting source subpath: %w

What it means

ModuleSource.withSDK changes the module's SDK, which can alter the source subpath; Dagger reloads the context and tolerates only NotFound during init. Any other reload failure is wrapped with 'failed to reload context after setting source subpath'. This indicates the context could not be re-read after the SDK-driven subpath change, typically a filesystem or git problem, not the SDK choice itself.

Source

Thrown at core/schema/modulesource.go:1396

	}
	src.SDK.Source = args.Source

	// Default source subpath to source root when an SDK is set but no explicit
	// source path was configured (e.g. during module init before dagger.json exists).
	// This mirrors the logic in initFromModConfig for the sdkSource != "" && modCfg.Source == "" case.
	if src.SourceSubpath == "" {
		src.SourceSubpath = src.SourceRootSubpath

		// Reload the context directory now that SourceSubpath is set, so that
		// the source files are included (e.g. an existing main.go). The initial
		// load may have used empty SourceSubpath which matches no files.
		err := s.loadModuleSourceContext(ctx, src)
		switch {
		case err == nil:
		case codes.NotFound == status.Code(err) && src.Kind == core.ModuleSourceKindLocal:
			// tolerate not found during init when context dir doesn't exist yet
		default:
			return nil, fmt.Errorf("failed to reload context after setting source subpath: %w", err)
		}
	}

	// reload the sdk implementation too
	query, err := core.CurrentQuery(ctx)
	if err != nil {
		return nil, err
	}
	src.SDKImpl, err = sdk.NewLoader().SDKForModule(ctx, query, src.SDK, src)
	if err != nil {
		return nil, fmt.Errorf("failed to load sdk for module source: %w", err)
	}

	// New SDK means new exposed functions and types. Different .env entries might match.
	if err := src.LoadUserDefaults(ctx); err != nil {
		return nil, fmt.Errorf("load user defaults: %w", err)
	}
	return src, nil

View on GitHub (pinned to 82ba2681db)

Solutions

  1. Confirm the SDK's expected source subpath directory exists and is readable in your module.
  2. Fix broken symlinks or mount points in the context directory.
  3. If using git sources, run git status/checkout to restore a clean state.
  4. Check the wrapped inner error for the precise reload failure.

Example fix

// before: python module context without expected subdir structure
withSDK("python") // reload fails
// after: ensure module layout matches SDK defaults
cd my-mod && mkdir -p src && dagger mod use . --sdk python
Defensive patterns

Strategy: validation

Validate before calling

import os
for sub in ['src', expected_sdk_subpath]:
    p = os.path.join(module_root, sub)
    if not os.path.isdir(p):
        print(f"warning: SDK-expected directory missing: {p}")

Try / catch

try {
  await mod.withSDK('python')
} catch (e) {
  if (String(e).includes('failed to reload context after setting source subpath')) {
    console.error('Context unreadable after SDK subpath change:', e.cause ?? e)
  }
  throw e
}

Prevention

When it happens

Trigger: ModuleSource.withSDK where loading the module context after the SDK default subpath change fails with an unexpected (non-NotFound) error.

Common situations: SDK expects a subpath directory that is unreadable; context dir is a broken symlink; git checkout state inconsistent; running inside a container without the mounted context.

Related errors


AI-assisted analysis of dagger/dagger@82ba2681db (2026-09-05). Data as JSON: /api/errors/e2eead86a7fedac2. Report an issue: GitHub.