alibaba/Sentinel · error · IllegalArgumentException

File can't be null or a directory

Error message

File can't be null or a directory

What it means

Thrown by the FileRefreshableDataSource constructor when the supplied java.io.File is null or points to a directory. FileRefreshableDataSource polls a regular file for Sentinel rule/config content, so a directory or missing File object is an invalid data source target. The check runs after the bufSize check but before any field assignment, so the data source is never partially constructed.

Source

Thrown at sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileRefreshableDataSource.java:84

    public FileRefreshableDataSource(File file, Converter<String, T> configParser, int bufSize)
        throws FileNotFoundException {
        this(file, configParser, DEFAULT_REFRESH_MS, bufSize, DEFAULT_CHAR_SET);
    }

    public FileRefreshableDataSource(File file, Converter<String, T> configParser, Charset charset)
        throws FileNotFoundException {
        this(file, configParser, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, charset);
    }

    public FileRefreshableDataSource(File file, Converter<String, T> configParser, long recommendRefreshMs, int bufSize,
                                     Charset charset) throws FileNotFoundException {
        super(configParser, recommendRefreshMs);
        if (bufSize <= 0 || bufSize > MAX_SIZE) {
            throw new IllegalArgumentException("bufSize must between (0, " + MAX_SIZE + "], but " + bufSize + " get");
        }
        if (file == null || file.isDirectory()) {
            throw new IllegalArgumentException("File can't be null or a directory");
        }
        if (charset == null) {
            throw new IllegalArgumentException("charset can't be null");
        }
        this.buf = new byte[bufSize];
        this.file = file;
        this.charset = charset;
        // If the file does not exist, the last modified will be 0.
        this.lastModified = file.lastModified();
        firstLoad();
    }

    private void firstLoad() {
        try {
            T newValue = loadConfig();
            getProperty().updateValue(newValue);
        } catch (Throwable e) {
            RecordLog.info("loadConfig exception", e);

View on GitHub (pinned to a3f40ba8e9)

Solutions

  1. Pass the path of the actual rule FILE, not its parent directory (e.g. /data/sentinel/flow-rule.json, not /data/sentinel/).
  2. If the path comes from configuration, log and validate it before constructing the File so a null/blank value fails with your own clear message.
  3. If you need to load several files, create one FileRefreshableDataSource per file.
  4. Wrap construction in try/catch IllegalArgumentException to fail fast with context (which property/variable supplied the bad path).

Example fix

// before
File dir = new File(System.getProperty("sentinel.rules.dir"));
new FileRefreshableDataSource<>(dir, parser, charset);

// after
File file = new File(Objects.requireNonNull(
    System.getProperty("sentinel.rules.file"), "sentinel.rules.file must be set"));
new FileRefreshableDataSource<>(file, parser, charset);
Defensive patterns

Strategy: validation

Validate before calling

// before constructing
if (file == null || file.isDirectory()) {
    throw new IllegalArgumentException("rule file path must point to a file: " + path);
}
new FileRefreshableDataSource<>(file, parser, charset);

Try / catch

try {
    dataSource = new FileRefreshableDataSource<>(file, parser, charset);
} catch (IllegalArgumentException e) {
    throw new IllegalStateException("Invalid FileRefreshableDataSource config for path: " + path, e);
}

Prevention

When it happens

Trigger: Calling new FileRefreshableDataSource(file, parser, charset) (or the 5-arg overload) with file == null, or with a File whose isDirectory() returns true (e.g. passing a config directory instead of the rules file itself).

Common situations: Pointing the data source at a directory like /etc/sentinel/ instead of /etc/sentinel/flow-rules.json; building the File from a misconfigured system property or environment variable that resolves to null/empty and the code does new File(nullValue) or passes null; copy-pasting an example where the path is a folder containing multiple rule files.

Related errors


AI-assisted analysis of alibaba/Sentinel@a3f40ba8e9 (2026-08-14). Data as JSON: /api/errors/540abd7bcf961dbc. Report an issue: GitHub.