{"record":{"id":"2364fbe3d44d0be2","repo":"apache/beam","slug":"trying-to-read-metadata-resourceid-which-is-a-directory","errorCode":null,"errorMessage":"Trying to read \" + metadata.resourceId() + \" which is a directory","messagePattern":"Trying to read \" \\+ metadata\\.resourceId\\(\\) \\+ \" which is a directory","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileIO.java","lineNumber":874,"sourceCode":"      builder.add(DisplayData.item(\"compression\", getCompression().toString()));\n      builder.add(DisplayData.item(\"directoryTreatment\", getDirectoryTreatment().toString()));\n    }\n\n    /**\n     * @return True if metadata is a directory and directory Treatment is SKIP.\n     * @throws java.lang.IllegalArgumentException if metadata is a directory and directoryTreatment\n     *     is Prohibited.\n     * @throws java.lang.UnsupportedOperationException if metadata is a directory and\n     *     directoryTreatment is not SKIP or PROHIBIT.\n     */\n    static boolean shouldSkipDirectory(\n        MatchResult.Metadata metadata, DirectoryTreatment directoryTreatment) {\n      if (metadata.resourceId().isDirectory()) {\n        switch (directoryTreatment) {\n          case SKIP:\n            return true;\n          case PROHIBIT:\n            throw new IllegalArgumentException(\n                \"Trying to read \" + metadata.resourceId() + \" which is a directory\");\n\n          default:\n            throw new UnsupportedOperationException(\n                \"Unknown DirectoryTreatment: \" + directoryTreatment);\n        }\n      }\n\n      return false;\n    }\n\n    /**\n     * Converts metadata to readableFile. Make sure {@link\n     * #shouldSkipDirectory(org.apache.beam.sdk.io.fs.MatchResult.Metadata,\n     * org.apache.beam.sdk.io.FileIO.ReadMatches.DirectoryTreatment)} returns false before using.\n     */\n    static ReadableFile matchToReadableFile(\n        MatchResult.Metadata metadata, Compression compression) {","sourceCodeStart":856,"sourceCodeEnd":892,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileIO.java#L856-L892","documentation":"FileIO's ReadableFile matching logic throws IllegalArgumentException when DirectoryTreatment.PROHIBIT is set and a matched resource is a directory. It lets users explicitly forbid directories in matched results instead of silently skipping them.","triggerScenarios":"FileIO.match()/matchAll() with .withDirectoryTreatment(DirectoryTreatment.PROHIBIT) while the match pattern (or a recursive match) resolves to a directory.","commonSituations":"Using a broad glob like 'gs://bucket/data/*' that includes subdirectories, combined with PROHIBIT to catch mistakes — often from bucket-layout changes adding new folders.","solutions":["Change DirectoryTreatment.PROHIBIT to SKIP if directories are acceptable to ignore.","Narrow the file pattern so it matches only files (e.g. append an extension glob like *.json).","Remove/move the offending directory out of the matched path."],"exampleFix":"// before\nFileIO.match().to(\"gs://bucket/data/*\").withDirectoryTreatment(DirectoryTreatment.PROHIBIT)\n// after\nFileIO.match().to(\"gs://bucket/data/*.json\").withDirectoryTreatment(DirectoryTreatment.SKIP)","handlingStrategy":"validation","validationCode":"if (metadata.resourceId().isDirectory() && treatment == DirectoryTreatment.PROHIBIT) { throw new IllegalArgumentException(\"Pattern must not match directories\"); }","typeGuard":null,"tryCatchPattern":"try { FileIO.match()... } catch (IllegalArgumentException e) { /* narrow the glob or switch to SKIP */ }","preventionTips":["Use file-extension globs (*.csv, *.json) instead of wildcard-all patterns.","Default to DirectoryTreatment.SKIP unless directory presence is a hard error.","Audit bucket/folder layouts after schema or pipeline changes."],"tags":["java","apache-beam","fileio","directory"],"backgroundTag":"incompatible-source-type","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}