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

Organise imports places @Timeout in the wrong position #3598

Closed
bsutton opened this issue Oct 6, 2021 · 3 comments
Closed

Organise imports places @Timeout in the wrong position #3598

bsutton opened this issue Oct 6, 2021 · 3 comments
Labels
in editor Relates to code editing or language features in lsp/analysis server Something to be fixed in the Dart analysis server is bug
Milestone

Comments

@bsutton
Copy link

bsutton commented Oct 6, 2021

If you are using the test package you are able to add an @timeout annotation to the top of a library to indicate a longer than default timeout for the tests.

The @timeout annotation MUST be at the top of the file.

If you have the @timeout annotation and run 'Organise Imports' the @timeout annotation often gets moved down in amongst the imports and in some cases is completely deleted.

This causes an error an you have to manually move the annotation back to the top of the file or worse still removes it all together and if it is deleted you may not even realise it has been deleted.

Delete Example
In this example 'dart:io' is an unused package. Running organise imports removes dart:io and also @timeout

@Timeout(Duration(minutes: 20))
import 'dart:io';
import 'package:dcli/dcli.dart' hide equals;
import 'package:nginx_le_container/src/util/acquisition_manager.dart';
import 'package:nginx_le_shared/nginx_le_shared.dart';
import 'package:test/test.dart';

After organize imports:

import 'package:dcli/dcli.dart' hide equals;
import 'package:nginx_le_container/src/util/acquisition_manager.dart';
import 'package:nginx_le_shared/nginx_le_shared.dart';
import 'package:test/test.dart';

Move example
The 'dart:io' package is in the wrong place

@Timeout(Duration(minutes: 20))

import 'package:dcli/dcli.dart' hide equals;
import 'package:nginx_le_container/src/util/acquisition_manager.dart';
import 'package:nginx_le_shared/nginx_le_shared.dart';
import 'package:test/test.dart';
import 'dart:io';
import 'mock_cerbot_paths.dart';

In this scenario the @timeout annotation ends up:

import 'dart:io';

@Timeout(Duration(minutes: 20))
import 'package:dcli/dcli.dart' hide equals;
import 'package:nginx_le_container/src/util/acquisition_manager.dart';
import 'package:nginx_le_shared/nginx_le_shared.dart';
import 'package:test/test.dart';

import 'mock_cerbot_paths.dart';

Not exactly a critical bug, but annoying.

@bsutton bsutton added the is bug label Oct 6, 2021
@DanTup
Copy link
Member

DanTup commented Oct 7, 2021

@bwilkerson the Timeout class is decorated with @Target(TargetKind.library) which is documented as:

  /// Indicates that an annotation is valid on the first directive in a library,
  /// whether that's a `library`, `import`, `export` or `part` directive. This
  /// doesn't include the `part of` directive in a part file.
  library,

Would this be the best way to determine whether the annotation should be anchored to the top?

I'm assuming it's not possible to have a single unused import (that would leave nowhere for the annotation to go), as you'd need to always either by importing the file containing the annotation, or if the annotation is in the current file, importing the meta package to be able to decorate it with Target?

@DanTup DanTup added in editor Relates to code editing or language features in lsp/analysis server Something to be fixed in the Dart analysis server labels Oct 7, 2021
@DanTup DanTup added this to the On Deck milestone Oct 7, 2021
@bwilkerson
Copy link

Would this be the best way to determine whether the annotation should be anchored to the top?

I can't think of any better way.

I'm assuming it's not possible to have a single unused import ...

Not a single unused import that's annotated with something from meta. You could certainly have an unused import that's annotated with an annotation that's defined locally:

@local
import 'dart:math';

const local = '';

In that case the annotation won't have a Target associated with it. Not sure whether it's better to remove the annotation along with the last directive or whether it's better to leave it attached to the first declaration (assuming there is one). I doubt it will come up often, though.

@DanTup DanTup modified the milestones: On Deck, v3.29.0 Nov 2, 2021
DanTup added a commit to DanTup/sdk that referenced this issue Nov 3, 2021
copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue Nov 9, 2021
… organizing imports

+ handle comments between the last annotation and the directive code.

Fixes Dart-Code/Dart-Code#3598.

Change-Id: I868c832c8fcb2bf67aab83400ef2e57a5f14ce45
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/219783
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
@DanTup
Copy link
Member

DanTup commented Nov 9, 2021

This should be fixed with dart-lang/sdk@445ac5d. It's a change in the SDK rather than Dart-Code so will show up with an SDK release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in editor Relates to code editing or language features in lsp/analysis server Something to be fixed in the Dart analysis server is bug
Projects
None yet
Development

No branches or pull requests

3 participants