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

[import/order] 2.20.1 Windows only: Resolver cannot distinguish external and internal imports #1655

Closed
warnellw opened this issue Feb 13, 2020 · 9 comments · Fixed by #1651
Closed

Comments

@warnellw
Copy link

warnellw commented Feb 13, 2020

Hi there,

When using import/order in conjunction with the import/no-unresolved rule, it doesn't appear possible to configure a resolver to distinguish between external (i.e. node_modules) and internal/absolute imports (i.e. src). In every case, either the resolver groups the external and absolute imports together, or, the resolver is unable to resolve one of the groups.

The following configuration is able to automatically add a newline between the external and absolute imports, but is unable to resolve the external imports:

"eslintConfig": {
    "extends": [
      "eslint:recommended",
      "react-app",
      "plugin:import/recommended",
      "plugin:import/react"
    ],
    "rules": {
      "import/order": [
        "error",
        {
          "groups": [
            "builtin",
            "external",
            "internal",
            "parent",
            "sibling",
            "index"
          ],
          "newlines-between": "always"
        }
      ]
    },
    "settings": {
      "import/external-module-folders": [
        "node_modules"
      ],
      "import/resolver": {
        "node": {
          "moduleDirectory": [
            "src"
          ],
          "extensions": [
            ".jsx",
            ".js"
          ]
        }
      }
    }
  }

In order to get the external imports to resolve, adding node_modules to the module_directory works, but then external and absolute internal imports are grouped together.

I've tried additional configurations using the webpack resolver as well, but am running into the same problem. Any help would be greatly appreciated!

@constgen
Copy link

Happens to me when I upgraded to the latest version. Was not the case before. Moved from 2.18.0 to 2.20.1

@warnellw
Copy link
Author

@constgen Thanks for your reply!

After a bit of bisecting it appears that in this particular case, the bug only affects Windows on v2.20.1. I tested both MacOS and Ubuntu and both were working properly on v2.20.1. So for now, I have rolled back to v2.20.0 for Windows compatibility.

Here is the latest eslint configuration I'm using that works with Windows on v2.20.0, but not v2.20.1:

"eslintConfig": {
    "extends": [
      "eslint:recommended",
      "react-app",
      "plugin:import/recommended",
      "plugin:import/react"
    ],
    "rules": {
      "no-console": "warn",
      "import/order": [
        "error",
        {
          "groups": [
            "builtin",
            "external",
            "internal",
            "parent",
            "sibling",
            "index"
          ],
          "newlines-between": "always"
        }
      ]
    },
    "settings": {
      "import/resolver": {
        "webpack": {
          "config": {
            "resolve": {
              "extensions": [
                ".js",
                ".jsx"
              ],
              "modules": [
                "src",
                "node_modules"
              ]
            }
          }
        }
      }
    }
  }

@warnellw warnellw changed the title [import/order] Resolver cannot distinguish external and internal imports [import/order] Windows only: Resolver cannot distinguish external and internal imports Feb 17, 2020
@warnellw warnellw changed the title [import/order] Windows only: Resolver cannot distinguish external and internal imports [import/order] 2.20.1 Windows only: Resolver cannot distinguish external and internal imports Feb 17, 2020
@ljharb
Copy link
Member

ljharb commented Feb 17, 2020

@warnellw can you perhaps try on windows on latest master on this plugin?

@warnellw
Copy link
Author

@ljharb Yes, #1651 seems to have fixed it - thanks!

@bduff9
Copy link

bduff9 commented Mar 12, 2020

Running into this same issue on Windows, any timeline on when this fix will be released?

@paztis
Copy link

paztis commented Mar 23, 2020

hi

First the PR is fixed but still not published on NPM. Is it possible to publish it to npm ?
Secondly, in case of monorepo, the external resolving is not working at all, as modules are symlinked and are not under node_modules folder.
In this case, it doens't work at all.

I don't understand how your trying to detect if a module is external withot knowing what is the path of the module we work on.

in case we know it, a path.relative(from, to) is enough to know if we are in or out of our module.
But we need the package.json path extra info to be in all your importType management, that we don't have at all.

I can work on it but it will generate lot of changes

@ljharb
Copy link
Member

ljharb commented Mar 24, 2020

Yes, it will be published to npm with the next release. That will happen when I have time to do it, and not before.

For the other thing, can you file a new issue to discuss what you need and why?

@paztis
Copy link

paztis commented Mar 24, 2020

Sure

I've create a PR here to fix all these problems #1696
Do I need to create a new defect or we can talk over the PR itself ?
Globally I replace the external detection part thanks to the resolved path, because before the resolving, hte import name has no real value.
Everything works correctly now, window / unix, monorepo, aliases, ...

Tell me if you can look at it

@ljharb
Copy link
Member

ljharb commented Mar 24, 2020

The PR is fine, I'll take a look at it today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

5 participants