{"record":{"id":"812bc0746dd45eff","repo":"bazelbuild/bazel","slug":"error-writing-shard-file-shardfile","errorCode":null,"errorMessage":"Error writing shard file ${shardFile}","messagePattern":"Error writing shard file (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/ShardingEnvironment.java","lineNumber":81,"sourceCode":"  /**\n   * Creates the shard file that is used to indicate that tests are\n   * being sharded.\n   */\n  public void touchShardFile() {\n    String shardStatusPath = System.getenv(\"TEST_SHARD_STATUS_FILE\");\n    File shardFile = (shardStatusPath == null ? null : new File(shardStatusPath));\n    touchShardFile(shardFile);\n  }\n\n  // VisibleForTesting\n  static void touchShardFile(File shardFile) {\n    if (shardFile != null) {\n      try {\n        if (!shardFile.createNewFile() && !shardFile.setLastModified(System.currentTimeMillis())) {\n          throw new IOException(\"Unable to update modification time of \" + shardFile);\n        }\n      } catch (IOException e) {\n        throw new RuntimeException(\"Error writing shard file \" + shardFile, e);\n      }\n    }\n  }\n\n  /**\n   * Returns the test sharding strategy optionally specified by the JVM flag\n   * {@link #TEST_SHARDING_STRATEGY}, which maps to the enums in\n   * {@link com.google.testing.junit.runner.sharding.ShardingFilters.ShardingStrategy}.\n   */\n  public String getTestShardingStrategy() {\n    return System.getProperty(TEST_SHARDING_STRATEGY);\n  }\n}\n","sourceCodeStart":63,"sourceCodeEnd":95,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/ShardingEnvironment.java#L63-L95","documentation":"Thrown by ShardingEnvironment.touchShardFile while creating or timestamping the file named by TEST_SHARD_STATUS_FILE. Under sharded bazel test execution each shard must touch this file so Bazel knows the shard started; failure to create the file or update its mtime raises an IOException that is wrapped in a RuntimeException.","triggerScenarios":"closeShardFile() runs after the test with TEST_SHARD_STATUS_FILE set and either createNewFile() fails (unwritable directory, read-only filesystem, disk full) or the file exists but setLastModified() returns false (filesystem refusing mtime updates, e.g. some network/overlay filesystems).","commonSituations":"Sandboxed executions where the shard-status directory is not included in the sandbox writable list; overlayfs/NFS mounts that do not honor utime; running sharded tests under a manually minimal container lacking write access to the runfiles tree.","solutions":["Run the test through bazel test so the harness provides a writable TEST_SHARD_STATUS_FILE path.","If wrapping the runner, point TEST_SHARD_STATUS_FILE at a writable directory (the runner only needs create/touch permission).","For sandboxing issues, add the status-file directory to the sandbox's writable inputs (e.g. --sandbox_writable_path) or disable sandboxing for that test.","Check disk space and filesystem mtime support on the host mounting the output tree."],"exampleFix":"# before\nbazel test --test_sharding_strategy=... //foo  # TEST_SHARD_STATUS_FILE dir not writable\n# after\nbazel test --sandbox_writable_path=/output-baseline/execroot/... //foo","handlingStrategy":"validation","validationCode":"// before running sharded tests\nString shardStatus = System.getenv(\"TEST_SHARD_STATUS_FILE\");\nif (shardStatus != null) {\n  File dir = new File(shardStatus).getAbsoluteFile().getParentFile();\n  if (dir == null || !dir.canWrite()) {\n    throw new IllegalStateException(\"Shard status dir not writable: \" + dir);\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Let bazel test own the TEST_SHARD_STATUS_FILE lifecycle; do not fabricate paths by hand.","Use --sandbox_writable_path for custom shard-status locations.","Avoid overlayfs/NFS mounts that do not honor mtime updates for output trees."],"tags":["bazel","sharding","filesystem","environment"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}