Description
[Edit: Updated the title to use 'library augmentation' rather than the older terminology 'augmentation library'. The comments on this issue generally use the older term because they were written before the renaming of that concept.]
As of https://dart-review.googlesource.com/c/sdk/+/315420 (and based on dart-lang/sdk#52575), the compile-time error for a part file that contains a script tag is implemented in the CFE.
It is still possible to use a part file as the only argument to dart
in order to use a library that has said part file as a part as the entry point, except that it is an error when the part file uses a library name in its part of
directive. For example:
// ---------- Part file 'part.dart'.
part of test_lib;
final foo = "foo";
final bar = "bar";
// ---------- Library 'test1.dart'
library test_lib;
part 'part.dart';
var foo2;
main() {
foo2 = 1;
print(bar);
}
// ---------- Library 'test2.dart'.
library test_lib;
part 'part.dart';
var foo;
main() {
foo = 1;
print(bar);
}
dart part.dart
is an error (because test1.dart
is not uniquely determined to be the library that has part.dart
as a part). But if part.dart
had used a URI to denote the owning library then it would not be an error.
In other words, executing a part file is partially supported because there are some situations where a library is uniquely determined to be the owner of the given part file, and that library is a script, i.e., a library that exports a main
top-level function. When no such unique determination can be made, it's an error to execute that part file.
Augmentation libraries have a uniquely determined augmented library (that is, "its owner"), so we could allow an augmentation library to be executed, there is no ambiguity.
However, I tend to see this as a misfeature: It does not add expressive power (just execute the augmented library rather than the augmentation library). Moreover, it may cause some confusion about the semantics ("OK, I'm running augmentation_lib_1.dart
and it seems to do the same thing as augmentation_lib_2.dart
, and the same thing as main.dart
, so what's the difference?")
I'd recommend that we avoid this messy behavior up front and make it a an error to execute an augmentation library.
Of course, this may be seen as a tool specific behavior rather than a language rule, and different tool teams might have different preferences (and it may or may not be a front-end only issue), but I still think it makes sense to have at least a brief shared discussion.
@dart-lang/language-team, @johnniwinther, @sigmundch, @mkustermann, WDYT? @srawlins, does it make any difference for the analyzer (presumably, the analyzer doesn't care which library would the entry point, if any)?