bazelbuild/bazel · error · RuntimeException
Could not create custom sharding strategy class ${strategy}
Error message
Could not create custom sharding strategy class ${strategy} What it means
Thrown by ShardingFilters.getShardingFilterFactory when the system property test.sharding.strategy is neither a value of the ShardingStrategy enum (compared case-insensitively) nor a loadable class name. The code first tries valueOf(), then falls back to reflection: load the class via the context classloader, narrow it to ShardingFilterFactory, and instantiate via a no-arg constructor. Any ReflectiveOperationException in that fallback is wrapped in this RuntimeException.
Source
Thrown at src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/ShardingFilters.java:104
shardingEnvironment.getTotalShards());
}
private ShardingFilterFactory getShardingFilterFactory() {
String strategy = shardingEnvironment.getTestShardingStrategy();
if (strategy == null) {
return defaultShardingStrategy;
}
ShardingFilterFactory shardingFilterFactory;
try {
shardingFilterFactory = ShardingStrategy.valueOf(strategy.toUpperCase(Locale.ENGLISH));
} catch (IllegalArgumentException e) {
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class<? extends ShardingFilterFactory> strategyClass =
classLoader.loadClass(strategy).asSubclass(ShardingFilterFactory.class);
shardingFilterFactory = strategyClass.getConstructor().newInstance();
} catch (ReflectiveOperationException | IllegalArgumentException e2) {
throw new RuntimeException(
"Could not create custom sharding strategy class " + strategy, e2);
}
}
return shardingFilterFactory;
}
}
View on GitHub (pinned to e6e199d060)
Solutions
- Use a built-in enum value of ShardingStrategy (e.g. DEFAULT) spelled exactly, case-insensitively.
- For a custom factory: ensure the class is on the test runtime classpath (deps of the java_test / runner config).
- Verify the class is public, implements com.google.testing.junit.runner.sharding.ShardingFilterFactory, and has a public no-arg constructor that does not throw.
- Read the suppressed cause (e2) from the stack trace: ClassNotFoundException vs ClassCastException vs InvocationTargetException tells you which link broke.
Example fix
// before
public final class MySharding implements ShardingFilterFactory {
MySharding(ShardIndex idx) {...} // package-private, no no-arg ctor
}
// after
public final class MySharding implements ShardingFilterFactory {
public MySharding() {} // public no-arg constructor required
} Defensive patterns
Strategy: try-catch
Validate before calling
// verify a custom strategy before setting the property
String strategy = "com.mycorp.MyShardingFactory";
if (!ShardingFilters.ShardingStrategy.VALID_NAMES.contains(strategy.toUpperCase(Locale.ROOT))) {
Class<?> c = Class.forName(strategy); // ClassNotFoundException surfaces early
if (!ShardingFilterFactory.class.isAssignableFrom(c)
|| c.getConstructor() == null) {
throw new IllegalArgumentException("Bad sharding strategy: " + strategy);
}
} Type guard
static boolean isValidShardingStrategy(String s) {
try {
ShardingFilters.ShardingStrategy.valueOf(s.toUpperCase(Locale.ENGLISH));
return true;
} catch (IllegalArgumentException e) {
try {
return ShardingFilterFactory.class
.isAssignableFrom(Thread.currentThread().getContextClassLoader().loadClass(s));
} catch (ClassNotFoundException cnf) { return false; }
}
} Try / catch
try {
ShardingFilters.createShardingFilterFactory(...);
} catch (RuntimeException e) {
Throwable cause = e.getCause(); // ReflectiveOperationException tells which step failed
log.config("Invalid test.sharding.strategy, falling back to DEFAULT: " + cause);
} Prevention
- Keep custom ShardingFilterFactory implementations public with a public no-arg constructor.
- Add the factory jar as a runtime dep of every test that uses the strategy.
- Pin the strategy property in a checked-in bazelrc so typos surface at config review.
When it happens
Trigger: Setting -Dtest.sharding.strategy=custom.factory (or a typo like "defualt") where the class is missing from the test classpath, does not implement ShardingFilterFactory, is not public, lacks a public no-arg constructor, or its constructor throws.
Common situations: Teams experimenting with custom sharding plug-ins who forget to add the factory jar to the test's deps; typos in the strategy property; refactoring that renamed or removed the factory class without updating the flag.
Related errors
- Error writing shard file ${shardFile}
- Unable to find the type of annotation %s.
- Could not get config: {stderr}
- Unsupported JUnit Runner API version ${JUNIT_API_VERSION_PRO
- No filter expression specified after ${arg}
AI-assisted analysis of bazelbuild/bazel@e6e199d060 (2026-08-14).
Data as JSON: /api/errors/4485368943b1c839.
Report an issue: GitHub.