Skip to content

Commit 5eff2a0

Browse files
jensjohacommit-bot@chromium.org
authored andcommitted
Split CanonicalNameError, no warning if CanonicalNameSdkError
Prior to this CL, we would issue a warning whenever a CanonicalNameError was encountered. This is in principal a good thing, but because we currently have no way to detect if the sdk we get is the one we expect (by any other measure than when it issues a CanonicalNameError) we often issue these warnings for no "real reason" whenever, for instance, the flutter sdk changes. This CL splits the CanonicalNameError in two such that errors with references to the sdk ("dart:" libraries) issue CanonicalNameSdkError instead, an we then handle that differently. Namely we silently ignore the error (i.e. don't issue a warning) and just don't initialize from dill. This should remedy the situation and be strictly better than to always swallow CanonicalNameErrors. Bug: 36032 Change-Id: Idbae0b5ee5b9843a5dbeb49b3c65ae25f5962e36 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/105240 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Kevin Millikin <[email protected]>
1 parent 0c1c0af commit 5eff2a0

File tree

3 files changed

+103
-27
lines changed

3 files changed

+103
-27
lines changed

pkg/front_end/lib/src/fasta/incremental_compiler.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import 'dart:async' show Future;
99
import 'package:kernel/binary/ast_from_binary.dart' show BinaryBuilder;
1010

1111
import 'package:kernel/binary/ast_from_binary.dart'
12-
show BinaryBuilder, CanonicalNameError, InvalidKernelVersionError;
12+
show
13+
BinaryBuilder,
14+
CanonicalNameError,
15+
CanonicalNameSdkError,
16+
InvalidKernelVersionError;
1317

1418
import 'package:kernel/class_hierarchy.dart' show ClassHierarchy;
1519

@@ -161,7 +165,9 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
161165
bytesLength =
162166
prepareSummary(summaryBytes, uriTranslator, c, data);
163167

164-
if (e is InvalidKernelVersionError || e is PackageChangedError) {
168+
if (e is InvalidKernelVersionError ||
169+
e is PackageChangedError ||
170+
e is CanonicalNameSdkError) {
165171
// Don't report any warning.
166172
} else {
167173
Uri gzInitializedFrom;

pkg/front_end/test/incremental_load_from_invalid_dill_test.dart

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import 'package:front_end/src/fasta/kernel/utils.dart' show serializeComponent;
4040

4141
import 'package:front_end/src/fasta/severity.dart' show Severity;
4242

43-
import 'package:kernel/kernel.dart' show Component;
43+
import 'package:kernel/kernel.dart' show Component, Library;
4444

4545
import 'incremental_load_from_dill_test.dart' show getOptions;
4646

@@ -56,24 +56,26 @@ class Tester {
5656
Uri sdkSummary;
5757
Uri initializeFrom;
5858
Uri helperFile;
59+
Uri helper2File;
5960
Uri entryPoint;
61+
Uri entryPointImportDartFoo;
6062
Uri platformUri;
6163
List<int> sdkSummaryData;
6264
List<DiagnosticMessage> errorMessages;
6365
List<DiagnosticMessage> warningMessages;
6466
MemoryFileSystem fs;
6567
CompilerOptions options;
68+
IncrementalCompiler compiler;
6669

67-
compileExpectInitializeFailAndSpecificWarning(
70+
void compileExpectInitializeFailAndSpecificWarning(
6871
Code expectedWarningCode, bool writeFileOnCrashReport) async {
6972
errorMessages.clear();
7073
warningMessages.clear();
7174
options.writeFileOnCrashReport = writeFileOnCrashReport;
72-
DeleteTempFilesIncrementalCompiler compiler =
73-
new DeleteTempFilesIncrementalCompiler(
74-
new CompilerContext(
75-
new ProcessedOptions(options: options, inputs: [entryPoint])),
76-
initializeFrom);
75+
compiler = new DeleteTempFilesIncrementalCompiler(
76+
new CompilerContext(
77+
new ProcessedOptions(options: options, inputs: [entryPoint])),
78+
initializeFrom);
7779
await compiler.computeDelta();
7880
if (compiler.initializedFromDill) {
7981
Expect.fail("Expected to not be able to initialized from dill, but did.");
@@ -91,13 +93,40 @@ class Tester {
9193
}
9294
}
9395

96+
Future<Component> compileExpectOk(
97+
bool initializedFromDill, Uri compileThis) async {
98+
errorMessages.clear();
99+
warningMessages.clear();
100+
options.writeFileOnCrashReport = false;
101+
compiler = new DeleteTempFilesIncrementalCompiler(
102+
new CompilerContext(
103+
new ProcessedOptions(options: options, inputs: [compileThis])),
104+
initializeFrom);
105+
Component component = await compiler.computeDelta();
106+
107+
if (compiler.initializedFromDill != initializedFromDill) {
108+
Expect.fail("Expected initializedFromDill to be $initializedFromDill "
109+
"but was ${compiler.initializedFromDill}");
110+
}
111+
if (errorMessages.isNotEmpty) {
112+
Expect.fail("Got unexpected errors: " + joinMessages(errorMessages));
113+
}
114+
if (warningMessages.isNotEmpty) {
115+
Expect.fail("Got unexpected warnings: " + joinMessages(warningMessages));
116+
}
117+
118+
return component;
119+
}
120+
94121
initialize() async {
95122
sdkRoot = computePlatformBinariesLocation(forceBuildDir: true);
96123
base = Uri.parse("org-dartlang-test:///");
97124
sdkSummary = base.resolve("vm_platform.dill");
98125
initializeFrom = base.resolve("initializeFrom.dill");
99126
helperFile = base.resolve("helper.dart");
127+
helper2File = base.resolve("helper2.dart");
100128
entryPoint = base.resolve("small.dart");
129+
entryPointImportDartFoo = base.resolve("small_foo.dart");
101130
platformUri = sdkRoot.resolve("vm_platform_strong.dill");
102131
sdkSummaryData = await new File.fromUri(platformUri).readAsBytes();
103132
errorMessages = <DiagnosticMessage>[];
@@ -108,6 +137,7 @@ class Tester {
108137
options.fileSystem = fs;
109138
options.sdkRoot = null;
110139
options.sdkSummary = sdkSummary;
140+
options.omitPlatform = true;
111141
options.onDiagnostic = (DiagnosticMessage message) {
112142
if (message.severity == Severity.error) {
113143
errorMessages.add(message);
@@ -121,17 +151,28 @@ class Tester {
121151
foo() {
122152
print("hello from foo");
123153
}
154+
""");
155+
fs.entityForUri(helper2File).writeAsStringSync("""
156+
foo2() {
157+
print("hello from foo2");
158+
}
124159
""");
125160
fs.entityForUri(entryPoint).writeAsStringSync("""
126161
import "helper.dart" as helper;
127162
main() {
128163
helper.foo();
129164
}
165+
""");
166+
fs.entityForUri(entryPointImportDartFoo).writeAsStringSync("""
167+
import "dart:foo" as helper;
168+
main() {
169+
helper.foo2();
170+
}
130171
""");
131172
}
132173

133174
Future<Null> test() async {
134-
IncrementalCompiler compiler = new IncrementalCompiler(
175+
compiler = new IncrementalCompiler(
135176
new CompilerContext(
136177
new ProcessedOptions(options: options, inputs: [entryPoint])),
137178
initializeFrom);
@@ -140,23 +181,32 @@ main() {
140181
List<int> dataGood = serializeComponent(componentGood);
141182
fs.entityForUri(initializeFrom).writeAsBytesSync(dataGood);
142183

143-
// Initialize from good dill file should be ok.
184+
// Create fake "dart:foo" library.
185+
options.omitPlatform = false;
144186
compiler = new IncrementalCompiler(
145187
new CompilerContext(
146-
new ProcessedOptions(options: options, inputs: [entryPoint])),
188+
new ProcessedOptions(options: options, inputs: [helper2File])),
147189
initializeFrom);
148-
compiler.invalidate(entryPoint);
149-
Component component = await compiler.computeDelta();
150-
if (!compiler.initializedFromDill) {
151-
Expect.fail(
152-
"Expected to have sucessfully initialized from dill, but didn't.");
153-
}
154-
if (errorMessages.isNotEmpty) {
155-
Expect.fail("Got unexpected errors: " + joinMessages(errorMessages));
156-
}
157-
if (warningMessages.isNotEmpty) {
158-
Expect.fail("Got unexpected warnings: " + joinMessages(warningMessages));
190+
Component componentHelper = await compiler.computeDelta();
191+
Library helper2Lib = componentHelper.libraries
192+
.firstWhere((lib) => lib.importUri == helper2File);
193+
helper2Lib.importUri = new Uri(scheme: "dart", path: "foo");
194+
List<int> sdkWithDartFoo = serializeComponent(componentHelper);
195+
options.omitPlatform = true;
196+
197+
// Compile with our fake sdk with dart:foo should be ok.
198+
List<int> orgSdkBytes = await fs.entityForUri(sdkSummary).readAsBytes();
199+
fs.entityForUri(sdkSummary).writeAsBytesSync(sdkWithDartFoo);
200+
Component component = await compileExpectOk(true, entryPointImportDartFoo);
201+
fs.entityForUri(sdkSummary).writeAsBytesSync(orgSdkBytes);
202+
if (component.libraries.length != 1) {
203+
Expect.fail("Expected 1 library, got ${component.libraries.length}: "
204+
"${component.libraries}");
159205
}
206+
List<int> dataLinkedToSdkWithFoo = serializeComponent(component);
207+
208+
// Initialize from good dill file should be ok.
209+
await compileExpectOk(true, entryPoint);
160210

161211
// Create a partial dill file.
162212
compiler.invalidate(entryPoint);
@@ -181,6 +231,11 @@ main() {
181231
codeInitializeFromDillUnknownProblem, true);
182232
await compileExpectInitializeFailAndSpecificWarning(
183233
codeInitializeFromDillUnknownProblemNoDump, false);
234+
235+
// Create a dill with a reference to a non-existing sdk thing:
236+
// Should be ok (for now), but we shouldn't actually initialize from dill.
237+
fs.entityForUri(initializeFrom).writeAsBytesSync(dataLinkedToSdkWithFoo);
238+
await compileExpectOk(false, entryPoint);
184239
}
185240
}
186241

pkg/kernel/lib/binary/ast_from_binary.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class CanonicalNameError {
3939
CanonicalNameError(this.message);
4040
}
4141

42+
class CanonicalNameSdkError extends CanonicalNameError {
43+
CanonicalNameSdkError(String message) : super(message);
44+
}
45+
4246
class _ComponentIndex {
4347
static const numberOfFixedFields = 9;
4448

@@ -528,8 +532,8 @@ class BinaryBuilder {
528532
// OK then.
529533
checkReferenceNode = false;
530534
} else {
531-
throw new CanonicalNameError(
532-
"Null reference (${child.name}) ($child).");
535+
throw buildCanonicalNameError(
536+
"Null reference (${child.name}) ($child).", child);
533537
}
534538
}
535539
if (checkReferenceNode) {
@@ -538,8 +542,8 @@ class BinaryBuilder {
538542
"Canonical name and reference doesn't agree.");
539543
}
540544
if (child.reference.node == null) {
541-
throw new CanonicalNameError(
542-
"Reference is null (${child.name}) ($child).");
545+
throw buildCanonicalNameError(
546+
"Reference is null (${child.name}) ($child).", child);
543547
}
544548
}
545549
}
@@ -548,6 +552,17 @@ class BinaryBuilder {
548552
}
549553
}
550554

555+
CanonicalNameError buildCanonicalNameError(
556+
String message, CanonicalName problemNode) {
557+
// Special-case missing sdk entries as that is probably a change to the
558+
// platform - that's something we might want to react differently to.
559+
String libraryUri = problemNode?.nonRootTop?.name ?? "";
560+
if (libraryUri.startsWith("dart:")) {
561+
return new CanonicalNameSdkError(message);
562+
}
563+
return new CanonicalNameError(message);
564+
}
565+
551566
_ComponentIndex _readComponentIndex(int componentFileSize) {
552567
int savedByteIndex = _byteOffset;
553568

0 commit comments

Comments
 (0)