Skip to content

Fixes to path handling on Windows#2568

Merged
dantleech merged 1 commit intophpactor:masterfrom
MatmaRex:windows-paths
Feb 27, 2024
Merged

Fixes to path handling on Windows#2568
dantleech merged 1 commit intophpactor:masterfrom
MatmaRex:windows-paths

Conversation

@MatmaRex
Copy link
Contributor

Paths don't start with '/', so use helpers for absolute/relative path, and may use both '/' and '\', so canonicalize before comparing them.

This resolves 26 test failures on Windows.

Paths don't start with '/', so use helpers for absolute/relative path,
and may use both '/' and '\', so canonicalize before comparing them.

This resolves 26 test failures on Windows.
$path = '/'.$path;
if (!Path::isAbsolute($path)) {
// Not sure if this makes sense… Maybe it should just throw?
$path = Path::makeAbsolute($path, '/');
Copy link
Collaborator

Choose a reason for hiding this comment

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

i think the idea is that this is always absolute, so fromParts(['home','daniel',])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To elaborate, there are two things that confuse me:

  • Where does data in this format come from? I'm not aware of any filesystem APIs that return paths like that, and I've never seen anyone use that format in their configuration. I also couldn't find what in your code generates them. I'm like 70% sure that this code path is unused apart from tests.
  • How is this supposed to work with Windows paths, where absolute paths must have a drive letter (e.g. "C:") in addition to the list of path segments? The way I made it work here, this function actually returns a drive-relative path (e.g. "\Users\daniel"), not an absolute one ("C:\Users\daniel"), and the drive letter will be resolved against the current working directory when trying to read from the path. This will probably often work, but it sometimes won't.

Copy link
Collaborator

Choose a reason for hiding this comment

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

it's used here:

https://github.com/phpactor/phpactor/blob/master/lib/Filesystem/Adapter/Simple/SimpleFilesystem.php

i guess it won't work for Windows... but feel free to throw an exception here, if the tests pass then it's all good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, just this one method?

    public function createPath(string $path): FilePath
    {
        if (Path::isRelative($path)) {
            return FilePath::fromParts([$this->path->path(), $path]);
        }

        ...
    }

If $this->path->path() is an absolute path, and it looks like it should be, then that will be fine.

@dantleech dantleech merged commit 230b16a into phpactor:master Feb 27, 2024
@dantleech
Copy link
Collaborator

nice, thanks

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.

2 participants