{"record":{"id":"23a8741b654655a8","repo":"golang/go","slug":"import-lookup-disabled-by-mod-s","errorCode":null,"errorMessage":"import lookup disabled by -mod=%s","messagePattern":"import lookup disabled by -mod=(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modload/import.go","lineNumber":623,"sourceCode":"\t\t//\n\t\t// Instead of trying QueryPattern, report an ImportMissingError immediately.\n\t\treturn module.Version{}, &ImportMissingError{\n\t\t\tPath:                      path,\n\t\t\tisStd:                     true,\n\t\t\tmodContainingCWD:          ld.MainModules.ModContainingCWD(),\n\t\t\tallowMissingModuleImports: ld.allowMissingModuleImports,\n\t\t}\n\t}\n\n\tif (cfg.BuildMod == \"readonly\" || cfg.BuildMod == \"vendor\") && !ld.allowMissingModuleImports {\n\t\t// In readonly mode, we can't write go.mod, so we shouldn't try to look up\n\t\t// the module. If readonly mode was enabled explicitly, include that in\n\t\t// the error message.\n\t\t// In vendor mode, we cannot use the network or module cache, so we\n\t\t// shouldn't try to look up the module\n\t\tvar queryErr error\n\t\tif cfg.BuildModExplicit {\n\t\t\tqueryErr = fmt.Errorf(\"import lookup disabled by -mod=%s\", cfg.BuildMod)\n\t\t} else if cfg.BuildModReason != \"\" {\n\t\t\tqueryErr = fmt.Errorf(\"import lookup disabled by -mod=%s\\n\\t(%s)\", cfg.BuildMod, cfg.BuildModReason)\n\t\t}\n\t\treturn module.Version{}, &ImportMissingError{\n\t\t\tPath:                      path,\n\t\t\tQueryErr:                  queryErr,\n\t\t\tmodContainingCWD:          ld.MainModules.ModContainingCWD(),\n\t\t\tallowMissingModuleImports: ld.allowMissingModuleImports,\n\t\t}\n\t}\n\n\t// Look up module containing the package, for addition to the build list.\n\t// Goal is to determine the module, download it to dir,\n\t// and return m, dir, ImportMissingError.\n\tfmt.Fprintf(os.Stderr, \"go: finding module for package %s\\n\", path)\n\n\tmg, err := rs.Graph(ld, ctx)\n\tif err != nil {","sourceCodeStart":605,"sourceCodeEnd":641,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modload/import.go#L605-L641","documentation":"Returned (wrapped in ImportMissingError.QueryErr) when an import path cannot be resolved to a module because -mod=readonly or -mod=vendor is in effect and module lookup is disabled. Readonly forbids modifying go.mod to add a new dependency; vendor forbids network/cache lookups entirely. The error surfaces only when cfg.BuildModExplicit is true (the user passed -mod=... explicitly).","triggerScenarios":"Building/importing a package whose module is not in go.mod while -mod=readonly is set explicitly on the command line (go build -mod=readonly), and ld.allowMissingModuleImports is false. cfg.BuildModExplicit is true so the plain message form is chosen.","commonSituations":"Adding a new import in code then running 'go build -mod=readonly' before 'go get'; CI that hard-codes -mod=readonly for reproducibility; vendored projects where the vendor/modules.txt is missing the new module.","solutions":["Run 'go get <importpath>@latest' (or the desired version) to add the missing module to go.mod, then rebuild.","Switch to -mod=mod for that invocation: 'go build -mod=mod' to let go.mod be updated automatically.","If vendoring, run 'go mod vendor' after adding the dependency so vendor/ and modules.txt include it.","Check that the import path is spelled correctly and the module is publicly fetchable."],"exampleFix":"// before\n$ go build -mod=readonly ./...\n// import lookup disabled by -mod=readonly\n\n// after\n$ go get golang.org/x/example@latest\n$ go build -mod=readonly ./...","handlingStrategy":"validation","validationCode":"// Before building, ensure every import is already in go.mod when you must use -mod=readonly.\n// Run: go list -mod=readonly -e -deps ./... and check for ImportMissingError markers,\n// or simply resolve deps ahead of time:\ncmd := exec.Command(\"go\", \"get\", \"./...\")\ncmd.Env = append(os.Environ(), \"GOFLAGS=-mod=mod\")\nif err := cmd.Run(); err != nil { return err }\n// now safe to use -mod=readonly","typeGuard":null,"tryCatchPattern":"out, err := exec.Command(\"go\", \"build\", \"-mod=readonly\", \"./...\").CombinedOutput()\nif err != nil && bytes.Contains(out, []byte(\"import lookup disabled by -mod=\")) {\n    // missing dep under readonly: run 'go get' then retry\n    if gerr := exec.Command(\"go\", \"get\", \"./...\").Run(); gerr != nil { return gerr }\n    out, err = exec.Command(\"go\", \"build\", \"-mod=readonly\", \"./...\").CombinedOutput()\n}\nreturn err","preventionTips":["Run 'go mod tidy' before builds that use -mod=readonly.","Pre-commit hook: 'go list -mod=readonly -deps ./...' to catch missing deps early.","Keep GOFLAGS=-mod=readonly in CI but document that 'go get' must precede new imports.","Ensure IDE add-import tooling also runs 'go mod tidy'."],"tags":["build-mod","readonly","vendor","missing-import","go-get"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}