Skip to content

Commit 96cd200

Browse files
authored
Add librariesSpec arg to FronendServerClient.start (#2855)
1 parent f27046a commit 96cd200

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎frontend_server_client/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Update Dart SDK constraint to `^3.10.0`.
44
- Allow package_config `3.x.x`.
5+
- Add `librariesSpec` parameter to `FrontendServerClient.start()`.
56
- Use `sdkRoot` to resolve Dart executable and frontend server snapshot locations when provided.
67

78
## 4.0.0

‎frontend_server_client/lib/src/dartdevc_frontend_server_client.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class DartDevcFrontendServerClient implements FrontendServerClient {
6767
String fileSystemScheme =
6868
'org-dartlang-root', // Custom scheme for virtual `fileSystemRoots`.
6969
String? frontendServerPath, // Defaults to the snapshot in the sdk.
70+
String? librariesSpec,
7071
String packagesJson = '.dart_tool/package_config.json',
7172
String? platformKernel, // Defaults to the dartdevc platform from the sdk.
7273
String? sdkRoot, // Defaults to the current SDK root.
@@ -83,6 +84,7 @@ class DartDevcFrontendServerClient implements FrontendServerClient {
8384
fileSystemRoots: fileSystemRoots,
8485
fileSystemScheme: fileSystemScheme,
8586
frontendServerPath: frontendServerPath,
87+
librariesSpec: librariesSpec,
8688
packagesJson: packagesJson,
8789
sdkRoot: sdkRoot,
8890
target: 'dartdevc',

‎frontend_server_client/lib/src/frontend_server_client.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class FrontendServerClient {
3939
/// The [entrypoint] and [packagesJson] may be a relative path or any uri
4040
/// supported by the frontend server.
4141
///
42+
/// The [librariesSpec] argument can be provided to specify a custom libraries
43+
/// specification file.
44+
///
4245
/// The [outputDillPath] determines where the primary output should be, and
4346
/// some targets may output additional files based on that file name (by
4447
/// adding file extensions for instance).
@@ -63,6 +66,7 @@ class FrontendServerClient {
6366
String fileSystemScheme =
6467
'org-dartlang-root', // Custom scheme for virtual `fileSystemRoots`.
6568
String? frontendServerPath, // Defaults to the snapshot in the sdk.
69+
String? librariesSpec,
6670
String packagesJson = '.dart_tool/package_config.json',
6771
String? sdkRoot, // Defaults to the current SDK root.
6872
String target = 'vm', // The kernel target type.
@@ -84,6 +88,7 @@ class FrontendServerClient {
8488
'--output-dill',
8589
outputDillPath,
8690
'--packages=$packagesJson',
91+
if (librariesSpec != null) ...['--libraries-spec', librariesSpec],
8792
if (enableHttpUris) '--enable-http-uris',
8893
'--incremental',
8994
if (verbose) '--verbose',

‎frontend_server_client/test/frontend_server_client_test.dart‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,58 @@ void main() {
337337
expect(processResult.stdout, startsWith('goodbye world'));
338338
expect(processResult.exitCode, 0);
339339
});
340+
341+
test('can support custom librariesSpec', () async {
342+
final defaultLibrariesJson = File(p.join(sdkDir, 'lib', 'libraries.json'));
343+
final libraries =
344+
jsonDecode(defaultLibrariesJson.readAsStringSync())
345+
as Map<String, Object?>;
346+
347+
// Create the custom library file
348+
final customLibFile = File(p.join(packageRoot, 'bin', 'custom_lib.dart'));
349+
await customLibFile.writeAsString('void hello() => print("custom!");');
350+
351+
if (libraries['vm_common'] case {
352+
'libraries': final Map<String, Object?> libs,
353+
}) {
354+
libs['custom_lib'] = {'uri': customLibFile.absolute.uri.toString()};
355+
}
356+
357+
final customLibrariesJsonFile = File(p.join(packageRoot, 'libraries.json'));
358+
await customLibrariesJsonFile.writeAsString(jsonEncode(libraries));
359+
360+
final entrypoint = p.join(packageRoot, 'bin', 'main_custom.dart');
361+
await File(entrypoint).writeAsString('''
362+
import 'dart:custom_lib';
363+
void main() {
364+
hello();
365+
}
366+
''');
367+
368+
client = await FrontendServerClient.start(
369+
entrypoint,
370+
p.join(packageRoot, 'out_custom.dill'),
371+
vmPlatformDill,
372+
packagesJson: packagesJsonPath,
373+
librariesSpec: customLibrariesJsonFile.uri.toString(),
374+
);
375+
var result = await client!.compile();
376+
client!.accept();
377+
expect(result.errorCount, 0);
378+
expect(result.dillOutput, isNotNull);
379+
await client!.shutdown();
380+
381+
// Without librariesSpec, compiling the same entrypoint should fail
382+
client = await FrontendServerClient.start(
383+
entrypoint,
384+
p.join(packageRoot, 'out_custom_fail.dill'),
385+
vmPlatformDill,
386+
packagesJson: packagesJsonPath,
387+
);
388+
result = await client!.compile();
389+
client!.accept();
390+
expect(result.errorCount, greaterThan(0));
391+
});
340392
}
341393

342394
Future<Isolate> waitForIsolatesAndResume(VmService vmService) async {

0 commit comments

Comments
 (0)