Skip to content

docs: explain why path.posix.normalize does not replace windows slashes #12298

Description

@JestDotty
  • Version: 4.4.2
  • Platform: Windows 8.1 64 bit
  • Subsystem: node.js native path module

console.log('1', path.posix.normalize("\\some\\thing\\like\\this"))
console.log('2', path.posix.normalize("/some/thing/like/this"))
console.log('3', path.win32.normalize("\\some\\thing\\like\\this"))
console.log('4', path.win32.normalize("/some/thing/like/this"))

output:
image

1 output should be the same as 2s though
Docs need to explain why this is the expected output

Activity

  1. added
    pathIssues and PRs related to the path subsystem.
    on Apr 10, 2017
  2. vsemozhetbyt commented on Apr 10, 2017

    @vsemozhetbyt
    Contributor

    If I get this right, win32 methods can process both separators, while posix methods don't consider \ a valid separator. Compare how other win32 methods parse paths with / properly, while the posix methods do not this for \:

    const path = require('path');
    
    const win32ToPosix = '\\win32\\to\\posix';
    const posixToWin32 = '/posix/to/win32';
    
    console.log(`
      ${path.posix.basename(win32ToPosix)}
      ${path.win32.basename(posixToWin32)}
    
      ${path.posix.dirname(win32ToPosix)}
      ${path.win32.dirname(posixToWin32)}
    
      ${path.posix.normalize(win32ToPosix)}
      ${path.win32.normalize(posixToWin32)}
    
      ${JSON.stringify(path.posix.parse(win32ToPosix))}
      ${JSON.stringify(path.win32.parse(posixToWin32))}
    
      ${path.posix.resolve(win32ToPosix)}
      ${path.win32.resolve(posixToWin32)}
    `);
    c:\>node test.js
    
      \win32\to\posix
      win32
    
      .
      /posix/to
    
      \win32\to\posix
      \posix\to\win32
    
      {"root":"","dir":"","base":"\\win32\\to\\posix","ext":"","name":"\\win32\\to\\posix"}
      {"root":"/","dir":"/posix/to","base":"win32","ext":"","name":"win32"}
    
      c:\/\win32\to\posix
      c:\posix\to\win32
    
    c:\>
    
  3. bnoordhuis commented on Apr 10, 2017

    @bnoordhuis
    Member

    Answered. Closing, working as intended.

  4. JestDotty commented on Apr 10, 2017

    @JestDotty
    Author

    the docs say:

    When multiple, sequential path segment separation characters are found (e.g. / on POSIX and \ on Windows), they are replaced by a single instance of the platform specific path segment separator. Trailing separators are preserved.

    which is a little weird so I skimmed it with my understanding, but for postarity this this doesn't work either:

    console.log('1', path.posix.normalize("\\\\some\\\\thing\\\\like\\\\this"))
    console.log('2', path.posix.normalize("/\\some/\\thing/\\like/\\this"))

    image

    To be fair it is vague enough to have concluded to my understanding... but on a more conceptual/abstract/module angle a conversion looking like this is intended / not available in a path manipulation library or am I missing something?

    path.normalize(file).split(/[\\\/]/g).join(path.posix.sep)
  5. addaleax commented on Apr 10, 2017

    @addaleax
    Member

    @Seudein On POSIX, \\ is not a path separator, so I think path.posix.normalize is right in that /\ shouldn’t get contracted into a single /… what would your expected output for that be?

  6. refack commented on Apr 10, 2017

    @refack
    Contributor

    Maybe we need a posix2windows / windows2posix methods

  7. addaleax commented on Apr 10, 2017

    @addaleax
    Member

    @refack Sorry, could you explain how those methods would behave?

  8. refack commented on Apr 10, 2017

    @refack
    Contributor

    @refack Sorry, could you explain how those methods would behave?

    posix2windows would assume the input is a posix path and flip the /
    (then do some voodoo to try and find on which drive the file exists image [or not])

    windows2posix would assume that the input is windows and make sure all \ are turned into /

    I think what @Seudein assumed was that using path.posix.* on a windows platform will act like windows2posix


    Answered. Closing, working as intended.

    @bnoordhuis I believe that if a user open an issue based on wrong assumptions that are not addressed in the docs, it should be treated as a bug in the docs.

  9. added
    docIssues and PRs related to Node.js documentation.
    and removed
    pathIssues and PRs related to the path subsystem.
    on Apr 10, 2017
  10. changed the title [-]path.posix.normalize does not replace windows slashes[/-] [+]docs: explain why path.posix.normalize does not replace windows slashes[/+] on Apr 10, 2017
  11. added
    good first issueIssues that are suitable for first-time contributors.
    on Apr 10, 2017
  12. reopened this on Apr 10, 2017
  13. refack commented on Apr 10, 2017

    @refack
    Contributor

    Reopened as a doc bug

  14. addaleax commented on Apr 10, 2017

    @addaleax
    Member

    then do some voodoo to try and find on which drive the file exists

    path methods generally don’t actually query the file system, I would like to keep it that way.

  15. refack commented on Apr 10, 2017

    @refack
    Contributor

    I would like to keep it that way.

    I agree. Super important!
    I was sarcastic, I was hoping the huge emoji and [or not] would convey that, sorry...

  16. added a commit that references this issue on May 7, 2017
  17. added a commit that references this issue on May 19, 2017
  18. added a commit that references this issue on Jun 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.good first issueIssues that are suitable for first-time contributors.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions