{"record":{"id":"c63bab74f2864c35","repo":"golang/go","slug":"local-imports-disallowed","errorCode":null,"errorMessage":"local imports disallowed","messagePattern":"local imports disallowed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/compile/internal/noder/import.go","lineNumber":64,"sourceCode":"\treturn pkg, err\n}\n\nfunc isDriveLetter(b byte) bool {\n\treturn 'a' <= b && b <= 'z' || 'A' <= b && b <= 'Z'\n}\n\n// is this path a local name? begins with ./ or ../ or /\nfunc islocalname(name string) bool {\n\treturn strings.HasPrefix(name, \"/\") ||\n\t\truntime.GOOS == \"windows\" && len(name) >= 3 && isDriveLetter(name[0]) && name[1] == ':' && name[2] == '/' ||\n\t\tstrings.HasPrefix(name, \"./\") || name == \".\" ||\n\t\tstrings.HasPrefix(name, \"../\") || name == \"..\"\n}\n\nfunc openPackage(path string) (*os.File, error) {\n\tif islocalname(path) {\n\t\tif base.Flag.NoLocalImports {\n\t\t\treturn nil, errors.New(\"local imports disallowed\")\n\t\t}\n\n\t\tif base.Flag.Cfg.PackageFile != nil {\n\t\t\treturn os.Open(base.Flag.Cfg.PackageFile[path])\n\t\t}\n\n\t\t// try .a before .o.  important for building libraries:\n\t\t// if there is an array.o in the array.a library,\n\t\t// want to find all of array.a, not just array.o.\n\t\tif file, err := os.Open(fmt.Sprintf(\"%s.a\", path)); err == nil {\n\t\t\treturn file, nil\n\t\t}\n\t\tif file, err := os.Open(fmt.Sprintf(\"%s.o\", path)); err == nil {\n\t\t\treturn file, nil\n\t\t}\n\t\treturn nil, errors.New(\"file not found\")\n\t}\n","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/compile/internal/noder/import.go#L46-L82","documentation":"Thrown by openPackage (src/cmd/compile/internal/noder/import.go) when the import path is a local name (starts with ./ or ../ or /, or is . or ..) and the compiler was invoked with -n / NoLocalImports. This flag forbids imports relative to the local file system, typically used when building the standard library or in constrained build environments.","triggerScenarios":"A Go source file contains `import \"./sub\"` or `import \"../sibling\"`, the path is recognized as local by islocalname, and base.Flag.NoLocalImports is true (compiler flag -n).","commonSituations":"Building stdlib or runtime packages where the build system passes -n to enforce canonical import paths; using relative imports in a module-based project (relative imports are discouraged); a build tool that sets NoLocalImports by default.","solutions":["Replace the local/relative import with the full canonical module import path.","If you control the compiler invocation, do not pass -n (NoLocalImports) unless the build environment requires it.","In module mode, ensure go.mod declares the module so you can use the full path."],"exampleFix":"// before\nimport \"./util\"\n// after\nimport \"example.com/myproject/util\"","handlingStrategy":"validation","validationCode":"// Flag relative imports in source before compiling with -n.\nimport \"strings\"\nfunc isRelativeImport(p string) bool {\n    return strings.HasPrefix(p, \"./\") || strings.HasPrefix(p, \"../\") || p == \".\" || p == \"..\"\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use canonical module import paths instead of relative ones.","Only pass -n (NoLocalImports) when building stdlib/runtime where it is required.","Lint for relative imports with goimports/golangci-lint in module projects."],"tags":["compiler","imports","local-imports","go-build"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}