Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid paths on Windows #100

Merged
merged 4 commits into from
Apr 26, 2024

Conversation

szaleq
Copy link

@szaleq szaleq commented Apr 25, 2024

Fixes #94

The problem

Not all paths on Windows contain backslash (\) as a directory separator. Some paths (e.g. vendor dir returned from Composer) still contain trailing forward slash (/). Thats why some code such as here does not work.
It produces a path which ends with /\ combo. It wouldn't be a problem in a require call etc, because multiple slashes get resolved correctly, so paths like C:\path\\to/\file.php get resolved as C:\path\to\file.php and it would work BUT here there is a str_replace call which aims to subtract the $sourcePath from $sourceAbsoluteFilepath to create a relative path and fails to do so due to the slash-backslash combo presence at the end of the $sourcePath.

The solution

The solution is simple: just replace all occurences of one or more slash or backslash combinations with DIRECTORY_SEPARATOR to create a normalized path, just like this one does.

Another issue about paths

IDK how it works on MacOS or Linux, but on Windows each path of this if-else condition fails, so the $this->relativePath is always null for all packages. It seems like the $vendorDirectory is something entirely different on different platforms... (??) For me it contains an absolute path of each package's vendor directory, e.g. C:\project-root\vendor\brianhenrieie\strauss\vendor...

The solution is to just use a package name as a relative path if anything else failed. On Windows it works like a charm, I also confirmed that it didn't break anything on MacOS. I didn't delve into this much, I don't know all the possible configuration options of composer etc... but couldn't it be used as a relative package name by default? I mean, each package has a name, which has a form of vendor-name/package-name and it gets installed into {project-root}/{composer-vendor-dir}/vendor-name/package-name... Isn't it always the case?

Comment on lines +126 to +128
} elseif (0 !== strpos($absolutePath, getcwd()) && 1 === preg_match('/.*[\/\\\\]([^\/\\\\]*[\/\\\\][^\/\\\\]*)[\/\\\\][^\/\\\\]*/', $vendorDirectory, $output_array)) {
$this->relativePath = $output_array[1];
} elseif (1 === preg_match('/.*\/([^\/]*\/[^\/]*)\/composer.json/', $composerJsonFileAbsolute, $output_array)) {
// Not every package gets installed to a folder matching its name (crewlabs/unsplash).
} elseif (1 === preg_match('/.*[\/\\\\]([^\/\\\\]+[\/\\\\][^\/\\\\]+)[\/\\\\]composer.json/', $composerJsonFileAbsolute, $output_array)) {
Copy link
Author

Choose a reason for hiding this comment

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

@BrianHenryIE Only now I realised why the relative paths were not created correctly: the regexes only worked for forward slash paths. Here is the fix to make it work with backslash paths also.

@BrianHenryIE BrianHenryIE merged commit 7df991f into BrianHenryIE:master Apr 26, 2024
6 of 11 checks 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.

Invalid relative path used on Windows
2 participants