Fix containsPath for paths that share a prefix#2509
Conversation
koesie10
left a comment
There was a problem hiding this comment.
Removing the platform argument makes sense to me, perhaps it made more sense when we didn't call into the path module to normalize paths.
| // On windows, if the two paths are in different drives, then the | ||
| // relative path will be an absolute path to the other drive. | ||
| !(process.platform === "win32" && isAbsolute(relativePath)) |
There was a problem hiding this comment.
On other platforms, receiving an absolute path back seems like it would also mean that parent does not contain child. What would prevent us from removing the check for process.platform here and simplifying this if-condition?
There was a problem hiding this comment.
I wasn't very sure, but I think we could remove the check for process.platform and it wouldn't change behaviour.
I agree if we get back an absolute path then that's a bit weird and likely means that parent does not contain child. I can't think of a reason it would happen outside of windows, so I included the process.platform check as a safety to limit the extra behaviour to just that OS.
I'll take it out. Let me know what you think?
…gnull/contains_pathns_path
For context, in #2490 I started added my own method to determine if one paths contains another, but then I realised that we already have
files.tsandcontainsPath.Unfortunately
containsPathuses astartsWithimplementation and this isn't correct for examples like/fooand/foobar. In this case neither path is a parent of the other, but they do share/fooas a prefix.This PR changes the implementation to use
path.relativeto do the work of deciding if a path contains another. We can also now remove the call tonormalizePathfrom this method, asrelativeperforms the same normalization as theresolvemethod. The only catch is we have to handle different drives on windows. See https://github.com/dsp-testing/robertbrignull-path-test/actions/runs/5244140271/jobs/9469730588 for some testing of this behaviour on linux vs windows.I also made the methods a little clearer in my opinion, with more descriptive parameter names, documenting behaviour more closely, and removing the
platformargument. We never actually pass aplatformargument that isn'tprocess.platform, and from my understanding it would be incorrect to ever do so because the behaviour of functions frompathchanges. It looks like we do in the tests, but actually the tests are skipped on mismatching platforms.Changes are split up into separate commits to tell the renames from the new code and behaviour changes.
Checklist
ready-for-doc-reviewlabel there.