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

Use path.relative instead of path.posix.relative for relative paths #299

Closed
wants to merge 1 commit into from

Conversation

birtles
Copy link
Contributor

@birtles birtles commented Oct 16, 2023

On Windows calling:

path.posix.relative('C:/Users/Brian/10ten-ja-reader', '.')

will produce ../../../...

I believe what it does in this case is run path.posix.resolve() on both arguments producing /Users/Brian/C:/Users/Brian/10ten-ja-reader and /Users/Brian and then determines the relative between the two giving us ../../../...

If we use path.relative instead, which does not try to resolve both paths to posix paths first, we get the expected result, an empty string:

If from and to each resolve to the same path (after calling
path.resolve() on each), a zero-length string is returned.

(Source: https://nodejs.org/api/path.html#pathrelativefrom-to)

We'll still probably want to convert the result of that to a posix path using our toPosix helper, in order to be consistent with how we handle paths elsewhere, otherwise we'll get results with backslashes in them.

I've so far only observed an incorrect result coming about from the '.' input and that only from the Playwright plugin so an alternative fix might be to get the Playwright plugin to stop producing relative paths of the form '.' but I suspect this could show up in other places.

On Windows calling:

```js
path.posix.relative('C:/Users/Brian/10ten-ja-reader', '.')
```

will produce `../../../..`.

I believe what it does in this case is run `path.posix.resolve()` on
both arguments producing `/Users/Brian/C:/Users/Brian/10ten-ja-reader`
and `/Users/Brian` and then determines the relative between the two
giving us `../../../..`.

If we use `path.relative` instead, which does not try to resolve both
paths to posix paths first, we get the expected result, an empty string:

> If `from` and `to` each resolve to the same path (after calling
> `path.resolve()` on each), a zero-length string is returned.

(Source: https://nodejs.org/api/path.html#pathrelativefrom-to)

We'll still probably want to convert the result of _that_ to a posix
path using our `toPosix` helper, in order to be consistent with how we
handle paths elsewhere, otherwise we'll get results with backslashes in
them.

I've so far only observed an incorrect result coming about from the
`'.'` input and that only from the Playwright plugin so an alternative
fix might be to get the Playwright plugin to stop producing relative
paths of the form `'.'` but I suspect this could show up in other
places.
@birtles
Copy link
Contributor Author

birtles commented Oct 16, 2023

Never mind, I saw you just fixed this in birchill/10ten-ja-reader#1359 (comment) 😅

Thank you!

@webpro
Copy link
Collaborator

webpro commented Oct 16, 2023

Sorry, beat you by a minute I'm afraid :/

Thanks anyway :)

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