Skip to content

require(".\\") doesn't resolve index.js on Windows #18299

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

Closed
jdalton opened this issue Jan 22, 2018 · 14 comments
Closed

require(".\\") doesn't resolve index.js on Windows #18299

jdalton opened this issue Jan 22, 2018 · 14 comments
Labels
module Issues and PRs related to the module subsystem. windows Issues and PRs related to the Windows platform.

Comments

@jdalton
Copy link
Member

jdalton commented Jan 22, 2018

While looking at #15015 (comment) I noticed that the trailing slash check in _findPath was only keying off of a forward slash and not the backslash that Windows allows.

You can repro this by simply doing the following in a directory with an index.js

require(".\\") // throws
require("./") // will find the index.js

Other places in Node account for the backslash in Windows paths so this looks like an oversight.

Update:

It looks like if it's a two dot relative path to an index.js then it does work.

require("..\\") // work
require("..\\.") // works
require("../") // works
require("../.") // works

Also this

require("./..") // works
require(".\\..") // throws

Notes:

It looks like path.resolve handles these cases fine so it can be excluded from the problem.

path.resolve(".\\") // 'C:\\projects\\bar\\foo'
path.resolve(".\\..") // 'C:\\projects\\bar'
@jdalton
Copy link
Member Author

jdalton commented Jan 22, 2018

It looks like part of the issue is in Module._resolveLookupPaths because it's only looking for a forward slash

request.charCodeAt(1) !== 47/*/*/)) {

Module._resolveLookupPaths("./", null)
[ './',
  [ '.',
    'C:\\projects\\bar\\foo\\node_modules',
    'C:\\projects\\bar\\node_modules',
    'C:\\projects\\node_modules',
    'C:\\node_modules',
    'C:\\Users\\jdalton\\.node_modules',
    'C:\\Users\\jdalton\\.node_libraries',
    'C:\\Program Files\\nodejs\\lib\\node' ] ]

but is snipped:

> Module._resolveLookupPaths(".\\", null)
[ '.\\',
  [ 'C:\\Users\\jdalton\\.node_modules',
    'C:\\Users\\jdalton\\.node_libraries',
    'C:\\Program Files\\nodejs\\lib\\node' ] ]

⚠️ Once Module._resolveLookupPaths is fixed though it'll run into issues with #15015.

@bmeck
Copy link
Member

bmeck commented Jan 24, 2018

@jdalton I think we could probably have windows normalize \\ to / in all cases [like the URL spec does], is there a case where \\ doesn't act like / in the existing usage of paths on win32?

joachimvh added a commit to LinkedSoftwareDependencies/Components.js that referenced this issue May 30, 2018
This is required to get around a node.js bug when paths start with './'
nodejs/node#18299
rubensworks pushed a commit to LinkedSoftwareDependencies/Components.js that referenced this issue May 30, 2018
This is required to get around a node.js bug in Windows when paths start with './'
nodejs/node#18299
@Trott
Copy link
Member

Trott commented Jul 20, 2019

@jdalton I think we could probably have windows normalize \\ to / in all cases [like the URL spec does], is there a case where \\ doesn't act like / in the existing usage of paths on win32?

I'm curious about the answer to @bmeck's question here, and also if this is still an issue?

@Trott
Copy link
Member

Trott commented Jul 20, 2019

@jdalton ^^^^

@targos targos added module Issues and PRs related to the module subsystem. windows Issues and PRs related to the Windows platform. labels Dec 11, 2019
@Trott
Copy link
Member

Trott commented Apr 22, 2020

@nodejs/modules-active-members @jdalton Should this be closed? Or is this an issue that should be addressed?

@jasnell
Copy link
Member

jasnell commented Jun 25, 2020

There's been no further action on this. Closing, but given that it's not fully resolved, I'm putting this on the Futures project board so that it does not get lost.

@jasnell jasnell closed this as completed Jun 25, 2020
@ljharb
Copy link
Member

ljharb commented Jun 25, 2020

@jasnell why would an inactive issue that's still a problem be closed?

@jasnell
Copy link
Member

jasnell commented Jun 25, 2020

An unresolved issue that no one ever looks at isn't useful either. These can always be reopened if someone intends to pick it up. Also, after I'm done taking a triage pass at all these stale old issues I'll be compiling a list of outstanding issues for each subsystem

@ljharb
Copy link
Member

ljharb commented Jun 25, 2020

How can anyone ever decide to pick it up if it's closed?

@jasnell jasnell reopened this Jun 25, 2020
@jasnell
Copy link
Member

jasnell commented Jun 26, 2020

I've reopened but as I said, "I'm putting this on the Futures project board so that it does not get lost." and "after I'm done taking a triage pass at all these stale old issues I'll be compiling a list of outstanding issues for each subsystem" ...

@pd4d10
Copy link
Contributor

pd4d10 commented May 4, 2021

Can confirm this issue has been fixed with all these paths:

require(".\\") // throws
require("./") // will find the index.js
require("..\\") // work
require("..\\.") // works
require("../") // works
require("../.") // works
require("./..") // works
require(".\\..") // throws

Tested versions:
v12.22.1
v14.16.1
v15.14.0
v16.0.0

@Trott
Copy link
Member

Trott commented May 4, 2021

As this appears to be fixed on all supported versions, I'm going to close it. Of course, if that's wrong and it's still an issue somewhere, please comment or re-open.

@Trott Trott closed this as completed May 4, 2021
@Trott
Copy link
Member

Trott commented May 4, 2021

Oh, wait, no, there's still one problem left, right? require(".\\..") // throws

@Trott Trott reopened this May 4, 2021
@pd4d10
Copy link
Contributor

pd4d10 commented May 4, 2021

Nope, it's just quoted from the original post.

All cases do not throw errors

@Trott Trott closed this as completed May 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module Issues and PRs related to the module subsystem. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

7 participants