Skip to content

Turn off logging for testing calls. #3275

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions pkgs/dart_services/lib/src/common_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:shelf_router/shelf_router.dart';
import 'analysis.dart';
import 'caching.dart';
import 'compiling.dart';
import 'context.dart';
import 'flutter_genui.dart';
import 'generative_ai.dart';
import 'project_templates.dart';
Expand Down Expand Up @@ -135,7 +136,7 @@ class CommonServerApi {
);

final results = await serialize(() {
return impl.compiler.compile(sourceRequest.source);
return impl.compiler.compile(sourceRequest.source, request.ctx);
});

if (results.hasOutput) {
Expand Down Expand Up @@ -181,7 +182,8 @@ class CommonServerApi {
return await _handleCompileDDC(
request,
apiVersion,
(request) => impl.compiler.compileDDC(request.source),
(compileRequest) =>
impl.compiler.compileDDC(compileRequest.source, request.ctx),
);
}

Expand All @@ -192,7 +194,8 @@ class CommonServerApi {
return await _handleCompileDDC(
request,
apiVersion,
(request) => impl.compiler.compileNewDDC(request.source),
(compileRequest) =>
impl.compiler.compileNewDDC(compileRequest.source, request.ctx),
);
}

Expand All @@ -203,8 +206,11 @@ class CommonServerApi {
return await _handleCompileDDC(
request,
apiVersion,
(request) =>
impl.compiler.compileNewDDCReload(request.source, request.deltaDill!),
(compileRequest) => impl.compiler.compileNewDDCReload(
compileRequest.source,
compileRequest.deltaDill!,
request.ctx,
),
);
}

Expand Down
47 changes: 30 additions & 17 deletions pkgs/dart_services/lib/src/compiling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import 'dart:convert';
import 'dart:io';

import 'package:bazel_worker/driver.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart' as path;

import 'common.dart';
import 'context.dart';
import 'logging.dart';
import 'project_templates.dart';
import 'pub.dart';
import 'sdk.dart';

final Logger _logger = Logger('compiler');
final DartPadLogger _logger = DartPadLogger('compiler');

/// An interface to the dart2js compiler. A compiler object can process one
/// compile at a time.
Expand Down Expand Up @@ -47,11 +48,12 @@ class Compiler {

/// Compile the given string and return the resulting [CompilationResults].
Future<CompilationResults> compile(
String source, {
String source,
RequestContext ctx, {
bool returnSourceMap = false,
}) async {
final temp = Directory.systemTemp.createTempSync('dartpad');
_logger.fine('Temp directory created: ${temp.path}');
_logger.fine('Temp directory created: ${temp.path}', ctx);

try {
_copyPath(_projectTemplates.dartPath, temp.path);
Expand All @@ -77,7 +79,7 @@ class Compiler {
final mainJs = File(path.join(temp.path, '$kMainDart.js'));
final mainSourceMap = File(path.join(temp.path, '$kMainDart.js.map'));

_logger.fine('About to exec: $_dartPath ${arguments.join(' ')}');
_logger.fine('About to exec: $_dartPath ${arguments.join(' ')}', ctx);

final result = await Process.run(
_dartPath,
Expand All @@ -104,24 +106,25 @@ class Compiler {
return results;
}
} catch (e, st) {
_logger.warning('Compiler failed: $e\n$st');
_logger.warning('Compiler failed: $e\n$st', ctx);
rethrow;
} finally {
temp.deleteSync(recursive: true);
_logger.fine('temp folder removed: ${temp.path}');
_logger.fine('temp folder removed: ${temp.path}', ctx);
}
}

/// Compile the given string and return the resulting [DDCCompilationResults].
Future<DDCCompilationResults> _compileDDC(
String source, {
String source,
RequestContext ctx, {
String? deltaDill,
required bool useNew,
}) async {
final imports = getAllImportsFor(source);

final temp = Directory.systemTemp.createTempSync('dartpad');
_logger.fine('Temp directory created: ${temp.path}');
_logger.fine('Temp directory created: ${temp.path}', ctx);

try {
final usingFlutter = usesFlutterWeb(imports);
Expand Down Expand Up @@ -182,7 +185,10 @@ class Compiler {
'--packages=${path.join(temp.path, '.dart_tool', 'package_config.json')}',
];

_logger.fine('About to exec dartdevc worker: ${arguments.join(' ')}"');
_logger.fine(
'About to exec dartdevc worker: ${arguments.join(' ')}"',
ctx,
);

final response = await _ddcDriver.doWork(
WorkRequest(arguments: arguments),
Expand Down Expand Up @@ -221,27 +227,34 @@ class Compiler {
return results;
}
} catch (e, st) {
_logger.warning('Compiler failed: $e\n$st');
_logger.warning('Compiler failed: $e\n$st', ctx);
rethrow;
} finally {
temp.deleteSync(recursive: true);
_logger.fine('temp folder removed: ${temp.path}');
_logger.fine('temp folder removed: ${temp.path}', ctx);
}
}

Future<DDCCompilationResults> compileDDC(String source) async {
return await _compileDDC(source, useNew: false);
Future<DDCCompilationResults> compileDDC(
String source,
RequestContext ctx,
) async {
return await _compileDDC(source, ctx, useNew: false);
}

Future<DDCCompilationResults> compileNewDDC(String source) async {
return await _compileDDC(source, useNew: true);
Future<DDCCompilationResults> compileNewDDC(
String source,
RequestContext ctx,
) async {
return await _compileDDC(source, ctx, useNew: true);
}

Future<DDCCompilationResults> compileNewDDCReload(
String source,
String deltaDill,
RequestContext ctx,
) async {
return await _compileDDC(source, deltaDill: deltaDill, useNew: true);
return await _compileDDC(source, ctx, deltaDill: deltaDill, useNew: true);
}

Future<void> dispose() async {
Expand Down
20 changes: 20 additions & 0 deletions pkgs/dart_services/lib/src/context.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:dartpad_shared/headers.dart';
import 'package:shelf/shelf.dart';

class RequestContext {
final bool loggingOn;

RequestContext({required this.loggingOn});
}

extension RequestExtension on Request {
RequestContext get ctx {
return RequestContext(
loggingOn: DartPadRequestHeaders.fromJson(headers).loggingOn,
);
}
}
22 changes: 22 additions & 0 deletions pkgs/dart_services/lib/src/logging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import 'package:logging/logging.dart';

import 'context.dart';

const bool verboseLogging = false;

final _wsRegex = RegExp(r' \s+');
Expand Down Expand Up @@ -32,3 +34,23 @@ void emitLogsToStdout() {
}
});
}

class DartPadLogger {
late final Logger _logger;

DartPadLogger(String name) {
_logger = Logger(name);
}

void fine(String s, RequestContext ctx) {
if (ctx.loggingOn) {
_logger.fine(s);
}
}

void warning(String s, RequestContext ctx) {
if (ctx.loggingOn) {
_logger.warning(s);
}
}
}
2 changes: 1 addition & 1 deletion pkgs/dart_services/test/analysis_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:dart_services/src/sdk.dart';
import 'package:dartpad_shared/model.dart' as api;
import 'package:test/test.dart';

import 'src/sample_code.dart';
import 'test_infra/sample_code.dart';

void main() => defineTests();

Expand Down
45 changes: 27 additions & 18 deletions pkgs/dart_services/test/compiling_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import 'package:dart_services/src/compiling.dart';
import 'package:dart_services/src/sdk.dart';
import 'package:test/test.dart';

import 'src/sample_code.dart';
import 'test_infra/sample_code.dart';
import 'test_infra/utils.dart';

void main() => defineTests();

Expand All @@ -24,7 +25,7 @@ void defineTests() {
});

test('simple', () async {
final result = await compiler.compile(sampleCode);
final result = await compiler.compile(sampleCode, ctx);

expect(result.problems, isEmpty);
expect(result.success, true);
Expand Down Expand Up @@ -169,55 +170,63 @@ void defineTests() {

testDDCEndpoint(
'compileDDC',
restartEndpoint: (source) => compiler.compileDDC(source),
restartEndpoint: (source) => compiler.compileDDC(source, ctx),
expectNewDeltaDill: false,
compiledIndicator: "define('dartpad_main', [",
);
if (sdk.dartMajorVersion >= 3 && sdk.dartMinorVersion >= 8) {
// DDC only supports these at version 3.8 and higher.
testDDCEndpoint(
'compileNewDDC',
restartEndpoint: (source) => compiler.compileNewDDC(source),
restartEndpoint: (source) => compiler.compileNewDDC(source, ctx),
expectNewDeltaDill: true,
compiledIndicator: 'defineLibrary("package:dartpad_sample/main.dart"',
);
testDDCEndpoint(
'compileNewDDCReload',
restartEndpoint: (source) => compiler.compileNewDDC(source),
restartEndpoint: (source) => compiler.compileNewDDC(source, ctx),
reloadEndpoint:
(source, deltaDill) =>
compiler.compileNewDDCReload(source, deltaDill),
compiler.compileNewDDCReload(source, deltaDill, ctx),
expectNewDeltaDill: true,
compiledIndicator: 'defineLibrary("package:dartpad_sample/main.dart"',
);
}

test('sourcemap', () async {
final result = await compiler.compile(sampleCode, returnSourceMap: true);
final result = await compiler.compile(
sampleCode,
ctx,
returnSourceMap: true,
);
expect(result.success, true);
expect(result.compiledJS, isNotEmpty);
expect(result.sourceMap, isNotNull);
expect(result.sourceMap, isNotEmpty);
});

test('version', () async {
final result = await compiler.compile(sampleCode, returnSourceMap: true);
final result = await compiler.compile(
sampleCode,
ctx,
returnSourceMap: true,
);
expect(result.sourceMap, isNotNull);
expect(result.sourceMap, isNotEmpty);
});

test('simple web', () async {
final result = await compiler.compile(sampleCodeWeb);
final result = await compiler.compile(sampleCodeWeb, ctx);
expect(result.success, true);
});

test('web async', () async {
final result = await compiler.compile(sampleCodeAsync);
final result = await compiler.compile(sampleCodeAsync, ctx);
expect(result.success, true);
});

test('errors', () async {
final result = await compiler.compile(sampleCodeError);
final result = await compiler.compile(sampleCodeError, ctx);
expect(result.success, false);
expect(result.problems.length, 1);
expect(result.problems[0].toString(), contains('Error: Expected'));
Expand All @@ -233,7 +242,7 @@ void main() {
}

''';
final result = await compiler.compile(code);
final result = await compiler.compile(code, ctx);
expect(result.problems.length, 0);
});

Expand All @@ -248,7 +257,7 @@ void main() {
}

''';
final result = await compiler.compile(code);
final result = await compiler.compile(code, ctx);
expect(result.problems.length, 0);
});

Expand All @@ -257,7 +266,7 @@ void main() {
import 'foo.dart';
void main() { missingMethod ('foo'); }
''';
final result = await compiler.compile(code);
final result = await compiler.compile(code, ctx);
expect(result.problems, hasLength(1));
expect(
result.problems.single.message,
Expand All @@ -270,7 +279,7 @@ void main() { missingMethod ('foo'); }
import 'http://example.com';
void main() { missingMethod ('foo'); }
''';
final result = await compiler.compile(code);
final result = await compiler.compile(code, ctx);
expect(result.problems, hasLength(1));
expect(
result.problems.single.message,
Expand All @@ -283,7 +292,7 @@ void main() { missingMethod ('foo'); }
import 'package:foo';
import 'package:bar';
''';
final result = await compiler.compile(code);
final result = await compiler.compile(code, ctx);
expect(result.problems, hasLength(1));
expect(
result.problems.single.message,
Expand All @@ -296,7 +305,7 @@ import 'package:bar';
});

test('disallow compiler warnings', () async {
final result = await compiler.compile(sampleCodeErrors);
final result = await compiler.compile(sampleCodeErrors, ctx);
expect(result.success, false);
});

Expand All @@ -305,7 +314,7 @@ import 'package:bar';
import 'dart:foo';
void main() { print ('foo'); }
''';
final result = await compiler.compile(code);
final result = await compiler.compile(code, ctx);
expect(result.problems.length, 1);
});
});
Expand Down
Loading