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

  1. Verify the path exists in the FS with fs.Stat(filesystem, root) before configuring the middleware.
  2. For embed.FS, use the path relative to the directory containing the //go:embed directive, and ensure the embed pattern actually includes it.
  3. If using fs.Sub, make sure root is relative to the sub-root.
  4. 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

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


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/e305b09cb493b82a. Report an issue: GitHub.