Skip to content

Fix crash on missing libraries.json #1472

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

Merged
Merged
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
1 change: 1 addition & 0 deletions dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Show an alert in the Dart Debug Extension for a multi-app scenario.
- Fix a bug where `dartEmitDebugEvents` was set as a `String` instead of `bool`
in the injected client.
- Emit a warning instead of crashing on missing `libraries.json`.

**Breaking changes:**

Expand Down
9 changes: 7 additions & 2 deletions dwds/lib/src/utilities/dart_uri.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class DartUri {

librariesPath ??=
p.toUri(p.join(_sdkDir.toFilePath(), 'lib', 'libraries.json'));

var packagesUri = p.toUri(p.join(currentDirectory, '.packages'));

clear();
Expand All @@ -169,6 +170,8 @@ class DartUri {

/// Clear the uri resolution tables.
static void clear() {
_librariesSpec = null;
_packageConfig = null;
_resolvedUriToUri.clear();
_uriToResolvedUri.clear();
}
Expand Down Expand Up @@ -198,8 +201,10 @@ class DartUri {
var json = File.fromUri(uri).readAsStringSync();
_librariesSpec =
LibrariesSpecification.parse(uri, json).specificationFor('dartdevc');
} on LibrariesSpecificationException catch (e, s) {
_logger.warning('Cannot read libraries spec: $uri', e, s);
} on LibrariesSpecificationException catch (e) {
_logger.warning('Cannot parse libraries spec: $uri', e);
} on FileSystemException catch (e) {
_logger.warning('Cannot read libraries spec: $uri', e);
}
}

Expand Down
54 changes: 54 additions & 0 deletions dwds/test/dart_uri_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:path/path.dart' as p;
import 'package:test/test.dart';

import 'fixtures/fakes.dart';
import 'fixtures/logging.dart';

class TestStrategy extends FakeStrategy {
@override
Expand Down Expand Up @@ -109,5 +110,58 @@ void main() {
expect(unresolved, 'dart:io');
});
});

group('initialized with other SDK directory with no libraries spec', () {
Directory outputDir;
var logs = <String>[];

void logWriter(level, message,
{String error, String loggerName, String stackTrace}) {
var errorMessage = error == null ? '' : ':\n$error';
var stackMessage = stackTrace == null ? '' : ':\n$stackTrace';
logs.add('[$level] $loggerName: $message'
'$errorMessage'
'$stackMessage');
}

setUpAll(() async {
configureLogWriter(customLogWriter: logWriter);
var systemTempDir = Directory.systemTemp;
outputDir = systemTempDir.createTempSync('foo bar');

var fakeSdkDir = outputDir.path;
var fakeLibrariesDir = p.join(fakeSdkDir, 'lib');
var fakeLibrariesPath = p.join(fakeLibrariesDir, 'libraries.json');

await DartUri.initialize(
sdkDir: Uri.file(fakeSdkDir),
librariesPath: Uri.file(fakeLibrariesPath));
await DartUri.recordAbsoluteUris(['dart:io', 'dart:html']);

expect(
logs,
containsAll([
contains('[WARNING] DartUri: Cannot read libraries spec:'),
contains('[WARNING] DartUri: Unresolved uri: dart:io'),
contains('[WARNING] DartUri: Unresolved uri: dart:html'),
]));
});

tearDownAll(() async {
DartUri.clear();
await outputDir?.delete(recursive: true);
});

test('cannot resolve uris', () {
var resolved = DartUri.toResolvedUri('dart:io');
expect(resolved, null);
});

test('cannot unresolve uris', () {
var unresolved =
DartUri.toPackageUri('org-dartlang-sdk:///sdk/lib/io/io.dart');
expect(unresolved, null);
});
});
});
}