lionsoul2014/ip2region · error

failed to open embedded file `%s`: %w

Error message

failed to open embedded file `%s`: %w

What it means

LoadContentFromFS opens a file inside an embed.FS by path. If fs.Open fails — the path was not embedded, the embed pattern excluded it, or the path is wrong — the error is wrapped as 'failed to open embedded file `%s`: %w'. This is the embedded-asset equivalent of a file-not-found error.

Source

Thrown at binding/golang/xdb/util.go:300

	handle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)
	if err != nil {
		return nil, fmt.Errorf("open xdb file `%s`: %w", dbFile, err)
	}
	defer handle.Close()

	cBuff, err := LoadContent(handle)
	if err != nil {
		return nil, err
	}

	return cBuff, nil
}

// LoadContentFromFS load the whole xdb binary from embed.FS
func LoadContentFromFS(fs embed.FS, filePath string) ([]byte, error) {
	file, err := fs.Open(filePath)
	if err != nil {
		return nil, fmt.Errorf("failed to open embedded file `%s`: %w", filePath, err)
	}
	defer file.Close()

	var cBuff []byte
	cBuff, err = io.ReadAll(file)
	if err != nil {
		return nil, fmt.Errorf("failed to read embedded file `%s`: %w", filePath, err)
	}

	return cBuff, nil
}

View on GitHub (pinned to c1a1fc7d59)

Solutions

  1. Confirm the //go:embed directive covers the file (e.g. //go:embed data/ip2region.xdb) and the file is committed in that directory.
  2. Use the exact slash-separated package-relative path (e.g. "data/ip2region.xdb"), never an absolute path.
  3. Check that embed.FS was non-nil and the file was included via fs.ReadFile or embed.FS listing before searching.
  4. For dynamic assets, fall back to LoadContentFromFile since embed.FS is fixed at compile time.

Example fix

// before
//go:embed data/*.bin
var xdbFS embed.FS

cBuff, err := xdb.LoadContentFromFS(xdbFS, "data/ip2region.xdb") // .xdb not matched
// after
//go:embed data/ip2region.xdb
var xdbFS embed.FS

cBuff, err := xdb.LoadContentFromFS(xdbFS, "data/ip2region.xdb")
Defensive patterns

Strategy: validation

Validate before calling

//go:embed data/ip2region.xdb
var xdbFS embed.FS

// at startup:
if _, err := xdbFS.Open("data/ip2region.xdb"); err != nil {
    return fmt.Errorf("xdb not embedded: %w", err)
}

Type guard

func embeddedXdbExists(fsys embed.FS, path string) bool {
    _, err := fs.Stat(fsys, path)
    return err == nil
}

Try / catch

cBuff, err := xdb.LoadContentFromFS(xdbFS, "data/ip2region.xdb")
if err != nil {
    if errors.Is(err, fs.ErrNotExist) {
        return fmt.Errorf("embed pattern does not include %s", "data/ip2region.xdb")
    }
    return err
}

Prevention

When it happens

Trigger: Calling LoadContentFromFS with a filePath that does not match the //go:embed pattern, using an absolute path (embed paths are slash-separated and relative to the package), or the embed directive missing the xdb entirely so compilation embeds nothing.

Common situations: Placing the xdb outside the embedded directory; forgetting to add it to //go:embed; using OS-style backslashes on Windows source code; renaming the data directory without updating the embed pattern; build tags excluding the asset.

Related errors


AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02). Data as JSON: /api/errors/5cd4c9ea877516a5. Report an issue: GitHub.