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

directives_ordering unexpectedly changed #4869

Closed
yangsfang opened this issue Feb 6, 2024 · 7 comments
Closed

directives_ordering unexpectedly changed #4869

yangsfang opened this issue Feb 6, 2024 · 7 comments

Comments

@yangsfang
Copy link

Describe the issue
The linter for the following seems to have broken by #4538

import '/foo1.dart';
import '../foo2.dart';
import 'foo3.dart';

is being flagged, and changed to

import '../foo2.dart';
import '/foo1.dart';
import 'foo3.dart';

To Reproduce
Create a dart file with the above import. Run dart lint <file>.

Expected behavior
Expected /foo1.dart to be at the top as it is an absolute path. '../' and './' are all relative paths. This was the behavior before the PR was merged.

import '/foo1.dart';
import '../foo2.dart';
import 'foo3.dart';

Additional context
#4538 was fixing #2678 which is unrelated to the issue above. The example given in #4538 had only relative paths. The proposed fix should not have affected absolute path ordering.

import './foo1.dart';
import '../../foo2.dart';
import '../foo3.dart';
import 'foo4.dart';
@srawlins
Copy link
Member

srawlins commented Feb 7, 2024

CC @oprypin

copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue Feb 7, 2024
Basically the code change just falls back to the behavior prior to commit aed089e45c35221ce2b82f3757132031f0344b8b when a leading slash is encountered.

Fixes dart-lang/linter#4869

Change-Id: I61fc7915f44f043f591ea439d40d4016fc8b3d27
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350861
Commit-Queue: Oleh Prypin <[email protected]>
Reviewed-by: Samuel Rawlins <[email protected]>
@lrhn
Copy link
Member

lrhn commented Feb 8, 2024

Should we use some kind of logical ordering instead of accidental lexicographical ordering?
Fx. order by distance to current directory, and even normalizing the URIs to avoid any . path segments or non-leading .. segments?

The example

import './foo1.dart';
import '../../foo2.dart';
import '../foo3.dart';
import 'foo4.dart';

shows './foo1.dart' which is equivalent to 'foo1.dart'. We should normalize that so it's sorted next to 'foo4.dart'.

If there are no leading or embedded .s, then the rest are actually ordered nicely because ../../ goes before ../, which means the directories com in "closer to root first" order (and with rooted paths going before relative paths, that feels right).

Generally, it should sort by ordering by path segment, rather than treating / as just a character (if it doesn't already, haven't checked. 😊)

@bwilkerson
Copy link
Member

We are currently enforcing the guidelines in the style guide (https://dart.dev/effective-dart/style#ordering). If we want to update the style guide then we'll update the lint (and sort command) to correspond. Assuming that it has the right definition of "alphabetical", I believe that this is currently working as intended (modulo the bug that was introduced).

@munificent For visibility.

@srawlins Did you recently review a CL with a fix for this bug?

@srawlins
Copy link
Member

srawlins commented Feb 8, 2024

Yes, "Fixes XYZ" does not work cross-repos, so every linter bug must be closed manually. ☹️

Fixed by dart-lang/sdk@9fdb0ca

@srawlins srawlins closed this as completed Feb 8, 2024
@yangsfang
Copy link
Author

yangsfang commented Feb 9, 2024

We are currently enforcing the guidelines in the style guide (https://dart.dev/effective-dart/style#ordering). If we want to update the style guide then we'll update the lint (and sort command) to correspond. Assuming that it has the right definition of "alphabetical", I believe that this is currently working as intended (modulo the bug that was introduced).

@munificent For visibility.

@srawlins Did you recently review a CL with a fix for this bug?

Well. I do not believe this reason is proper for closing the issue. Here is a quote from Effective dart that was linked:

DO place dart: imports before other imports

This issue does not deal with that. We are dealing with "other imports" exclusively.

DO place package: imports before relative imports

This issue does not deal with that. We are dealing with "relative imports" (relative to its own package) exclusively.

DO specify exports in a separate section after all imports

This issue does not deal with exports.

DO sort sections alphabetically

Emphasis added. Slashes are not included in the (English) alphabet. The example also implies that the author meant the English alphabet.

Effective dart did not specify whether the ordering should be strictly lexicographical according to the ASCII alphabet, which would be honestly quite arbitrary.

At the very least, please update effective Dart to document such ordering as intended. (e.g. change "alphabetical" to "lexicographical")


Should we use some kind of logical ordering instead of accidental lexicographical ordering? Fx. order by distance to current directory, and even normalizing the URIs to avoid any . path segments or non-leading .. segments?

I would definitely support this idea. Google's style guide for C/C++ does have a list of ordering it from farthest to closest to the current file: https://google.github.io/styleguide/cppguide.html#Names_and_Order_of_Includes

@yangsfang
Copy link
Author

Never mind, silly me. I thought the issue was closed as "WAI", but it is in fact fixed. Please ignore my previous comment.

Thanks for the fix.

@munificent
Copy link
Member

Should we use some kind of logical ordering instead of accidental lexicographical ordering?

Eh, probably not worth the effort. Lexicographical is close enough to logical order except in the case where users are pointlessly doing ./ which they can just... not do. We aren't Node and you don't need Node-style import paths in Dart. :)

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