Skip to content

Fix custom PHP binary deleted when binary path is the app root#117

Open
simonhamp wants to merge 2 commits into
mainfrom
fix-custom-php-binary-prune
Open

Fix custom PHP binary deleted when binary path is the app root#117
simonhamp wants to merge 2 commits into
mainfrom
fix-custom-php-binary-prune

Conversation

@simonhamp
Copy link
Copy Markdown
Member

Summary

Fixes #115.

When NATIVEPHP_PHP_BINARY_PATH is set to a value that points at the project root (e.g. ./), native:build deletes the entire built app right before electron-builder packages it.

Root cause

In PrunesVendorDirectory::pruneVendorDirectory(), the configured binary package directory was interpolated naively into "app/{$binaryPackageDirectory}". When the value normalizes to a no-op (./, ., /, or a trailing /.), Path::join('/build', 'app/./') collapses to /build/app, so the prune step removes the whole app directory. The previous ! empty() guard did not catch this because strings like ./ are non-empty.

native:run was unaffected because it does not run the prune step.

Fix

Trim the configured path of surrounding dots, slashes, and whitespace, then skip removal entirely when nothing meaningful remains:

$binaryPackageDirectory = trim($this->binaryPackageDirectory(), "./\\ \t\n\r\0\x0B");
if ($binaryPackageDirectory !== '' && $filesystem->exists($this->buildPath("app/{$binaryPackageDirectory}"))) {
    $filesystem->remove($this->buildPath("app/{$binaryPackageDirectory}"));
}

Tests

Added tests/Build/PruneVendorDirectoryTest.php:

Verified the regression test fails against the old code and passes with the fix. Full suite (114 tests) passes and Pint is clean.

🤖 Generated with Claude Code

simonhamp and others added 2 commits May 29, 2026 15:38
Trim the configured binary package path before pruning so values that
normalize to a no-op (./, ., /, trailing /.) no longer collapse to the
app root and delete the entire built app.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@simonhamp
Copy link
Copy Markdown
Member Author

@bmckay959 would you mind trying this fix out when you get a chance?

@simonhamp simonhamp marked this pull request as ready for review May 29, 2026 07:00
| Tests
|--------------------------------------------------------------------------
*/
it('removes the custom php binary package directory', function () use ($buildPath, $command) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused by this test....why do we want the custom binary removed? Feels like this test is introducing the bug the opposite direction? Basically, we don't want to delete the directory where the NATIVEPHP_PHP_BINARY points....or am I missing something?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understandable confusion. The binary is moved to a location in the app bundle itself during build. So leaving it in the source directory would include it twice 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom PHP binary gets deleted if in root of project

3 participants