wavetermdev/waveterm · error

gofmt failed: %w Output: %s

Error message

gofmt failed: %w
Output: %s

What it means

FormatGoFile runs `gofmt -w <filePath>` and captures combined stdout/stderr. If gofmt exits non-zero — meaning the Go source has syntax errors or gofmt could not write the file — the error is wrapped as "gofmt failed" including the process output. The output section contains gofmt's own diagnostic, typically the parse error with line/column.

Source

Thrown at pkg/waveappstore/waveappstore.go:435

	}

	filePath, err := validateAndResolveFilePath(appDir, fileName)
	if err != nil {
		return err
	}

	if filepath.Ext(filePath) != ".go" {
		return fmt.Errorf("file is not a Go file: %s", fileName)
	}

	gofmtPath, err := waveapputil.ResolveGoFmtPath()
	if err != nil {
		return fmt.Errorf("failed to resolve gofmt path: %w", err)
	}

	cmd := exec.Command(gofmtPath, "-w", filePath)
	if output, err := cmd.CombinedOutput(); err != nil {
		return fmt.Errorf("gofmt failed: %w\nOutput: %s", err, string(output))
	}

	return nil
}

func ListAllAppFiles(appId string) (*fileutil.ReadDirResult, error) {
	if err := ValidateAppId(appId); err != nil {
		return nil, fmt.Errorf("invalid appId: %w", err)
	}

	appDir, err := GetAppDir(appId)
	if err != nil {
		return nil, err
	}

	if _, err := os.Stat(appDir); os.IsNotExist(err) {
		return nil, fmt.Errorf("app directory does not exist: %s", appDir)
	}

View on GitHub (pinned to a4447c1563)

Solutions

  1. Read the `Output:` portion of the error — gofmt reports the exact line/column of the syntax error — and fix the source
  2. Only invoke format on parseable files; run a parser/linter check first if available
  3. Check file write permissions if the syntax looks valid (gofmt -w needs write access)
  4. Fix the syntax error at the reported location and retry FormatGoFile

Example fix

// before
func main() {
    fmt.Println("hi")
// after
func main() {
	fmt.Println("hi")
}
Defensive patterns

Strategy: try-catch

Validate before calling

if err := parser.ParseFile(token.NewFileSet(), filePath, nil, parser.ParseComments); err != nil {
	return fmt.Errorf("file has syntax errors, fix before formatting: %v", err)
}

Try / catch

if err := FormatGoFile(appId, fileName); err != nil {
	if strings.Contains(err.Error(), "gofmt failed") {
		var gofmtOutput string
		if idx := strings.Index(err.Error(), "Output: "); idx >= 0 {
			gofmtOutput = err.Error()[idx+len("Output: "):]
		}
		return fmt.Errorf("syntax error in %s: %s", fileName, gofmtOutput)
	}
	return err
}

Prevention

When it happens

Trigger: Running FormatGoFile on a .go file containing syntax errors (unclosed brace, invalid tokens) or a file gofmt cannot write (permissions/locked file).

Common situations: Saving a partially edited Go file and hitting format before it parses; generated code with template placeholders not yet filled in; corrupted file from an interrupted write.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/13080f20cc7451f2. Report an issue: GitHub.