Skip to content

Fix go vet: non-constant format string in robots.go#7

Merged
bdk38 merged 2 commits intomainfrom
copilot/fix-go-vet-warning
Mar 6, 2026
Merged

Fix go vet: non-constant format string in robots.go#7
bdk38 merged 2 commits intomainfrom
copilot/fix-go-vet-warning

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 6, 2026

go vet was failing because fmt.Fprintf(ctx, paths.String()) passes a variable as the format string, which is both a vet warning and a latent correctness issue if the string ever contains % characters.

Changes

  • internal/http/robots.go: Replace fmt.Fprintf with ctx.WriteString — no formatting is happening here, so writing directly is both correct and more efficient. Removed the now-unused fmt import.
// before
if _, err := fmt.Fprintf(ctx, paths.String()); err != nil {

// after
if _, err := ctx.WriteString(paths.String()); err != nil {
Original prompt

Fix go vet Warning: Non-Constant Format String

Problem

The build is failing with a go vet error:

# github.com/bdk38/HellPot/internal/http
internal/http/robots.go:30:32: non-constant format string in call to fmt.Fprintf
make: *** [Makefile:5: check] Error 1

Root Cause

In internal/http/robots.go line 30, we're using fmt.Fprintf with a variable string:

if _, err := fmt.Fprintf(ctx, paths.String()); err != nil {
    slog.Error().Err(err).Msg("SERVE_ROBOTS_ERROR")
}

go vet correctly identifies this as a potential security issue because paths.String() is not a constant format string. If the string contains format specifiers like %s or %d, it could cause unexpected behavior or security vulnerabilities.

Solution

Since we're not actually doing any formatting (just writing a plain string), we should use ctx.WriteString() directly instead of fmt.Fprintf.

Change Required

In file internal/http/robots.go, line 30:

OLD CODE:

if _, err := fmt.Fprintf(ctx, paths.String()); err != nil {
    slog.Error().Err(err).Msg("SERVE_ROBOTS_ERROR")
}

NEW CODE:

if _, err := ctx.WriteString(paths.String()); err != nil {
    slog.Error().Err(err).Msg("SERVE_ROBOTS_ERROR")
}

Why This Fix is Better

  1. Fixes the go vet warning - No more non-constant format string issue
  2. More efficient - Direct write without format parsing overhead
  3. More correct - We're not formatting, just writing a string
  4. Same behavior - ctx.WriteString() returns the same (int, error) signature

Alternative Fix (if needed)

If for some reason ctx.WriteString() doesn't work, we could use fmt.Fprint() (without the f):

if _, err := fmt.Fprint(ctx, paths.String()); err != nil {

But ctx.WriteString() is the preferred approach since we're working with fasthttp's RequestCtx.

Testing

After the fix:

  1. Run make - should pass without errors
  2. Run go vet ./... - should pass without warnings
  3. Test robots.txt endpoint - should work as before

Impact

  • ✅ Low risk - Simple function replacement
  • ✅ Same behavior - Just changes how we write the string
  • ✅ Allows build to succeed

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…s.go

Co-authored-by: bdk38 <244781262+bdk38@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix go vet warning for non-constant format string Fix go vet: non-constant format string in robots.go Mar 6, 2026
Copilot finished work on behalf of bdk38 March 6, 2026 17:54
@bdk38 bdk38 marked this pull request as ready for review March 6, 2026 17:55
@bdk38 bdk38 merged commit 9822500 into main Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants