Skip to content

Incorrect file resolution for paths higher than CWD #1259

Closed
@aliaksandr-yermalayeu

Description

@aliaksandr-yermalayeu

Env:

Windows 7 x64, NodeJs v10.15.0, eslint 5.11.1, eslint-plugin-import: 2.14.0

Repro steps:

Rule no-unresolved is turned on with case sensitive option

"rules": {
        "import/no-unresolved": ["error", { caseSensitive: true}]
 }

Project root is "B/"
CWD is "C/"

Project Structure (tree)

├─ A
   ├─ B
   │  ├─ C
   │  │  ├─ D.js
import D from 'B/C/D.js'; // should be no error (casing is preserved correctly)
import D from 'B/C/d.js'; // causes error (filename is misspelled)
import D from 'B/c/D.js'; // causes error (cwd is misspelled)

Expected result:

import D from 'b/C/D.js'; // should cause error (project root is misspelled)

Actual result:

import D from 'b/C/D.js'; // no error (project root is misspelled)

Description:

Eslint task is launched by grunt, where grunt config file is located in "C/" folder (CWD). While actual project folder is one ore more levels higher than cwd ("B/" ). So imports with such path don't cause error.

It is caused by "fileExistsWithCaseSync" function
https://github.com/benmosher/eslint-plugin-import/blob/1cd82eb27df85768fbd076e4ff6b7f36d6f652ce/utils/resolve.js#L38
So it basically doesn't go any higher than cwd to cache path.

Suggestion:
It either should not be limited at all (because it is relatively cheap operation as it caches previous paths),

import D from 'a/B/C/D.js'; // should also cause error (it is higher than project root)

or at least it should be limited to project dir (something like getBaseDir() is doing - line 153)

import D from 'b/C/D.js'; // should cause error (project root is misspelled)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions