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

Allow library declarations with no name. #1073

Closed
9 of 21 tasks
lrhn opened this issue Jul 7, 2020 · 22 comments
Closed
9 of 21 tasks

Allow library declarations with no name. #1073

lrhn opened this issue Jul 7, 2020 · 22 comments
Assignees
Labels
request Requests to resolve a particular developer problem small-feature A small feature which is relatively cheap to implement.

Comments

@lrhn
Copy link
Member

lrhn commented Jul 7, 2020

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.

Feature specification

https://github.com/dart-lang/language/blob/master/accepted/future-releases/unnamed-libraries/feature-specification.md

Experiment flag

--unnamed-libraries

Implementation Tasks

@lrhn lrhn added the request Requests to resolve a particular developer problem label Jul 7, 2020
@lrhn lrhn added the small-feature A small feature which is relatively cheap to implement. label Jul 8, 2020
@kevmoo
Copy link
Member

kevmoo commented Jul 30, 2020

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

@jcollins-g
Copy link

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.

@lrhn
Copy link
Member Author

lrhn commented Dec 10, 2020

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

@Silentdoer
Copy link

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);

@lrhn
Copy link
Member Author

lrhn commented Apr 28, 2021

@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.

@eernstg
Copy link
Member

eernstg commented Apr 28, 2021

Dart disallows such conflicts

We actually removed that error in 14fcda2, 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.

@rakudrama
Copy link
Member

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.

@lrhn
Copy link
Member Author

lrhn commented Jul 20, 2022

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.)

@kevmoo
Copy link
Member

kevmoo commented Jul 20, 2022

Just do it!

Yes please!!!

@jcollins-g
Copy link

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

@srawlins
Copy link
Member

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

@srawlins
Copy link
Member

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?

@leafpetersen
Copy link
Member

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 leafpetersen added this to Being implemented in Language funnel Sep 14, 2022
@srawlins
Copy link
Member

Specification mailed in: #2490

@kevmoo

This comment was marked as resolved.

@osa1

This comment was marked as resolved.

@osa1 osa1 reopened this Sep 23, 2022
@kevmoo

This comment was marked as resolved.

@kevmoo
Copy link
Member

kevmoo commented Sep 23, 2022

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

@srawlins
Copy link
Member

Yeah I think leaving it open for now is good.

copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue Oct 4, 2022
Bug: dart-lang/language#1073
Change-Id: I9d57c86c35b5712029c610197e6ad823d34c492e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/262580
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue Oct 25, 2022
TEST=tests/language/library/unnamed_library_test.dart

Bug: dart-lang/language#1073
Change-Id: I1c7fa7b4ee4450e344a7613525765e4ab590cc8c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/265381
Reviewed-by: Alexander Aprelev <aam@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Michael Thomsen <mit@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
@kevmoo
Copy link
Member

kevmoo commented Oct 26, 2022

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

@srawlins
Copy link
Member

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

@srawlins
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem small-feature A small feature which is relatively cheap to implement.
Projects
Status: Done
Development

No branches or pull requests

9 participants