{"record":{"id":"64a74c25da1915a6","repo":"golang/go","slug":"import-lookup-disabled-by-mod-s-s","errorCode":null,"errorMessage":"import lookup disabled by -mod=%s\n\t(%s)","messagePattern":"import lookup disabled by -mod=(.+?)\n\t\\((.+?)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modload/import.go","lineNumber":625,"sourceCode":"\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 {\n\t\treturn module.Version{}, err\n\t}","sourceCodeStart":607,"sourceCodeEnd":643,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modload/import.go#L607-L643","documentation":"Same precondition as error 1061 (readonly/vendor mode blocks import lookup) but emitted when the mode was set IMPLICITLY with a recorded reason (cfg.BuildModExplicit false, cfg.BuildModReason set). The message appends the human-readable reason so the user knows why -mod was enabled. The wrapped ImportMissingError still carries the unresolved path.","triggerScenarios":"An import is missing while BuildMod is readonly/vendor due to GOFLAGS=-mod=readonly, GOFLAGS=-mod=vendor, or because a vendored directory was detected, AND cfg.BuildModReason is non-empty (e.g. 'go: -mod=readonly set in GOFLAGS'). ld.allowMissingModuleImports is false.","commonSituations":"A shell rc file or CI image exports GOFLAGS=-mod=readonly globally; a project auto-entered vendor mode because ./vendor exists; the user is confused why -mod is active since they did not type it.","solutions":["Run 'go get <importpath>' to add the dependency, then rebuild.","Inspect and unset the implicit source: 'go env GOFLAGS' and remove -mod=readonly, or delete the GOFLAGS entry.","If vendor mode was auto-selected, run 'go mod vendor' after adding the dep, or remove the vendor/ dir to fall back to mod mode.","Override per-command: 'go build -mod=mod ./...' to allow the update this once."],"exampleFix":"// before\n$ go env GOFLAGS\n-mod=readonly\n$ go build ./...\n// import lookup disabled by -mod=readonly\n//\t(go: -mod=readonly set in GOFLAGS)\n\n// after\n$ go env -u GOFLAGS          // or: export GOFLAGS=\n$ go get <newdep> && go build ./...","handlingStrategy":"validation","validationCode":"// Detect implicit -mod source before building.\ngoflags := os.Getenv(\"GOFLAGS\")\nif strings.Contains(goflags, \"-mod=readonly\") || strings.Contains(goflags, \"-mod=vendor\") {\n    if err := exec.Command(\"go\", \"get\", \"./...\").Run(); err != nil { return err }\n}\n// or surface the cause to the user:\nif reason, _ := exec.Command(\"go\", \"env\", \"GOFLAGS\").Output(); len(reason) > 0 {\n    fmt.Printf(\"note: GOFLAGS=%s may block import lookup\\n\", strings.TrimSpace(string(reason)))\n}","typeGuard":null,"tryCatchPattern":"out, err := exec.Command(\"go\", \"build\", \"./...\").CombinedOutput()\nif err != nil && bytes.Contains(out, []byte(\"import lookup disabled by -mod=\")) {\n    // parse the reason line and unset its source (e.g. GOFLAGS)\n    fmt.Fprintln(os.Stderr, \"clearing implicit -mod; re-run after 'go get'\")\n    os.Unsetenv(\"GOFLAGS\")\n    out, err = exec.Command(\"go\", \"build\", \"./...\").CombinedOutput()\n}\nreturn err","preventionTips":["Do not export GOFLAGS=-mod=readonly globally in shell rc files.","Document the GOFLAGS contract in the repo README.","In CI, set -mod=readonly explicitly per-command rather than via env.","If a vendor/ dir exists, keep it complete via 'go mod vendor'."],"tags":["build-mod","goflags","implicit-config","vendor","missing-import"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}