language icon indicating copy to clipboard operation
language copied to clipboard

Allow library declarations with no name.

Open lrhn opened this issue 5 years ago • 14 comments

Dart has library declarations of the form library some.name; which are no longer commonly used. The name used to be required for having part files, but is now not necessary. Giving a library a name is only really useful for avoiding duplicate imports with different paths, so most libraries do not use the declaration.

However, it's still useful to have a library declaration for a number of reasons:

  • Adding annotations/metadata to the library.
  • Writing documentation for the library.

The latter is "fixed" by the dartdoc tool assuming that a dartdoc occurring before the first import is really documenting the library, and not the import. (Edit: No, it actually doesn't. The culprit is the @TestOn annotation.) The former is not fixed, so when you want to annotate your library with metadata, you need to give it an otherwise completely useless name.

The obvious solution would be to allow a declaration of the form library;, which gives the library the empty name (same as no library declaration). It's just a hook to hang your annotations from.

lrhn avatar Jul 07 '20 15:07 lrhn

FYI: there is special handling in pkg:test for this as well. Would be great to have this feature!

kevmoo avatar Jul 30 '20 17:07 kevmoo

However, it's still useful to have a library declaration for a number of reasons:

  • Adding annotations/metadata to the library.
  • Writing documentation for the library.

The latter is "fixed" by the dartdoc tool assuming that a dartdoc occurring before the first import is really documenting the library, and not the import.

AFAIK this is not the case, see dart-lang/dartdoc#1082.

jcollins-g avatar Dec 09 '20 17:12 jcollins-g

It does indeed seem like that feature never made it into DartDoc. All the more reason to introduce the library; declaration.

lrhn avatar Dec 10 '20 10:12 lrhn

library name is useful for mirror, such as: await currentMirrorSystem().isolate.loadUri(Uri.parse('./testaa.dart')); await currentMirrorSystem().isolate.loadUri(Uri.parse('./testbb.dart'));

but testaa's library name is aa and testbb's library name is aa, the code will throw exception: currentMirrorSystem().findLibrary(#aa);

Silentdoer avatar Apr 28 '21 09:04 Silentdoer

@Silentdoer Not sure what point you are making. You can add names to libraries if you want to, and you can choose not to if you want to. The one thing you can't currently do is not have a library name and have metadata or documentation on the library. That's what this feature request tries to address.

People choose not to give libraries names for a reason.

Letting libraries define their own name means that you risk conflicts. Dart disallows such conflicts, but there is no way to resolve them other than to not include one of the libraries in the program. Generally in Dart, a declaration with a name is included in a scope which limits that name's reach (and risk of conflict). You can't have two top-level declarations in the same library, but a library is also a unit of editing, so the person adding the conflict is also able to resolve it again. You can't import (and use) two declarations with the same name into the same library, but the person doing the import is also the one using the names, and the able to add an import with a prefix if necessary.

The scope of library names is the program. That means that any library name you declare can potentially conflict with any other library. The solution to that is to use structured library names based on the package name and library name, like pkg.mypackage.src.foo_bar for package:mypackage/src/foo_bar.dart. At that point, there really isn't much advantage over just using the URI directly, having a library name is a liability, not an advantage.

The solution to that is to not give libraries names. That's what people are already doing. Whether library names are useful for mirrors or not isn't really important, because you can still choose to give libraries a name if you want to, and most people choose not to do that. This request is to allow those people to still attach metadata (including documentation) to the library.

lrhn avatar Apr 28 '21 09:04 lrhn

Dart disallows such conflicts

We actually removed that error in 14fcda2457501e6712ea6e25b37cbc592b5f6ba2, July 2020. Of course, this means that library names are only useful for code using mirrors, which makes it even more natural to use a bare library; in a library which needs to have metadata (aka annotations) on the library per se, especially on platforms where mirrors are not supported.

eernstg avatar Apr 28 '21 09:04 eernstg

This would be very useful to me right now. I am about to recommend that a customer place a certain @pragma annotation on their many thousands of libraries. But they don't use the library declaration, so they will have to invent a whole system of names.

rakudrama avatar Jul 20 '22 17:07 rakudrama

Just do it!

(My biggest obstacle to having already done this was that it required changes to the analyzer AST model. Parsing is trivial. Libraries having empty names is already possible. Some code will not expect a library with a library declaration to have an empty name, though.)

lrhn avatar Jul 20 '22 19:07 lrhn

Just do it!

Yes please!!!

kevmoo avatar Jul 20 '22 19:07 kevmoo

@bwilkerson @srawlins Is there something in the analyzer blocking this?

jcollins-g avatar Jul 20 '22 20:07 jcollins-g

No, there shouldn't be. We'd be game to implement.

srawlins avatar Jul 20 '22 20:07 srawlins

I believe I've more-or-less implemented this in https://dart-review.googlesource.com/c/sdk/+/257490.

@leafpetersen should I still spell out the feature real quick in a language repo spec?

srawlins avatar Sep 14 '22 18:09 srawlins

It's nice to have something around as a record, so if you don't mind, maybe a 1 paragraph spec would be great? You could land it directly in accepted/future_releases I think?

leafpetersen avatar Sep 14 '22 20:09 leafpetersen

Specification mailed in: https://github.com/dart-lang/language/pull/2490

srawlins avatar Sep 15 '22 04:09 srawlins

@osa1 – weird that YOUR fork of the SDK closed THIS issue. 🤷

kevmoo avatar Sep 23 '22 19:09 kevmoo

Interesting.. This must be a bug in GitHub..

osa1 avatar Sep 23 '22 19:09 osa1

Interesting.. This must be a bug in GitHub..

it's weird. You have access to change this. And it's NICE that we can close issue cross repo. Just broken in this case 🤷

kevmoo avatar Sep 23 '22 19:09 kevmoo

@srawlins – leave it open until we have a plan to ship it in a specific release? (CC @mit-mit @leafpetersen )

kevmoo avatar Sep 23 '22 19:09 kevmoo

Yeah I think leaving it open for now is good.

srawlins avatar Sep 23 '22 20:09 srawlins

@srawlins – can we close this as "done"? 😺

kevmoo avatar Oct 26 '22 21:10 kevmoo

I want to check off the last box there for vm_service support. I hoped to work on it today, but... tomorrow!

srawlins avatar Oct 26 '22 22:10 srawlins

Debugging a library without a name seems to work perfectly :D. Checked off vm_service.

srawlins avatar Oct 28 '22 22:10 srawlins