{"record":{"id":"cb663ba06545074f","repo":"apache/hadoop","slug":"can-not-create-s-file-because-parent-folder-do","errorCode":null,"errorMessage":"Can not create '%s' file, because parent folder does not exist: %s","messagePattern":"Can not create '(.+?)' file, because parent folder does not exist: (.+?)","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-gcp/src/main/java/org/apache/hadoop/fs/gs/GoogleHadoopFileSystem.java","lineNumber":350,"sourceCode":"                this, getGcsPath(hadoopPath), fileOptions, statistics), statistics);\n      },\n      String.format(\"create(%s, %s)\", hadoopPath, overwrite));\n  }\n\n  @Override\n  public FSDataOutputStream createNonRecursive(\n      Path hadoopPath,\n      FsPermission permission,\n      EnumSet<CreateFlag> flags,\n      int bufferSize,\n      short replication,\n      long blockSize,\n      Progressable progress)\n      throws IOException {\n    URI gcsPath = getGcsPath(checkNotNull(hadoopPath, \"hadoopPath must not be null\"));\n    URI parentGcsPath = UriPaths.getParentPath(gcsPath);\n    if (!getGcsFs().getFileInfo(parentGcsPath).exists()) {\n      throw new FileNotFoundException(\n          String.format(\n              \"Can not create '%s' file, because parent folder does not exist: %s\",\n              gcsPath, parentGcsPath));\n    }\n\n    return create(\n        hadoopPath,\n        permission,\n        flags.contains(CreateFlag.OVERWRITE),\n        bufferSize,\n        replication,\n        blockSize,\n        progress);\n  }\n\n  /**\n   * Appends to an existing file (optional operation). Not supported.\n   *","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-gcp/src/main/java/org/apache/hadoop/fs/gs/GoogleHadoopFileSystem.java#L332-L368","documentation":"GoogleHadoopFileSystem.createNonRecursive explicitly refuses to create parent directories: it resolves the GCS parent path (UriPaths.getParentPath) and throws FileNotFoundException if getFileInfo(parent) reports it does not exist. This contrasts with create(), which implicitly creates intermediates.","triggerScenarios":"fs.createNonRecursive(new Path(\"gs://b/newdir/file.txt\"), ...) when gs://b/newdir was never created; also when a concurrent job deleted the parent between check and create.","commonSituations":"Code ported from FileSystems where non-recursive create auto-mkdirs; intentional strict-mode writes (fail if structure missing) where the caller forgot to build the structure first; race with cleanup jobs removing directories.","solutions":["Create the parent first: fs.mkdirs(path.getParent()).","Or simply use fs.create(path, ...), which creates intermediate directories implicitly.","For strict semantics, keep createNonRecursive but pre-validate the tree and fail with your own actionable error."],"exampleFix":"// before\nfs.createNonRecursive(new Path(\"gs://bucket/newdir/file.txt\"), FsPermission.getDefault(),\n    EnumSet.of(CreateFlag.CREATE), bufferSize, replication, blockSize, progress);\n// -> FileNotFoundException: parent folder does not exist\n\n// after\nif (!fs.exists(path.getParent())) {\n  fs.mkdirs(path.getParent());\n}\nfs.createNonRecursive(path, FsPermission.getDefault(),\n    EnumSet.of(CreateFlag.CREATE), bufferSize, replication, blockSize, progress);","handlingStrategy":"validation","validationCode":"if (!fs.exists(path.getParent())) {\n  fs.mkdirs(path.getParent());\n}\nfs.createNonRecursive(path, FsPermission.getDefault(), EnumSet.of(CreateFlag.CREATE),\n    bufferSize, replication, blockSize, null);","typeGuard":null,"tryCatchPattern":"catch FileNotFoundException with message contains(\"parent folder does not exist\") - create the parent (mkdirs) and retry, or switch to fs.create() which creates parents implicitly.","preventionTips":["Reserve createNonRecursive for strict-structure writes and always pre-build the directory tree.","Know the contrast: create() auto-creates intermediates, createNonRecursive does not."],"tags":["create","gcs","hadoop","directories","file-not-found"],"backgroundTag":"parent-directory-missing","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}