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

[sort/imports] Bug: relative paths more than one layer sorted out of order #62

Closed
1 task
abannachGrafana opened this issue Mar 15, 2024 · 6 comments · Fixed by #64
Closed
1 task

[sort/imports] Bug: relative paths more than one layer sorted out of order #62

abannachGrafana opened this issue Mar 15, 2024 · 6 comments · Fixed by #64
Labels

Comments

@abannachGrafana
Copy link

Description

Relative paths greater than one layer deep e.g. ../../some/path imports are not sorted with other relative path imports.

Example config

"sort/imports": [
      "error",
      {
        "groups": [
          { "regex": "^react$", "order": 10 },
          { "type": "dependency", "order": 15 },
          { "type": "side-effect", "order": 20 },
          { "regex": "^@grafana", "order": 30 },
          { "regex": "^.+\\.s?css$", "order": 50 },
          { "type": "other", "order": 40 }
        ],
        "separator": "\n"
      }
    ],

Example sorted imports

import currentRules from '../../jest/data/current-rules.json'; // outside of source dir where alias does not work
import exemptions from '../../jest/data/exemptions.json';
import labels from '../../jest/data/labels.json';
import metrics from '../../jest/data/metrics.json';
import recommendations from '../../jest/data/recommendations.json';
import recommendationsConfig from '../../jest/data/recommendations-config.json';
import recommendationsVerbose from '../../jest/data/recommendations-verbose.json';
import { rest } from 'msw'; // normal dependency

import { paths } from '@/util/constants'; // path alias

Example sorted imports with single layer relative

import currentRules from '../../jest/data/current-rules.json';
import exemptions from '../../jest/data/exemptions.json';
import labels from '../../jest/data/labels.json';
import metrics from '../../jest/data/metrics.json';
import recommendations from '../../jest/data/recommendations.json';
import recommendationsVerbose from '../../jest/data/recommendations-verbose.json';
import { rest } from 'msw';

import recommendationsConfig from '../jest/data/recommendations-config.json';
import { paths } from '@/util/constants';

Expected behavior

  • Multi layer relative imports are sorted along with single layer relative imports
@mskelton
Copy link
Owner

@abannachGrafana I tried reproducing this but couldn't. Could you setup a codesandbox that reproduces it?

@abannachGrafana
Copy link
Author

I'll see what I can do 👍

@abannachGrafana
Copy link
Author

Whoop! Was able to recreate with a simple example and multiple file locations
https://codesandbox.io/p/github/abannachGrafana/eslint-sort-repro/draft/agitated-rosalind

@abannachGrafana
Copy link
Author

Also I just created a public repo in GitHub that recreates so you could just check it out if you want https://github.com/abannachGrafana/eslint-sort-repro

@mskelton
Copy link
Owner

@abannachGrafana So, this happens in instances where you have an import with a relative path that is resolvable by the isomorphic-resolve package in node_modules. In your reproduction repo, this happened with ../../jest... which was resolvable since isomorphic-resolve was 2 levels deep and thus could resolve the relative path, and incorrectly assumed it was a dependency. The solution here is to detect relative imports and always flag those as non-dependencies before attempting to call require.resolve.

Copy link

🎉 This issue has been resolved in version 3.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

2 participants