{"record":{"id":"fe8d5e6ae317bb80","repo":"apple/pkl","slug":"cannothaverelativeimport","errorCode":"cannotHaveRelativeImport","errorMessage":"cannotHaveRelativeImport ${moduleUri}","messagePattern":"cannotHaveRelativeImport (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java","lineNumber":3280,"sourceCode":"    } catch (SecurityManagerException | PackageLoadError e) {\n      throw exceptionBuilder().withSourceSection(createSourceSection(ctx)).withCause(e).build();\n    } catch (VmException e) {\n      throw exceptionBuilder()\n          .evalError(e.getMessage(), e.getMessageArguments())\n          .withCause(e.getCause())\n          .withHintBuilder(e.getHintBuilder())\n          .withSourceSection(createSourceSection(ctx))\n          .build();\n    } catch (ExternalReaderProcessException e) {\n      throw exceptionBuilder()\n          .evalError(\"externalReaderFailure\")\n          .withCause(e.getCause())\n          .withSourceSection(createSourceSection(ctx))\n          .build();\n    }\n\n    if (!resolvedUri.isAbsolute()) {\n      throw exceptionBuilder()\n          .evalError(\"cannotHaveRelativeImport\", moduleKey.getUri())\n          .withSourceSection(createSourceSection(ctx))\n          .build();\n    }\n    return resolvedUri;\n  }\n\n  private ConstLevel getConstLevel(int modifiers) {\n    if (VmModifier.isConst(modifiers)) return ConstLevel.ALL;\n    return symbolTable.getCurrentScope().getConstLevel();\n  }\n\n  private VmException missingLocalPropertyValue(TypeAnnotation typeAnn) {\n    var stop = typeAnn.span().stopIndex();\n    return exceptionBuilder()\n        .evalError(\"missingLocalPropertyValue\")\n        .withSourceSection(source.createSection(stop + 1, 0))\n        .build();","sourceCodeStart":3262,"sourceCodeEnd":3298,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java#L3262-L3298","documentation":"After resolution, if the resulting module URI is not absolute, the builder rejects the import: every loaded module must resolve to an absolute URI. A relative string that could not be resolved against the importing module's URI triggers this.","triggerScenarios":"An `import` with a relative path used in a context with no base URI to resolve against (e.g. module loaded from a string/stdin), leaving `resolvedUri` relative.","commonSituations":"Evaluating modules from stdin or in-memory where relative imports have no filesystem anchor; programmatic use of `pkl` APIs with a module source lacking a base.","solutions":["Load the module from a concrete file/project so relative imports can resolve","Rewrite the import as an absolute URI (file:///... or package://...)","Pass an explicit base/module URI when using the embedding APIs"],"exampleFix":"// before (no base for relative import)\nimport \"config/base.pkl\"\n// after\nimport \"file:///opt/pkl/config/base.pkl\"","handlingStrategy":"validation","validationCode":"function resolvesAbsolute(base, ref) {\n  try { return new URL(ref, base).href } catch { return null }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Load modules from real files/projects so relative imports have a base","When embedding Pkl, always supply a concrete module/base URI","Use absolute URIs for imports in string-sourced modules"],"tags":["pkl","import","uri","relative-path"],"backgroundTag":"invalid-url","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}