Skip to content

Safely load api routes only when routes file exists - #6

Merged
shanerbaner82 merged 1 commit into
NativePHP:mainfrom
prateekbhujel:fix/safe_api_routing
Sep 20, 2026
Merged

shanerbaner82 merged 1 commit into
NativePHP:mainfrom
prateekbhujel:fix/safe_api_routing

Conversation

@prateekbhujel

Copy link
Copy Markdown
Contributor

Fixes #5

When bundling mobile applications with NativePHP, cleanup_exclude_files removes routes/api.php to keep mobile builds lean. However, bootstrap/app.php unconditionally required api: DIR . '/../routes/api.php', causing native:run on iOS and Android to fail during boot with: Failed to open stream: No such file or directory.

This checks file_exists before passing the path so web and local development loads API routes normally, while mobile bundles safely skip missing api routes without crashing.

Added Pest tests covering application boot both when routes/api.php exists and when it is missing.

@coderabbitai

coderabbitai Bot commented Sep 16, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: cd6cd004-2857-4ae8-be01-2e41c08ee764

📥 Commits

Reviewing files that changed from the base of the PR and between a39fd6b and 60c59e8.

📒 Files selected for processing (2)
  • bootstrap/app.php
  • tests/Feature/ApiRoutingTest.php

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


Walkthrough

The application now checks whether routes/api.php exists before registering API routes. When the file is absent, the API route path is set to null. Feature tests cover bootstrapping without the file and verify that /api/user returns HTTP 401 when the file exists.

Suggested reviewers: shanerbaner82

Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 60c59

The missing API route file no longer prevents application boot, while normal API routing remains enabled when the file exists.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: it conditionally loads API routes only when the routes file exists.
Description check ✅ Passed The description explains the missing routes/api.php failure, the conditional loading fix, and the added tests. It directly relates to the changeset.
Linked Issues check ✅ Passed Issue #5 requires application boot to avoid the missing routes/api.php error. bootstrap/app.php now passes the API route path to withRouting only when file_exists returns true. The added Pest …
Out of Scope Changes check ✅ Passed The reviewed changes are limited to conditional API route registration and focused feature tests for the missing-file and existing-file cases. These changes directly support Issue #5.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…

Warning

Some tools did not complete. Review the errors below.

🔧 PHPStan (2.2.9)

PHP Parse error: syntax error, unexpected token "->" in /vendor/phpunit/phpunit/src/Runner/Version.php on line 48
Parse error: syntax error, unexpected token "->" in /vendor/phpunit/phpunit/src/Runner/Version.php on line 48


Comment @coderabbitai help to get the list of available commands.

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 16, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@shanerbaner82

Copy link
Copy Markdown
Contributor

Thanks for this. The fix is right, and it's hitting everyone who runs native:run on a fresh kit (#5 is the same bug). I ran into it myself today.

One thing before I merge: could you drop the first test in tests/Feature/ApiRoutingTest.php?

It renames the real routes/api.php out of the way and restores it in a finally. If the process dies before that block runs (fatal, kill -9, an interrupted run) the file is left behind as routes/api.php.test_backup in the working tree. It also boots a second Application inside the running suite, which re-swaps facade roots and container statics and can bleed into whatever test runs next.

The /api/user → 401 test is good, keep that one.

If you want to cover the real failure, the useful assertion is about the bundle rather than the framework: nothing in config('nativephp.cleanup_exclude_files') should be referenced unconditionally at boot. routes/ai.php is already safe (laravel/mcp does its own file_exists) and app/Providers/Filament is gated on NATIVEPHP_RUNNING in bootstrap/providers.php, so routes/api.php was the only gap. Not a blocker though, happy to merge as soon as the first test is gone.

@shanerbaner82
shanerbaner82 merged commit 8fe1e30 into NativePHP:main Sep 20, 2026
1 check passed
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.

api.php not found after running native:run

2 participants