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 inconsistent with other orderings #2678

Closed
davidmorgan opened this issue May 26, 2021 · 6 comments · Fixed by #4538
Closed

directives_ordering inconsistent with other orderings #2678

davidmorgan opened this issue May 26, 2021 · 6 comments · Fixed by #4538
Labels
customer-google3 set-internal Affects a rule used by a team internally at Google (but possibly not in a published ruleset)

Comments

@davidmorgan
Copy link
Contributor

The sort order does not seem to be consistent with analyzer/tidy_dart when there are packages with . in the name; it looks like we need to split the package name out, sort by then, then sort by name within package.

Other suggestions to check:

  import "/src/foo.dart";
  import "./foo.dart";
  import "../foo.dart";  
@pq
Copy link
Member

pq commented Aug 4, 2021

@davidmorgan: are we good on this one now?

@pq pq added the set-internal Affects a rule used by a team internally at Google (but possibly not in a published ruleset) label Aug 4, 2021
@davidmorgan
Copy link
Contributor Author

IIRC, yes :)

@pq
Copy link
Member

pq commented Aug 5, 2021

Super! I'll ride your enthusiasm and close this one; we can re-open if we hit any snags. Thanks!

/fyi @jefflim-google

@pq pq closed this as completed Aug 5, 2021
@oprypin
Copy link
Contributor

oprypin commented Jun 27, 2023

I found another edge case.

What directives_ordering enforces:

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

What I expect & what tidy_dart does:

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

Or alternatively the exact opposite of the latter also might make sense, but not the mixed order.


I think the culprit is the fact that this function is invoked on the uri whether or not it is actually from a package, and so the part before the first slash is sorted separately:

int compareDirectives(String a, String b) {
var indexA = a.indexOf('/');
var indexB = b.indexOf('/');
if (indexA == -1 || indexB == -1) return a.compareTo(b);
var result = a.substring(0, indexA).compareTo(b.substring(0, indexB));
if (result != 0) return result;
return a.substring(indexA + 1).compareTo(b.substring(indexB + 1));
}

I.e. it sorts the items as if they are such tuples:

 ('.', 'foo1.dart')
 ('..', '../foo2.dart')
 ('..', 'foo3.dart')

I can't think of any other scenario where this "tuple-ness" causes wrong orderings to happen, although at first it might seem like a wide class of errors. The ASCII table ordering goes like [., /, then letters]. For letters it's correct anyway but specifically the dots are affected.


And also I guess one big reason that this error has gone unnoticed is that you're just not supposed to write './foo.dart'. Maybe that's another thing that could be separately considered for enforcement - drop ./ if possible :)

@bwilkerson
Copy link
Member

Has this been fixed now?

@oprypin
Copy link
Contributor

oprypin commented Jul 10, 2023

I reopened because it was only mostly fixed.

With that, the status is:

@oprypin oprypin linked a pull request Jul 6, 2023 that will close this issue
Fix directives_ordering for a leading dot in path #4538

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-google3 set-internal Affects a rule used by a team internally at Google (but possibly not in a published ruleset)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants