jackwener/OpenCLI · error · ArgumentError

Not a valid file: ${absPath}

Error message

Not a valid file: ${absPath}

What it means

After the extension check, validateImagePaths stats each resolved path with fs.statSync(..., { throwIfNoEntry: false }); if the file does not exist or is not a regular file (directory, symlink to nothing, device), an ArgumentError with the absolute path is thrown.

Source

Thrown at clis/weibo/publish.js:65

    if (t.length > 2000) throw new ArgumentError('weibo publish text exceeds 2000 characters');
    return t;
}

function validateImagePaths(raw) {
    if (!raw) return [];
    const paths = raw.split(',').map(s => s.trim()).filter(Boolean);
    if (paths.length > MAX_IMAGES) {
        throw new ArgumentError(`Too many images: ${paths.length} (max ${MAX_IMAGES})`);
    }
    return paths.map(p => {
        const absPath = path.resolve(p);
        const ext = path.extname(absPath).toLowerCase();
        if (!SUPPORTED_EXTENSIONS.has(ext)) {
            throw new ArgumentError(`Unsupported image format "${ext}". Supported: jpg, png, gif, webp`);
        }
        const stat = fs.statSync(absPath, { throwIfNoEntry: false });
        if (!stat || !stat.isFile()) {
            throw new ArgumentError(`Not a valid file: ${absPath}`);
        }
        return absPath;
    });
}

cli({
    site: 'weibo',
    name: 'publish',
    access: 'write',
    description: 'Publish a new Weibo post immediately',
    domain: 'weibo.com',
    strategy: Strategy.UI,
    browser: true,
    args: [
        {
            name: 'text',
            type: 'string',
            required: true,

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Verify each path exists and is a file (`ls -l` / fs.statSync) before invoking
  2. Use absolute paths to avoid cwd-dependent resolution
  3. Fix typos and remove directories or dead symlinks from the list

Example fix

// before
--images ./shots
// after
--images /abs/path/shots/shot1.png,/abs/path/shots/shot2.png
Defensive patterns

Strategy: validation

Validate before calling

const fs = require('fs');
for (const p of paths) {
  const abs = require('path').resolve(p);
  const st = fs.statSync(abs, { throwIfNoEntry: false });
  if (!st || !st.isFile()) throw new Error(`not a file: ${abs}`);
}

Try / catch

try {
  await cli.run(['weibo', 'publish', '--text', text, '--images', images]);
} catch (err) {
  if (/Not a valid file/.test(err.message)) {
    console.error('Missing path or directory passed:', err.message);
  } else throw err;
}

Prevention

When it happens

Trigger: --images entries pointing to missing files, directories, or broken symlinks; typos in paths; relative paths resolving against an unexpected working directory.

Common situations: Running the CLI from a different cwd than the script assumed; cleanup jobs deleting temp screenshots before publish; passing a directory of images instead of individual files.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/0addc105f7da27a7. Report an issue: GitHub.