{"record":{"id":"1636d66613daa9ba","repo":"golang/go","slug":"cannot-embed-s-s-in-different-module","errorCode":null,"errorMessage":"cannot embed %s %s: in different module","messagePattern":"cannot embed (.+?) (.+?): in different module","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/load/pkg.go","lineNumber":2207,"sourceCode":"\t\tvar list []string\n\t\tfor _, file := range match {\n\t\t\t// relative path to p.Dir which begins without prefix slash\n\t\t\trel := filepath.ToSlash(str.TrimFilePathPrefix(file, pkgdir))\n\n\t\t\twhat := \"file\"\n\t\t\tinfo, err := fsys.Lstat(file)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, nil, err\n\t\t\t}\n\t\t\tif info.IsDir() {\n\t\t\t\twhat = \"directory\"\n\t\t\t}\n\n\t\t\t// Check that directories along path do not begin a new module\n\t\t\t// (do not contain a go.mod).\n\t\t\tfor dir := file; len(dir) > len(pkgdir)+1 && !dirOK[dir]; dir = filepath.Dir(dir) {\n\t\t\t\tif _, err := fsys.Stat(filepath.Join(dir, \"go.mod\")); err == nil {\n\t\t\t\t\treturn nil, nil, fmt.Errorf(\"cannot embed %s %s: in different module\", what, rel)\n\t\t\t\t}\n\t\t\t\tif dir != file {\n\t\t\t\t\tif info, err := fsys.Lstat(dir); err == nil && !info.IsDir() {\n\t\t\t\t\t\treturn nil, nil, fmt.Errorf(\"cannot embed %s %s: in non-directory %s\", what, rel, dir[len(pkgdir)+1:])\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdirOK[dir] = true\n\t\t\t\tif elem := filepath.Base(dir); isBadEmbedName(elem) {\n\t\t\t\t\tif dir == file {\n\t\t\t\t\t\treturn nil, nil, fmt.Errorf(\"cannot embed %s %s: invalid name %s\", what, rel, elem)\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn nil, nil, fmt.Errorf(\"cannot embed %s %s: in invalid directory %s\", what, rel, elem)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tswitch {\n\t\t\tdefault:","sourceCodeStart":2189,"sourceCodeEnd":2225,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/load/pkg.go#L2189-L2225","documentation":"Thrown while walking each ancestor directory of an embed match (loop at line 2204). For every directory from the matched file up toward pkgdir, the loader stats <dir>/go.mod; if a go.mod exists, the embed target is inside a different module. go:embed deliberately cannot cross module boundaries because the embedded bytes would not be reproducible from go.mod alone.","triggerScenarios":"Embedding a path that descends into a nested module: e.g. a vendored copy or a git submodule that has its own go.mod, or a workspace member nested under the main module directory. The match was produced by fsys.Glob, then the ancestor walk finds the foreign go.mod.","commonSituations":"Adding a nested git submodule that ships a go.mod; using a `replace` to a local path that itself is a module and then trying to embed its files from the parent; embedding a directory that contains a checked-out dependency.","solutions":["Move the files you want to embed out of the nested module directory into the current package's tree so no go.mod sits between them.","Delete the stray go.mod in the subdirectory if that subdirectory is not meant to be its own module.","Restructure so the embedded assets live in the same module as the package with the //go:embed directive.","If you really need the nested module's files, copy them at build time via go:generate rather than //go:embed."],"exampleFix":"// before: //go:embed vendor/foolib/assets  (vendor/foolib/go.mod exists)\n// after: move assets to ./assets in the main module, then\n//go:embed assets","handlingStrategy":"validation","validationCode":"// Ensure no go.mod sits between the package dir and any embed target.\npackage embedcheck\n\nimport (\n\t\"errors\"\n\t\"os\"\n\t\"path/filepath\"\n)\n\nfunc NoNestedModuleBetween(pkgDir, target string) error {\n\tabs, err := filepath.Abs(filepath.Join(pkgDir, target))\n\tif err != nil {\n\t\treturn err\n\t}\n\tfor d := abs; len(d) > len(pkgDir)+1; d = filepath.Dir(d) {\n\t\tif _, err := os.Stat(filepath.Join(d, \"go.mod\")); err == nil {\n\t\t\treturn errors.New(\"embed target crosses module boundary at \" + d)\n\t\t}\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep all embedded assets inside the same module as the embedding package.","Beware git submodules and `replace` directives into local modules — both introduce go.mod files inside your tree."],"tags":["embed","modules","go-embed","build"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}