golang/go · error

unterminated quoted string in pkgconf output

Error message

unterminated quoted string in pkgconf output

What it means

Thrown by parsePkgConfigOutput in cmd/go/internal/work after scanning the cflags/ldflags that pkg-config (pkgconf) printed, when the parser reaches end-of-input with a quote still open (a '"' or '\'' with no matching close). pkg-config output is parsed shell-style; an unterminated quote means the tool emitted malformed output that cannot be safely split into flags.

Source

Thrown at src/cmd/go/internal/work/exec.go:2041

			didQuote = true
			continue

		case ' ', '\t', '\n':
			if len(flag) > 0 || didQuote {
				flags = append(flags, string(flag))
			}
			flag, didQuote = flag[:0], false
			continue
		}

		flag = append(flag, c)
	}

	// Prefer to report a missing quote instead of a missing escape. If the string
	// is something like `"foo\`, it's ambiguous as to whether the trailing
	// backslash is really an escape at all.
	if quote != 0 {
		return nil, errors.New("unterminated quoted string in pkgconf output")
	}
	if escaped {
		return nil, errors.New("broken character escaping in pkgconf output")
	}

	if len(flag) > 0 || didQuote {
		flags = append(flags, string(flag))
	}
	return flags, nil
}

// Calls pkg-config if needed and returns the cflags/ldflags needed to build a's package.
func (b *Builder) getPkgConfigFlags(a *Action, p *load.Package) (cflags, ldflags []string, err error) {
	sh := b.Shell(a)
	if pcargs := p.CgoPkgConfig; len(pcargs) > 0 {
		// pkg-config permits arguments to appear anywhere in
		// the command line. Move them all to the front, before --.
		var pcflags []string

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Run `pkg-config --cflags --libs <yourpkg>` directly and inspect the output for stray quotes.
  2. Open the offending .pc file and fix the unbalanced quote in Cflags/Libs.
  3. Reinstall or upgrade the package that owns the broken .pc file (check with `pkg-config --variable=pcfiledir <pkg>`).
  4. As a workaround, replace the `#cgo pkg-config: foo` line with explicit `#cgo CFLAGS` / `#cgo LDFLAGS` literals.

Example fix

# before — /usr/lib/pkgconfig/foo.pc
Cflags: -I${includedir}/foo -DFOO="bar
Libs: -L${libdir} -lfoo

# after
Cflags: -I${includedir}/foo -DFOO=\"bar\"
Libs: -L${libdir} -lfoo
Defensive patterns

Strategy: try-catch

Validate before calling

// Inspect .pc output before building:
//   pkg-config --cflags --libs <pkg>
// Look for any line with an unbalanced '"' or '\''.
func balancedQuotes(s string) bool {
    in byte
    for i := 0; i < len(s); i++ {
        c := s[i]
        if in == 0 && (c == '"' || c == '\'') { in = c } else if c == in { in = 0 }
    }
    return in == 0
}

Try / catch

out, err := b.getPkgConfigFlags(a, p)
if err != nil && strings.Contains(err.Error(), "pkgconf output") {
    // log the raw pkg-config output, fix the offending .pc file, and retry
    log.Printf("pkg-config emitted malformed flags for %v; inspect .pc file", p.CgoPkgConfig)
}

Prevention

When it happens

Trigger: A .pc file (pkg-config package metadata) containing an unbalanced double quote in Cflags or Libs, e.g. `Cflags: -DFOO="bar`. A pkg-config implementation that prints trailing whitespace/quotes differently. Manual edits to system .pc files in /usr/lib/pkgconfig or /usr/local/lib/pkgconfig.

Common situations: CGo projects using #cgo pkg-config: directives. A misbehaving or buggy pkgconf version. Hand-edited .pc files where a closing quote was deleted. Cross-compilation sysroots with broken .pc files.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/c382b8cdf44c0dc7. Report an issue: GitHub.