github/copilot-sdk · warning

failed to read package directory

Error message

failed to read package directory %q: %w

What it means

detectPackageName reads the package directory to infer the main package name for bundling; this error wraps an os.ReadDir failure, falling back to defaultPackageName. It indicates the directory could not be read at all (missing, not a directory, or permission problem).

Solutions

  1. Verify the directory path exists and is readable (ls <dir>) before running the bundler.
  2. Run the bundler from the Go module root, or pass the correct package directory flag.
  3. Fix filesystem permissions (chmod/chown) or run in an environment that can read the path.
  4. Check the wrapped OS error in the message to distinguish 'no such file or directory' from 'permission denied'.

Example fix

// before
bundler --platform linux/amd64 --dir ./cmd/typo

// after
bundler --platform linux/amd64 --dir ./cmd/app
Defensive patterns

Strategy: validation

Validate before calling

if info, err := os.Stat(dir); err != nil || !info.IsDir() {
    return fmt.Errorf("package directory %q not readable: %v", dir, err)
}

Try / catch

if err != nil && strings.Contains(err.Error(), "failed to read package directory") {
    var pathErr *fs.PathError
    if errors.As(err, &pathErr) {
        log.Fatalf("check path/permissions for %s: %v", pathErr.Path, pathErr.Err)
    }
}

Prevention

When it happens

Trigger: detectPackageName (called from main) invokes os.ReadDir on the configured or default ('.') directory and the OS returns an error, e.g. the package dir flag points to a nonexistent or unreadable path.

Common situations: Typo in the --dir/package directory flag; running the bundler from the wrong working directory; the source directory was deleted or renamed; insufficient filesystem permissions (CI containers, read-only mounts).

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/5a8cbae4f11fbd7c. Report an issue: GitHub.

Appendix: source

Thrown at go/cmd/bundler/main.go:217

func validPlatforms() []string {
	result := make([]string, 0, len(platforms))
	for p := range platforms {
		result = append(result, p)
	}
	return result
}

// detectPackageName reads package clauses from files that match the target
// platform and build constraints. It returns defaultPackageName with an error
// when detection fails.
func detectPackageName(dir, goos, goarch string) (string, error) {
	if dir == "" {
		dir = "."
	}

	entries, err := os.ReadDir(dir)
	if err != nil {
		return defaultPackageName, fmt.Errorf("failed to read package directory %q: %w", dir, err)
	}

	buildContext := build.Default
	buildContext.GOOS = goos
	buildContext.GOARCH = goarch

	packageName := ""
	for _, entry := range entries {
		name := entry.Name()
		if entry.IsDir() || !strings.HasSuffix(name, ".go") ||
			strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") ||
			strings.HasSuffix(name, "_test.go") || strings.HasPrefix(name, "zcopilot_") {
			continue
		}
		matches, err := buildContext.MatchFile(dir, name)
		if err != nil {
			return defaultPackageName, fmt.Errorf("failed to evaluate build constraints in %q: %w", filepath.Join(dir, name), err)
		}

View on GitHub (pinned to cd8cf15dc3)