[dart2js,ddc] Ensure valid d8 breakpoints in testing

D8 breakpoints used in sourcemap testing should only be set for
test files and not, accidentally, in sdk source files. This
was hit in a dart2js step debugging test where, after unforking,
the line number in the test file accidentally matched those of
print.dart from the sdk.

Change-Id: I6a9c6f5bfd28b8821e9cee9b10cc6c67e4fc689b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/146781
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
This commit is contained in:
Johnni Winther 2020-05-06 14:36:32 +00:00 committed by commit-bot@chromium.org
parent 097c3aab2c
commit a2015351c8
4 changed files with 23 additions and 13 deletions

View file

@ -14,6 +14,7 @@ import 'package:testing/testing.dart';
class Data {
Uri uri;
Directory outDir;
String testFileName;
AnnotatedCode code;
List<String> d8Output;
}
@ -75,8 +76,8 @@ class StepWithD8 extends Step<Data, Data, ChainContext> {
@override
Future<Result<Data>> run(Data data, ChainContext context) async {
var outWrapperPath = p.join(data.outDir.path, 'wrapper.js');
var runResult =
runD8AndStep(data.outDir.path, data.code, ['--module', outWrapperPath]);
var runResult = runD8AndStep(data.outDir.path, data.testFileName, data.code,
['--module', outWrapperPath]);
data.d8Output = (runResult.stdout as String).split('\n');
return pass(data);
}

View file

@ -40,8 +40,9 @@ class Compile extends Step<Data, Data, ChainContext> {
data.outDir = await Directory.systemTemp.createTemp('ddc_step_test');
data.code = AnnotatedCode.fromText(
File(inputFile).readAsStringSync(), commentStart, commentEnd);
data.testFileName = 'test.dart';
var outDirUri = data.outDir.uri;
var testFile = outDirUri.resolve('test.dart');
var testFile = outDirUri.resolve(data.testFileName);
File.fromUri(testFile).writeAsStringSync(data.code.sourceCode);
var outputFilename = 'js.js';
var outputFile = outDirUri.resolve(outputFilename);

View file

@ -13,8 +13,8 @@ import 'package:source_maps/source_maps.dart';
* It is furthermore expected that the js has been compiled from a file in the
* same folder called test.dart.
*/
ProcessResult runD8AndStep(
String outputPath, AnnotatedCode code, List<String> scriptD8Command) {
ProcessResult runD8AndStep(String outputPath, String testFileName,
AnnotatedCode code, List<String> scriptD8Command) {
var outputFile = path.join(outputPath, "js.js");
SingleMapping sourceMap =
parse(new File("${outputFile}.map").readAsStringSync());
@ -37,12 +37,13 @@ ProcessResult runD8AndStep(
// Annotations are 1-based, js breakpoints are 0-based.
for (Annotation breakAt
in code.annotations.where((a) => a.text.trim() == "bl")) {
breakpoints.add(_getJsBreakpointLine(sourceMap, breakAt.lineNo - 1));
breakpoints
.add(_getJsBreakpointLine(testFileName, sourceMap, breakAt.lineNo - 1));
}
for (Annotation breakAt
in code.annotations.where((a) => a.text.trim().startsWith("bc:"))) {
breakpoints.add(_getJsBreakpointLineAndColumn(
sourceMap, breakAt.lineNo - 1, breakAt.columnNo - 1));
testFileName, sourceMap, breakAt.lineNo - 1, breakAt.columnNo - 1));
}
File inspectorFile = new File.fromUri(
@ -376,13 +377,16 @@ class _PointMapping {
* frontend/blob/fa18d70a995f06cb73365b2e5b8ae974cf60bd3a/front_end/sources/
* JavaScriptSourceFrame.js#L1520-L1523
*/
String _getJsBreakpointLine(SingleMapping sourceMap, int breakOnLine) {
String _getJsBreakpointLine(
String testFileName, SingleMapping sourceMap, int breakOnLine) {
List<_PointMapping> mappingsOnLines = [];
for (var line in sourceMap.lines) {
for (var entry in line.entries) {
if (entry.sourceLine == null) continue;
if (entry.sourceLine >= breakOnLine &&
entry.sourceLine < breakOnLine + 4) {
entry.sourceLine < breakOnLine + 4 &&
entry.sourceUrlId != null &&
sourceMap.urls[entry.sourceUrlId] == testFileName) {
mappingsOnLines.add(new _PointMapping(
entry.sourceLine, entry.sourceColumn, line.line, entry.column));
}
@ -407,12 +411,14 @@ String _getJsBreakpointLine(SingleMapping sourceMap, int breakOnLine) {
/**
* Input and output is expected to be 0-based.
*/
String _getJsBreakpointLineAndColumn(
String _getJsBreakpointLineAndColumn(String testFileName,
SingleMapping sourceMap, int breakOnLine, int breakOnColumn) {
for (var line in sourceMap.lines) {
for (var entry in line.entries) {
if (entry.sourceLine == breakOnLine &&
entry.sourceColumn == breakOnColumn)
entry.sourceColumn == breakOnColumn &&
entry.sourceUrlId != null &&
sourceMap.urls[entry.sourceUrlId] == testFileName)
return "${line.line}:${entry.column}";
}
}

View file

@ -67,8 +67,9 @@ Future runTest(AnnotatedCode annotatedCode, String config,
bool verbose: false,
List<String> options: const <String>[]}) async {
Directory dir = Directory.systemTemp.createTempSync('stepping_test');
String testFileName = 'test.dart';
String path = dir.path;
String inputFile = '$path/test.dart';
String inputFile = '$path/$testFileName';
new File(inputFile).writeAsStringSync(annotatedCode.sourceCode);
String outputFile = '$path/js.js';
List<String> arguments = <String>[
@ -82,7 +83,8 @@ Future runTest(AnnotatedCode annotatedCode, String config,
'$sdkPath/_internal/js_runtime/lib/preambles/d8.js',
outputFile
];
ProcessResult result = runD8AndStep(dir.path, annotatedCode, scriptD8Command);
ProcessResult result =
runD8AndStep(dir.path, testFileName, annotatedCode, scriptD8Command);
List<String> d8output = result.stdout.split("\n");
if (verbose) {
d8output.forEach(print);