Skip to content

Migrate .packages to package_config #1543

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 15 commits into from
Mar 22, 2022
1 change: 1 addition & 0 deletions dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
restart.
- Remove verbose printing on receiving DevTools events.
- Update `vm_service` version to `^8.2.0`.
- Migrate .packages to package_config.json.
- Update error message on expression evaluation using unloaded libraries.

**Breaking changes:**
Expand Down
8 changes: 4 additions & 4 deletions dwds/lib/src/readers/frontend_server_asset_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ class FrontendServerAssetReader implements AssetReader {
/// Corresponding `.json` and `.map` files will be read relative to
/// [outputPath].
///
/// [_packageRoot] is the path to the directory that contains a `.packages`
/// file for the application.
/// [_packageRoot] is the path to the directory that contains a
/// `.dart_tool/package_config.json` file for the application.
FrontendServerAssetReader(
String outputPath,
this._packageRoot,
) : _mapOriginal = File('$outputPath.map'),
_mapIncremental = File('$outputPath.incremental.map'),
_jsonOriginal = File('$outputPath.json'),
_jsonIncremental = File('$outputPath.incremental.json'),
_packageConfig = loadPackageConfig(
File(p.absolute(p.join(_packageRoot, '.packages'))));
_packageConfig = loadPackageConfig(File(p
.absolute(p.join(_packageRoot, '.dart_tool/package_config.json'))));

@override
Future<String> dartSourceContents(String serverPath) async {
Expand Down
7 changes: 4 additions & 3 deletions dwds/lib/src/utilities/dart_uri.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ class DartUri {
/// Record library and script uris to enable resolving library and script paths.
static Future<void> initialize(SdkConfiguration sdkConfiguration) async {
_sdkConfiguration = sdkConfiguration;
var packagesUri = p.toUri(p.join(currentDirectory, '.packages'));
var packagesUri =
p.toUri(p.join(currentDirectory, '.dart_tool/package_config.json'));

clear();

Expand Down Expand Up @@ -190,8 +191,8 @@ class DartUri {
/// Returns the dirname for the server URI.
static String _dirForServerUri(String uri) => p.dirname(Uri.parse(uri).path);

/// Load the .packages file associated with the running application so we can
/// resolve file URLs into package: URLs appropriately.
/// Load the .dart_tool/package_config.json file associated with the running
/// application so we can resolve file URLs into package: URLs appropriately.
static Future<void> _loadPackageConfig(Uri uri) async {
_packageConfig = await loadPackageConfigUri(uri, onError: (e) {
_logger.warning('Cannot read packages spec: $uri', e);
Expand Down
19 changes: 12 additions & 7 deletions dwds/test/fixtures/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,17 @@ class TestContext {
.absolute(directory ?? p.relative(relativeDirectory, from: p.current)));

DartUri.currentDirectory = workingDirectory;
_packagesFilePath = p.join(workingDirectory, '.packages');
_packagesFilePath =
p.join(workingDirectory, '.dart_tool/package_config.json');

_entryFile = File(p.normalize(
p.absolute(entry ?? p.relative(relativeEntry, from: p.current))));
var entryFilePath = p.normalize(
p.absolute(entry ?? p.relative(relativeEntry, from: p.current)));

_logger.info('Serving: $pathToServe/$path');
_logger.info('Packages: $_packagesFilePath');
_logger.info('Entry: $entryFilePath');

_entryFile = File(entryFilePath);
_entryContents = _entryFile.readAsStringSync();
}

Expand Down Expand Up @@ -241,15 +247,14 @@ class TestContext {
case CompilationMode.frontendServer:
{
soundNullSafety ??= true;
var fileSystemRoot = p.dirname(_packagesFilePath);
var projectDirectory = p.dirname(p.dirname(_packagesFilePath));
var entryPath =
_entryFile.path.substring(fileSystemRoot.length + 1);
_entryFile.path.substring(projectDirectory.length + 1);
webRunner = ResidentWebRunner(
'${Uri.file(entryPath)}',
urlEncoder,
fileSystemRoot,
_packagesFilePath,
[fileSystemRoot],
[projectDirectory],
'org-dartlang-app',
_outputDir.path,
verboseCompiler);
Expand Down
14 changes: 14 additions & 0 deletions dwds/test/frontend_server_breakpoint_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:vm_service/vm_service.dart';
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';

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

final context = TestContext(
directory: p.join('..', 'fixtures', '_testPackage'),
Expand All @@ -27,10 +28,22 @@ ChromeProxyService get service =>
WipConnection get tabConnection => context.tabConnection;

void main() {
// Enable verbose logging for debugging.
var debug = false;

// Change to 'true' to print expression compiler messages to console.
//
// Note: expression compiler runs in an isolate, so its output is not
// currently redirected to a logger. As a result, it will be printed
// regardless of the logger settings.
var verboseCompiler = false;

group('shared context', () {
setUpAll(() async {
setCurrentLogWriter(debug: debug);
await context.setUp(
compilationMode: CompilationMode.frontendServer,
verboseCompiler: verboseCompiler,
);
});

Expand All @@ -46,6 +59,7 @@ void main() {
Stream<Event> stream;

setUp(() async {
setCurrentLogWriter(debug: debug);
vm = await service.getVM();
isolate = await service.getIsolate(vm.isolates.first.id);
scripts = await service.getScripts(isolate.id);
Expand Down
13 changes: 5 additions & 8 deletions frontend_server_common/lib/src/asset_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestAssetServer implements AssetReader {
TestAssetServer(
this._root,
this._httpServer,
this._packages,
this._packageConfig,
this.internetAddress,
this._fileSystem,
);
Expand All @@ -47,14 +47,12 @@ class TestAssetServer implements AssetReader {
String hostname,
int port,
UrlEncoder urlTunneller,
PackageConfig packageConfig,
) async {
var address = (await InternetAddress.lookup(hostname)).first;
var httpServer = await HttpServer.bind(address, port);
var packages = await loadPackageConfigUri(Uri.base.resolve('.packages'),
loader: (Uri uri) => fileSystem.file(uri).readAsBytes());
var server =
TestAssetServer(root, httpServer, packages, address, fileSystem);

TestAssetServer(root, httpServer, packageConfig, address, fileSystem);
return server;
}

Expand All @@ -64,8 +62,7 @@ class TestAssetServer implements AssetReader {
final Map<String, Uint8List> _sourcemaps = <String, Uint8List>{};
final Map<String, Uint8List> _metadata = <String, Uint8List>{};
String _mergedMetadata;
// ignore: deprecated_member_use
final PackageConfig _packages;
final PackageConfig _packageConfig;
final InternetAddress internetAddress;

Uint8List getFile(String path) => _files[path];
Expand Down Expand Up @@ -241,7 +238,7 @@ class TestAssetServer implements AssetReader {
// The file might have been a package file which is signaled by a
// `/packages/<package>/<path>` request.
if (segments.first == 'packages') {
var packageFile = _fileSystem.file(_packages
var packageFile = _fileSystem.file(_packageConfig
.resolve(Uri(scheme: 'package', pathSegments: segments.skip(1))));
if (packageFile.existsSync()) {
return packageFile;
Expand Down
29 changes: 17 additions & 12 deletions frontend_server_common/lib/src/devfs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:dwds/dwds.dart';
import 'package:file/file.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:package_config/package_config.dart';
import 'package:path/path.dart' as p;

import 'asset.dart';
Expand All @@ -30,8 +31,7 @@ class WebDevFS {
this.fileSystem,
this.hostname,
this.port,
this.packagesFilePath,
this.packagesPath,
this.packageConfigPath,
this.root,
this.urlTunneller,
});
Expand All @@ -40,24 +40,31 @@ class WebDevFS {
TestAssetServer assetServer;
final String hostname;
final int port;
final String packagesFilePath;
final String packagesPath;
final String packageConfigPath;
final String root;
final UrlEncoder urlTunneller;
Directory _savedCurrentDirectory;
List<Uri> sources;
PackageConfig _packageConfig;

Future<Uri> create() async {
_savedCurrentDirectory = fileSystem.currentDirectory;
fileSystem.currentDirectory = packagesPath;
// package_config.json is located in <project directory>/.dart_tool/package_config
var projectDirectory = p.dirname(p.dirname(packageConfigPath));

fileSystem.currentDirectory = projectDirectory;

_packageConfig = await loadPackageConfigUri(Uri.file(packageConfigPath),
loader: (Uri uri) => fileSystem.file(uri).readAsBytes());

assetServer = await TestAssetServer.start(
fileSystem, root, hostname, port, urlTunneller);
fileSystem, root, hostname, port, urlTunneller, _packageConfig);
return Uri.parse('http://$hostname:$port');
}

Future<void> dispose() {
fileSystem.currentDirectory = _savedCurrentDirectory;
return assetServer.close();
return assetServer?.close();
}

Future<UpdateFSReport> update({
Expand Down Expand Up @@ -102,11 +109,9 @@ class WebDevFS {
generator.reset();

var compilerOutput = await generator.recompile(
'org-dartlang-app:///$mainPath',
invalidatedFiles,
outputPath: p.join(dillOutputPath, 'app.dill'),
packagesFilePath: packagesFilePath,
);
Uri.parse('org-dartlang-app:///$mainPath'), invalidatedFiles,
outputPath: p.join(dillOutputPath, 'app.dill'),
packageConfig: _packageConfig);
if (compilerOutput == null || compilerOutput.errorCount > 0) {
return UpdateFSReport(success: false);
}
Expand Down
Loading