rectorphp/rector · error · InvalidConfigurationException

Invalid memory limit format "%s".

Error message

Invalid memory limit format "%s".

What it means

MemoryLimiter::validateMemoryLimitFormat() enforces VALID_MEMORY_LIMIT_REGEX '#^-?\d+[kMG]?$#i' before touching ini_set: an optional minus, digits, then at most one single-letter unit k/M/G (case-insensitive) or none (bytes). Anything else — '512MB', '1 g', 'unlimited', '2G5' — throws InvalidConfigurationException('Invalid memory limit format "%s".') and the ini_set is never attempted.

Source

Thrown at src/Util/MemoryLimiter.php:39

        $memoryLimit = $configuration->getMemoryLimit();
        if ($memoryLimit === null) {
            return;
        }
        $this->validateMemoryLimitFormat($memoryLimit);
        $memorySetResult = ini_set('memory_limit', $memoryLimit);
        if ($memorySetResult === \false) {
            $errorMessage = sprintf('Memory limit "%s" cannot be set.', $memoryLimit);
            throw new InvalidConfigurationException($errorMessage);
        }
    }
    private function validateMemoryLimitFormat(string $memoryLimit): void
    {
        $memoryLimitFormatMatch = Strings::match($memoryLimit, self::VALID_MEMORY_LIMIT_REGEX);
        if ($memoryLimitFormatMatch !== null) {
            return;
        }
        $errorMessage = sprintf('Invalid memory limit format "%s".', $memoryLimit);
        throw new InvalidConfigurationException($errorMessage);
    }
}

View on GitHub (pinned to 408fcb0ff1)

Solutions

  1. Use the compact format: 512M, 1G, 1024K, plain bytes like 268435456, or -1 for unlimited
  2. Strip trailing 'B' and whitespace from sourced values: $limit = rtrim(strtoupper(trim($limit)), 'B')
  3. Add a pre-flight regex check in your wrapper script so the run fails before Rector boots

Example fix

# before
vendor/bin/rector process src --memory-limit=512MB

# after
vendor/bin/rector process src --memory-limit=512M
Defensive patterns

Strategy: validation

Validate before calling

// validate the same shape Rector enforces, before launching
$limit = strtoupper(trim((string) ($argv[2] ?? '')));
if (preg_match('#^-?\d+[KMG]?$#', $limit) !== 1) {
    fwrite(STDERR, "Invalid memory limit format; use e.g. 512M, 1G, or -1\n");
    exit(1);
}
shell_exec('vendor/bin/rector process src --memory-limit=' . escapeshellarg($limit));

Type guard

function isValidMemoryLimit(string $limit): bool
{
    return preg_match('#^-?\d+[kMG]?$#i', $limit) === 1;
}

Prevention

When it happens

Trigger: Passing --memory-limit=512MB (double-letter unit), --memory-limit=2048M with a stray space, or a value read from an env var containing a typo; also 'max'/'default' style words.

Common situations: Copying values from memory_limit=512MB php.ini syntax (where 'B' suffix is legal) into the CLI flag where it is not; env-var templating that appends units conditionally.

Related errors


AI-assisted analysis of rectorphp/rector@408fcb0ff1 (2026-08-21). Data as JSON: /api/errors/a36eb92618a789ae. Report an issue: GitHub.