Skip to content

@docImports should support as imports #56527

Closed

Description

@goderbauer

Something like this should work:

/// @docImport 'dart:io' as io;
library;

/// You can use [io.Stdin] to...
void foo() {
  // intentionally left blank
}

Activity

  1. dart-github-bot commented on Aug 20, 2024

    @dart-github-bot
    Collaborator

    Summary: The @docImport annotation currently doesn't support the as keyword for aliasing imported libraries, making it difficult to reference imported symbols in documentation comments. This issue proposes adding support for as imports within @docImport annotations.

  2. added
    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).
    triage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.
    type-enhancementA request for a change that isn't a bug
    on Aug 20, 2024
  3. added
    devexp-serverIssues related to some aspect of the analysis server
    P2A bug or feature request we're likely to work on
    and removed
    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).
    on Aug 20, 2024
  4. removed
    triage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.
    on Aug 22, 2024
  5. added
    area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.
    and removed on Feb 21, 2025
  6. szakarias commented on Sep 29, 2026

    @szakarias
    Contributor

    This issue is central to the removal of dartdoc's universal scope (dart-lang/dartdoc#4283). Once dartdoc transitions to resolving doc comment references strictly through the analyzer (relying on the language's scope rules), references to library names will stop linking, as they currently rely on dartdoc's universal lookup fallback.

    I strongly agree with @srawlins' proposal in #56528 to reference a library through its import prefix. It is the cleanest solution because it aligns with how the language already works: an import prefix is just a name in the library scope. This means the comment resolver wouldn't need a special case for library names. It also doesn't depend on library foo; names. Like deferred imports, this would require the import prefix to be unique.

    I am currently fixing unresolved doc comment references in Flutter so that the comment_references lint can be enabled (flutter/flutter#192870). On current main, all 24 remaining reports (in 12 files) need exactly this: library references and prefix-qualified references. Both of these groups are fixed by supporting as on @docImport, so resolving this issue would cover #56528 as well.

    Regarding performance: @srawlins mentioned in dart-lang/dartdoc#3762 that an earlier attempt at supporting prefixes in @docImport caused a performance regression. @scheglov, could you take a look at whether there's a way to support this without that cost? CC @johnniwinther.

  7. scheglov commented on Sep 29, 2026

    @scheglov
    Contributor

    Should @docImport support full enhanced parts like functionality?
    Or just just @docImport inside the library file, and all parts use it in documentation comments?

  8. FMorschel commented on Sep 29, 2026

    @FMorschel
    Contributor
  9. self-assigned this
    on Sep 29, 2026
  10. szakarias commented on Sep 30, 2026

    @szakarias
    Contributor

    @scheglov Thanks for looking into this! I think we can start with just @docImport inside the library file, with all parts using it in documentation comments. That covers the Flutter cases. Scoping doc imports per part file, like imports under enhanced parts, could be added later without breaking existing doc comments, since the library file's doc imports would remain visible in all parts.

    Side note: while looking into this, I noticed that @docImports on a part of directive are currently parsed but silently ignored, without any diagnostic, even for a URI that doesn't exist. I'll file a separate issue for that.

  11. bwilkerson commented on Sep 30, 2026

    @bwilkerson
    Member

    I still think that it could be confusing to users for docImports to behave differently that normal imports and that it would be better to just add that support with the parts-with-imports feature. If the behavior of 'Add import' and 'Add doc import' differ it will be harder for users to remember what they do.

    I don't know whether generated code usually includes comments (though if public APIs are being generated I certainly hope so). If it does, then we'll want code generators to be able to add local docImports.

  12. scheglov commented on Sep 30, 2026

    @scheglov
    Contributor

    I will implement @docImport in parts too, so with enhanced parts support.

  13. bwilkerson commented on Sep 30, 2026

    @bwilkerson
    Member

    Does that mean that docImport in parts will be tied to the parts-with-imports feature? (I think it's fine either way, just curious.)

  14. scheglov commented on Sep 30, 2026

    @scheglov
    Contributor

    https://dart-review.googlesource.com/c/sdk/+/557680

    @bwilkerson Yes. @docImports on a part of directive are only used with the enhanced-parts (parts-with-imports) feature, and are then scoped like imports in parts: a part sees the doc imports of its enclosing files, can shadow them, and a doc import prefix in a part extends the enclosing prefix with the same name. Lookups still try the normal scope, including imports, first.

    Without the feature, @docImports on part of are reported with a new doc_import_in_part_file warning, instead of being silently ignored as before, and only library @docImports are considered.

  15. bwilkerson commented on Sep 30, 2026

    @bwilkerson
    Member

    Thanks!

  16. added a commit that references this issue on Oct 1, 2026
  17. scheglov commented on Oct 1, 2026

    @scheglov
    Contributor
  18. szakarias commented on Oct 2, 2026

    @szakarias
    Contributor

    @scheglov This is great, thanks for the quick turnaround!

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

Metadata

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work onarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-serverIssues related to some aspect of the analysis servertype-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions