Skip to content

🐛 fiber migrate produces invalid code for filesystem middleware migration #267

@CybotTM

Description

@CybotTM

Bug Description

The fiber migrate --to v3.0.0 command produces invalid Go code when migrating the filesystem middleware to the new static middleware.

Issues Found

1. Invalid os.DirFS() usage with http.FS()

The migration tool generates:

app.Use("/static", static.New("", static.Config{
    FS:     os.DirFS(http.FS(embedFS)),
    MaxAge: 24 * 60 * 60,
}))

This is invalid because http.FS() returns http.FileSystem, not a string path that os.DirFS() expects.

Correct code should be:

app.Use("/static", static.New("", static.Config{
    FS:     embedFS,  // embed.FS implements fs.FS directly
    MaxAge: 24 * 60 * 60,
}))

2. Import naming conflict

When a project has its own package named static (e.g., internal/web/static for embedded assets), the migration tool imports github.com/gofiber/fiber/v3/middleware/static without aliasing, causing a naming conflict:

import (
    "github.com/gofiber/fiber/v3/middleware/static"
    "myproject/internal/web/static"  // Conflict!
)

Steps to Reproduce

  1. Create a Fiber v2 project with:

    • An embedded filesystem using embed.FS
    • A package named static (e.g., internal/web/static)
    • Usage of filesystem.New() middleware
  2. Run fiber migrate --to v3.0.0

  3. Observe the generated code fails to compile

Expected Behavior

The migration tool should:

  1. Recognize that embed.FS already implements fs.FS and use it directly
  2. Detect naming conflicts and add an alias (e.g., staticmw "github.com/gofiber/fiber/v3/middleware/static")

Environment

  • Fiber CLI version: v0.11.3
  • Go version: 1.25.7
  • OS: Linux

Original Code (v2)

import (
    "net/http"
    "github.com/gofiber/fiber/v2/middleware/filesystem"
    "myproject/internal/web/static"
)

app.Use("/static", filesystem.New(filesystem.Config{
    Root:   http.FS(static.Static),
    MaxAge: 24 * 60 * 60,
}))

Generated Code (broken)

import (
    "os"
    "net/http"
    "github.com/gofiber/fiber/v3/middleware/static"
    "myproject/internal/web/static"  // CONFLICT
)

app.Use("/static", static.New("", static.Config{
    FS:     os.DirFS(http.FS(static.Static)),  // INVALID
    MaxAge: 24 * 60 * 60,
}))

Correct Code (manual fix)

import (
    "github.com/gofiber/fiber/v3/middleware/static"
    webstatic "myproject/internal/web/static"  // Aliased
)

app.Use("/static", static.New("", static.Config{
    FS:     webstatic.Static,  // Direct use of embed.FS
    MaxAge: 24 * 60 * 60,
}))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions