github/copilot-sdk · error

failed to parse package clause in

Error message

failed to parse package clause in %q: %w

What it means

detectPackageName walks a directory's .go files and parses each package clause with go/parser to find the package name. If any file's package clause cannot be parsed, it wraps the parser error and aborts with this message. It indicates a corrupted, unreadable, or non-Go file inside the directory being bundled.

Solutions

  1. Run gofmt -l or go build on the directory to find and fix the syntactically invalid .go file
  2. Inspect the wrapped path %q in the error and open that exact file to repair its package clause
  3. Remove or rename any non-Go files that use a .go extension
  4. Check file permissions and re-run the bundler

Example fix

// before: bundler fails on broken file
// $ bundler ./pkg  -> failed to parse package clause in "pkg/gen.go"
// after: fix the file's package clause
// -package gen
// +package generated
Defensive patterns

Strategy: validation

Validate before calling

fset := token.NewFileSet()
if _, err := parser.ParseFile(fset, path, nil, parser.PackageClauseOnly); err != nil {
    return fmt.Errorf("invalid Go source %s: %v", path, err)
}
// or simply: gofmt -l ./dir && go vet ./dir before bundling

Try / catch

if _, err := bundle(dir); err != nil {
    var parseErr *os.PathError
    if errors.Is(err, os.ErrPermission) || strings.Contains(err.Error(), "failed to parse package clause") {
        // repair or exclude the offending file, then retry
    }
}

Prevention

When it happens

Trigger: A .go file in the directory passed to the bundler contains a syntax error in (or before) its package clause, is empty, or cannot be read from disk; parser.ParseFile with PackageClauseOnly then fails and this error is returned.

Common situations: Partially written or truncated files from an interrupted VCS checkout; stray files with a .go extension that aren't Go source; permission or encoding issues causing the file to be unreadable.

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/1853ac41713f3dff. Report an issue: GitHub.

Appendix: source

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

	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)
		}
		if !matches {
			continue
		}

		path := filepath.Join(dir, name)
		file, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.PackageClauseOnly)
		if err != nil {
			return defaultPackageName, fmt.Errorf("failed to parse package clause in %q: %w", path, err)
		}

		if packageName == "" {
			packageName = file.Name.Name
			continue
		}
		if packageName != file.Name.Name {
			return defaultPackageName, fmt.Errorf("multiple packages %q and %q found in %q", packageName, file.Name.Name, dir)
		}
	}

	if packageName == "" {
		return defaultPackageName, fmt.Errorf("no Go package found in %q", dir)
	}
	return packageName, nil
}

// detectCLIVersion detects the CLI version by:

View on GitHub (pinned to cd8cf15dc3)