gofiber/fiber · error
static: %w
Error message
static: %w
What it means
Thrown by isFile when a custom fs.FS was supplied and filesystem.Open(root) returns an error. The %w wraps the fs.FS error. This fires before any stat: the configured root path could not be opened within the provided/embedded filesystem.
Source
Thrown at middleware/static/static.go:338
// Reset response to default
c.RequestCtx().SetContentType("") // Issue #420
c.RequestCtx().Response.SetStatusCode(fiber.StatusOK)
c.RequestCtx().Response.SetBodyString("")
// Next middleware
return c.Next()
}
}
// isFile checks if the root is a file.
func isFile(root string, filesystem fs.FS) (bool, error) {
var file fs.File
var err error
if filesystem != nil {
file, err = filesystem.Open(root)
if err != nil {
return false, fmt.Errorf("static: %w", err)
}
defer func() {
_ = file.Close() //nolint:errcheck // not needed
}()
} else {
file, err = os.Open(filepath.Clean(root))
if err != nil {
return false, fmt.Errorf("static: %w", err)
}
defer func() {
_ = file.Close() //nolint:errcheck // not needed
}()
}
stat, err := file.Stat()
if err != nil {
return false, fmt.Errorf("static: %w", err)
}View on GitHub (pinned to a105acad6c)
Solutions
- Verify the path exists in the FS with fs.Stat(filesystem, root) before configuring the middleware.
- For embed.FS, use the path relative to the directory containing the //go:embed directive, and ensure the embed pattern actually includes it.
- If using fs.Sub, make sure root is relative to the sub-root.
- Spell-check the root and confirm the build includes the embedded files.
Example fix
// before
app.Use("/", static.New("assets", static.FS(embeddedFS)))
// -> static: open assets: no such file
// after — verify, and use the correct path inside the FS
if _, err := fs.Stat(embeddedFS, "static/assets"); err != nil { log.Fatal(err) }
app.Use("/", static.New("static/assets", static.FS(embeddedFS))) Defensive patterns
Strategy: validation
Validate before calling
// Verify the path exists inside the FS before wiring up static.
if _, err := fs.Stat(filesystem, root); err != nil {
log.Fatalf("static FS root %q: %v", root, err)
}
app.Use("/", static.New(root, static.FS(filesystem))) Type guard
func fsPathExists(fsys fs.FS, p string) bool {
if fsys == nil { return false }
_, err := fs.Stat(fsys, p)
return err == nil
} Try / catch
_, err := isFile(root, filesystem)
if err != nil {
log.Printf("static FS open failed for %q, disabling static: %v", root, err)
// proceed without static, or fail fast at startup
} Prevention
- For embed.FS, use paths relative to the //go:embed directory and confirm the embed pattern includes them.
- If using fs.Sub, remember root is relative to the sub-root.
- Add a startup fs.Stat check for the static root.
- Unit-test static config against a known-good embedded fixture.
When it happens
Trigger: Static middleware is configured with an FS (e.g. embed.FS, io/fs overlay) and the root path does not exist in that FS, is not a regular file path the FS recognizes, or permission/scope rules of the FS reject it.
Common situations: embed.FS path that omits the package-relative prefix (e.g. "./static" vs "static"); root pointing outside an fs.Sub-rooted filesystem; typo in the root; embed directive that didn't include the files.
Related errors
- ErrInvalidPath
- fiber: failed to reload views: %w
- open file error: %w
- failed to check directory: %w
- failed to create directory: %w
AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11).
Data as JSON: /api/errors/e305b09cb493b82a.
Report an issue: GitHub.