{"record":{"id":"1d6b15493f234c71","repo":"apple/pkl","slug":"cannotfindresource-1d6b15","errorCode":"cannotFindResource","errorMessage":"cannotFindResource","messagePattern":"cannotFindResource","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/unary/ReadNode.java","lineNumber":37,"sourceCode":"import com.oracle.truffle.api.dsl.Specialization;\nimport com.oracle.truffle.api.nodes.NodeInfo;\nimport com.oracle.truffle.api.source.SourceSection;\nimport org.pkl.core.module.ModuleKey;\nimport org.pkl.core.runtime.VmContext;\n\n@NodeInfo(shortName = \"read\")\npublic abstract class ReadNode extends AbstractReadNode {\n  protected ReadNode(SourceSection sourceSection, ModuleKey moduleKey) {\n    super(sourceSection, moduleKey);\n  }\n\n  @Specialization\n  public Object read(String resourceUri) {\n    var result = doRead(resourceUri, VmContext.get(this), this);\n    if (result != null) return result;\n\n    CompilerDirectives.transferToInterpreter();\n    throw exceptionBuilder().evalError(\"cannotFindResource\", resourceUri).build();\n  }\n}\n","sourceCodeStart":19,"sourceCodeEnd":40,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/unary/ReadNode.java#L19-L40","documentation":"Thrown by the `read()` builtin when no reader can produce content for the given URI string — the resource does not exist or is not accessible. This is Pkl's equivalent of file-not-found for the read API, and the requested URI is included in the error.","triggerScenarios":"Calling `read(\"file.txt\")`, `read(\"env:VAR\")`, or `read(\"prop:x\")` where the resource manager resolves nothing: nonexistent file, unset environment variable, undefined property, wrong URI scheme.","commonSituations":"Typos in file paths, reading files outside the module path, missing environment variables in CI, referencing resources deleted or renamed after authoring the module.","solutions":["Verify the URI string is correct and the file/resource actually exists at evaluation time.","For relative paths, resolve them against the module's location — check the current module directory.","For `env:` reads, ensure the environment variable is exported in the evaluation environment.","Add the file's directory to the module path if it is outside the current tree.","Use `read().exists`/catch semantics or provide a default via null-safe access when absence is expected."],"exampleFix":"// before\nval cfg = read(\"config.pkl\").text\n// after\nval cfg = if (read?(\"config.pkl\") != null) read(\"config.pkl\").text else \"\"","handlingStrategy":"validation","validationCode":"function safeRead(uri) =\n  if (uri.startsWith(\"env:\")) {\n    val name = uri.substring(4)\n    if (System.getenv(name) == null) throw new Error(\"env var not set: \" + name)\n  }\n  uri","typeGuard":"const resourceLikelyExists = (uri) => uri.startsWith('env:') ? process.env[uri.slice(4)] != null : fs.existsSync(resolve(uri))","tryCatchPattern":"try {\n  content = read(uri).text\n} catch (e) {\n  if (e.code === 'cannotFindResource') content = defaultContent(e.data.resourceUri)\n  else throw e\n}","preventionTips":["Verify file paths and schemes before calling read()","Resolve relative paths against the module's directory","Ensure env: variables are exported in the evaluation environment","Keep referenced resources inside the module path","Provide defaults for resources that may be absent"],"tags":["read","resource","file-not-found"],"backgroundTag":"resource-not-found","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}