BrianHenryIE / strauss

Prefix PHP namespaces and classnames to allow multiple versions of libraries to exist without conflict.
https://brianhenryie.github.io/strauss/
MIT License
143 stars 23 forks source link

Invalid relative path used on Windows #94

Closed szaleq closed 7 months ago

szaleq commented 7 months ago

Strauss creates invalid "relative" file paths on Windows, which are actually absolute. It causes an error: Unable to read file from location: (...), because it prepends the so called relative path with an absolute project path, creating paths which contain the absolute project base path twice.

Steps to reproduce

  1. Clone this repo on Windows: https://github.com/szaleq/strauss-example
  2. run composer install
  3. run composer prefix-namespaces
  4. see the error:
    Unable to read file from location: I:/strauss-example/vendor/react/promise/src/functions_include.php. file_get_contents(I:\strauss-example\I:/strauss-example/vendor/react/promise/src/functions_include.php): Failed to open stream: No such file or directory

    image

The solution

Here there is a line, which creates "relative" source file path using $vendorDir, which is an absolute path (at least on Windows - IDK how it behaves in other environments). Instead the $workingDir could be subtracted from the $sourceAbsolutePath to create a relative path (which actually works):

$sourceRelativePath = str_replace($this->workingDir, '', $sourceAbsolutePath);
BrianHenryIE commented 7 months ago

Yeah, $vendorDir = $config->getVendorDirectory() is a relative path on my Mac, i.e. just vendor/, meant to be relative from the project root.

I'm aware it doesn't work on Windows, see #30 for possible cause/solution.

Unfortunately I don't have or use a Windows computer myself, so the motivation to resolve this has been low. (i.e. I fix easier to fix problems first!)

I did add Windows to the GitHub Actions CI text matrix recently: https://github.com/BrianHenryIE/strauss/commit/457b53c719879a6f978227bb5f33396c9bba8dc4, so I do intend to address this, and your issue might help quite a bit.

It looks like something is going wrong here: https://github.com/BrianHenryIE/strauss/blob/f015905edefe603c0841a4bec088c1b8fdaecafc/src/Composer/ProjectComposerPackage.php#L49

BrianHenryIE commented 7 months ago

That should work now. Please test dev-master and let me know. https://github.com/BrianHenryIE/strauss/commit/148caba0c02b3e1b45d4f7bead15464c78458303

szaleq commented 7 months ago

@BrianHenryIE Thaks for the quick action. Unfortunately, it still does not work, now with the following error:

Unable to read file from location: vendor/src/I:/strauss-example/vendor/brianhenryie/strauss/src/Autoload.php. file_get_contents(I:\strauss-example\vendor/src/I:/strauss-example/vendor/brianhenryie/strauss/src/Autoload.php): Failed to open stream: No such file or directory

image

I've updated the Strauss version to dev-master in the repo linked above.