phalcon/cphalcon · error · Phalcon\Assets\Exceptions\AssetSourceTargetCollision

Asset '{path}' has the same source and target paths

Error message

Asset '{path}' has the same source and target paths

What it means

For a filtered local asset the manager refuses to operate when the resolved target path equals the source path. Writing the filtered result would overwrite the original file with its own minified copy (irreversible data loss), so AssetSourceTargetCollision is thrown as a safety stop.

Source

Thrown at phalcon/Assets/Manager.zep:521

                /**
                 * Get the target path, we need to write the filtered content to
                 * a file
                 */
                let targetPath = asset->getRealTargetPath(completeTargetPath);

                /**
                 * We need a valid final target path
                 */
                if (true === empty(targetPath)) {
                    throw new InvalidAssetTargetPath(asset->getPath());
                }

                if (true === asset->isLocal()) {
                    /**
                     * Make sure the target path is not the same source path
                     */
                    if (targetPath === sourcePath) {
                        throw new AssetSourceTargetCollision(targetPath);
                    }

                    if (true === this->phpFileExists(targetPath)) {
                        if (filemtime(targetPath) !== filemtime(sourcePath)) {
                            let filterNeeded = true;
                        }
                    } else {
                        let filterNeeded = true;
                    }
                }
            } else {
                /**
                 * If there are no filters, just print/buffer the HTML
                 */
                let prefixedPath = this->calculatePrefixedPath(
                    collection,
                    asset->getRealTargetUri(),
                    asset->getRealSourcePath()

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Choose a distinct output filename: `$asset->setTargetPath('css/app.min.css')` (or a separate targetBasePath such as public/build/)
  2. If you truly want in-place replacement, minify outside Phalcon in a build step that works on a copy
  3. Keep generated artifacts out of source directories so this collision cannot occur

Example fix

// before
$collection->addCss('css/app.css')->setTargetPath('css/app.css');
// after
$collection->addCss('css/app.css')->setTargetPath('css/app.min.css');
Defensive patterns

Strategy: validation

Validate before calling

$source = rtrim($sourceBasePath, '/') . '/' . ltrim($asset->getPath(), '/');
$target = rtrim($targetBasePath, '/') . '/' . ltrim($asset->getTargetPath(), '/');
if ($source === $target) {
    throw new RuntimeException('Asset target would overwrite its own source: ' . $target);
}
echo $assets->outputCss($name);

Try / catch

try {
    echo $assets->outputCss('app');
} catch (\Phalcon\Assets\Exceptions\AssetSourceTargetCollision $e) {
    $logger->error('Refusing to overwrite asset source; set a distinct targetPath: ' . $e->getMessage());
    throw $e;
}

Prevention

When it happens

Trigger: An asset or collection whose targetPath resolves to exactly the same file as its source, e.g. source 'css/app.css' and target 'css/app.css' (or same base paths), with filters enabled and the asset local.

Common situations: Attempting 'minify in place' during deployment; sourceBasePath and targetBasePath both pointing at public/ with identical relative paths; copy-pasted targetPath values.

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/30989cd3a4d60e0c. Report an issue: GitHub.