fix: normalize directory separators in route file paths for Windows#10210
fix: normalize directory separators in route file paths for Windows#10210jalexiscv wants to merge 1 commit into
Conversation
Fixes codeigniter4#7474 On Windows, realpath() returns paths with backslashes (e.g., D:\path\to\file.php) while routeFiles paths use forward slashes (App/Config/Routes.php). This caused in_array() comparison to fail in loadRoutes(), preventing route files from being recognized as already loaded and causing 404 override tests to fail. The fix normalizes all realpath() results to use forward slashes, making path comparisons consistent across platforms. Ref: codeigniter4#7474
|
Hi there, jalexiscv! 👋 Thank you for sending this PR! We expect the following in all Pull Requests (PRs).
Important We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works. If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md Sincerely, the mergeable bot 🤖 |
neznaika0
left a comment
There was a problem hiding this comment.
To support Windows, you need a comprehensive solution, starting with initialization index.php . Then all paths can be processed with a single normalize_path() function. For tests, you can temporarily replace slashes during assertion.
paulbalandan
left a comment
There was a problem hiding this comment.
This is an incomplete fix. As I remember, I have proposed a similar normalize_path() helper function in the past. You can read on that for the history. In order to fix the issue, we would need:
- A centralised function to do the normalization (e.g.
normalize_path()) - Adapt our testing workflows to run on windows (
runs-on: windows-latest) - A documentation write up
Description
Fixes #7474 - [4.4] Failed tests on Windows
Problem
On Windows,
realpath()returns paths with backslashes (e.g.,D:\path\to\file.php) whilerouteFilespaths use forward slashes (App/Config/Routes.php). This causedin_array()comparison inloadRoutes()to fail, preventing route files from being recognized as already loaded.This led to tests like
testRun404Overridefailing because the 404 override routes from test support files weren't properly recognized.Solution
Normalize all
realpath()results in RouteCollection to use forward slashes by replacing\with/. Applied in two locations:loadRoutes()- when normalizing the routeFiles arrayloadRoutes()- when normalizing the passed routesFile parameterThis makes path comparisons consistent across Windows and Unix platforms.
Changes
One file:
system/Router/RouteCollection.phploadRoutes(): normalizerealpath()results withstr_replace('\\', '/', ...)Ref: #7474
Closes #7474