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

Windows paths normalized to forward slashes, not platform-native backslashes #419

Closed
ryanblock opened this issue Jul 16, 2020 · 7 comments
Closed

Comments

@ryanblock
Copy link

ryanblock commented Jul 16, 2020

Description of issue

As many folks are aware, much of the world has sandardized on forward slashes as path / folder delimiters (eg /path/to/your/file.txt or https://domain.com/path/to/your/view).

Windows, however, has historically relied on backslashes (eg C:\path\to\your\file.txt).

Node, of course, respects both per-platform:

  • *nix: console.log(process.cwd()) // /path/to/current/folder
  • Win: console.log(process.cwd()) // C:\\path\\to\\current\\folder

When node-glob returns results on Windows, it outputs them with forward slash-normalized paths. This can cause cross-platform compatibility problems.

Example:

let files = glob.sync('*')
let relativePaths = files.map(file => file.replace(path.join(process.cwd(), path.sep)))

In Windows, the above code would not work, as using methods intended to ease cross platform compatibility (like path.sep) break with node-glob's forward-slash normalized output.

Expected behavior

  • Glob would output file paths that are platform-native and that can be used without additional denormalization steps

Repro steps

  • Glob files on Windows; file paths are normalized to forward slashes
  • Compare to process.cwd(), etc. in Windows; file Node presents backslash-normalized file paths
@jakearchibald
Copy link

I ran into this too. Node's path.resolve helps.

@NotWearingPants
Copy link

On version 7.1.7:

glob('/**') //= absolute with backslashes
glob('**') //= relative with forward slashes
glob('**', { realpath: true }) //= absolute with backslashes
glob('**', { absolute: true }) //= absolute with forward slashes

What is going on

It seems that tiny-glob works mostly the same and solves this issue, always returning native separators (unless you use an absolute path glob).

recursive-beast added a commit to recursive-beast/laravel-mix-manifest-paths that referenced this issue Oct 8, 2021
kthompson added a commit to kthompson/react-native-monorepo-tools that referenced this issue Nov 7, 2021
Due to an error in how node-glob returns paths, a specific option is 
required in order to return paths appropriately for each platform.

See: isaacs/node-glob#419
Fixes: mmazzarolo/react-native-universal-monorepo#12
mmazzarolo pushed a commit to mmazzarolo/react-native-monorepo-tools that referenced this issue Nov 7, 2021
Due to an error in how node-glob returns paths, a specific option is 
required in order to return paths appropriately for each platform.

See: isaacs/node-glob#419
Fixes: mmazzarolo/react-native-universal-monorepo#12
@ryanblock
Copy link
Author

I'm pretty sure 8 resolves this by normalizing everything to forward slashes? Unless anyone disagrees, I am so incredibly happy to close this issue.

@isaacs
Copy link
Owner

isaacs commented Aug 3, 2022

Yep, that was the intent. Closing!

@liudonghua123
Copy link

I use filename.replace(/\\/g,'/') like glob.glob("./**/*.ts").then( filenames =>filenames.map(item => item.replace(/\\/g,'/'))).then(files=>console.info(files)).

@isaacs
Copy link
Owner

isaacs commented Jun 19, 2024

You can also use the path-scurry objects which have relativePosix() and fullpathPosix() methods.

const results = (await glob('./**/*.ts', { withFileTypes: true })).map(p => p.relativePosix())

@liudonghua123
Copy link

Thanks, it is a more elegant method for doing this. 😄

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

No branches or pull requests

5 participants