Commit 96ba7f76 authored by Michael Goderbauer's avatar Michael Goderbauer Committed by GitHub

Revert "Simplify path handling logic in dependency checker and devFS (#8414)" (#8467)

This reverts commit e7bde11c.

Reason: broke hot reload when using "package:" style imports for sources
within the same project.
parent 0533ffc0
0edcbb7db427e4fcaff23308ea95f9aec0ebb432
eaacbf5f6f8f7f0be4cec64babc777816c68dd44
......@@ -5,6 +5,7 @@
import 'dart:convert';
import '../artifacts.dart';
import '../base/file_system.dart';
import '../base/process.dart';
class DartDependencySetBuilder {
......@@ -29,6 +30,16 @@ class DartDependencySetBuilder {
String output = runSyncAndThrowStdErrOnError(args);
return new Set<String>.from(LineSplitter.split(output));
final List<String> lines = LineSplitter.split(output).toList();
final Set<String> minimalDependencies = new Set<String>();
for (String line in lines) {
if (!line.startsWith('package:')) {
// We convert the uris so that they are relative to the project
// root.
line = fs.path.relative(line, from: projectRootPath);
}
minimalDependencies.add(line);
}
return minimalDependencies;
}
}
......@@ -5,6 +5,7 @@
import 'asset.dart';
import 'base/file_system.dart';
import 'dart/dependencies.dart';
import 'dart/package_map.dart';
import 'globals.dart';
class DependencyChecker {
......@@ -17,7 +18,10 @@ class DependencyChecker {
/// if it cannot be determined.
bool check(DateTime threshold) {
_dependencies.clear();
PackageMap packageMap;
// Parse the package map.
try {
packageMap = new PackageMap(builder.packagesFilePath)..load();
_dependencies.add(builder.packagesFilePath);
} catch (e, st) {
printTrace('DependencyChecker: could not parse .packages file:\n$e\n$st');
......@@ -25,7 +29,16 @@ class DependencyChecker {
}
// Build the set of Dart dependencies.
try {
_dependencies.addAll(builder.build());
Set<String> dependencies = builder.build();
for (String path in dependencies) {
// Ensure all paths are absolute.
if (path.startsWith('package:')) {
path = packageMap.pathForPackage(Uri.parse(path));
} else {
path = fs.path.join(builder.projectRootPath, path);
}
_dependencies.add(path);
}
} catch (e, st) {
printTrace('DependencyChecker: error determining .dart dependencies:\n$e\n$st');
return true;
......
......@@ -461,10 +461,10 @@ class DevFS {
}
bool _shouldIgnore(String devicePath) {
List<String> ignoredPrefixes = <String>['android' + fs.path.separator,
List<String> ignoredPrefixes = <String>['android/',
getBuildDirectory(),
'ios' + fs.path.separator,
'.pub' + fs.path.separator];
'ios/',
'.pub/'];
for (String ignoredPrefix in ignoredPrefixes) {
if (devicePath.startsWith(ignoredPrefix))
return true;
......@@ -473,14 +473,16 @@ class DevFS {
}
Future<bool> _scanDirectory(Directory directory,
{String directoryNameOnDevice,
{String directoryName,
bool recursive: false,
bool ignoreDotFiles: true,
String packagesDirectoryName,
Set<String> fileFilter}) async {
if (directoryNameOnDevice == null) {
directoryNameOnDevice = fs.path.relative(directory.path, from: rootDirectory.path);
if (directoryNameOnDevice == '.')
directoryNameOnDevice = '';
String prefix = directoryName;
if (prefix == null) {
prefix = fs.path.relative(directory.path, from: rootDirectory.path);
if (prefix == '.')
prefix = '';
}
try {
Stream<FileSystemEntity> files =
......@@ -506,8 +508,24 @@ class DevFS {
}
final String relativePath =
fs.path.relative(file.path, from: directory.path);
final String devicePath = fs.path.join(directoryNameOnDevice, relativePath);
if ((fileFilter != null) && !fileFilter.contains(file.absolute.path)) {
final String devicePath = fs.path.join(prefix, relativePath);
bool filtered = false;
if ((fileFilter != null) &&
!fileFilter.contains(devicePath)) {
if (packagesDirectoryName != null) {
// Double check the filter for packages/packagename/
final String packagesDevicePath =
fs.path.join(packagesDirectoryName, relativePath);
if (!fileFilter.contains(packagesDevicePath)) {
// File was not in the filter set.
filtered = true;
}
} else {
// File was not in the filter set.
filtered = true;
}
}
if (filtered) {
// Skip files that are not included in the filter.
continue;
}
......@@ -530,26 +548,27 @@ class DevFS {
PackageMap packageMap = new PackageMap(_packagesFilePath);
for (String packageName in packageMap.map.keys) {
Uri packageUri = packageMap.map[packageName];
String packagePath = fs.path.fromUri(packageUri);
Directory packageDirectory = fs.directory(packageUri);
String directoryNameOnDevice = fs.path.join('packages', packageName);
bool packageExists;
if (fs.path.isWithin(rootDirectory.path, packagePath)) {
// We already scanned everything under the root directory.
packageExists = packageDirectory.existsSync();
directoryNameOnDevice = fs.path.relative(packagePath, from: rootDirectory.path);
} else {
packageExists =
await _scanDirectory(packageDirectory,
directoryNameOnDevice: directoryNameOnDevice,
recursive: true,
fileFilter: fileFilter);
}
Uri uri = packageMap.map[packageName];
// This project's own package.
final bool isProjectPackage = uri.toString() == 'lib/';
final String directoryName =
isProjectPackage ? 'lib' : fs.path.join('packages', packageName);
// If this is the project's package, we need to pass both
// package:<package_name> and lib/ as paths to be checked against
// the filter because we must support both package: imports and relative
// path imports within the project's own code.
final String packagesDirectoryName =
isProjectPackage ? fs.path.join('packages', packageName) : null;
Directory directory = fs.directory(uri);
bool packageExists =
await _scanDirectory(directory,
directoryName: directoryName,
recursive: true,
packagesDirectoryName: packagesDirectoryName,
fileFilter: fileFilter);
if (packageExists) {
sb ??= new StringBuffer();
sb.writeln('$packageName:$directoryNameOnDevice');
sb.writeln('$packageName:$directoryName');
}
}
if (sb != null) {
......
......@@ -98,7 +98,18 @@ class HotRunner extends ResidentRunner {
new DartDependencySetBuilder(
mainPath, projectRootPath, packagesFilePath);
try {
_dartDependencies = new Set<String>.from(dartDependencySetBuilder.build());
Set<String> dependencies = dartDependencySetBuilder.build();
_dartDependencies = new Set<String>();
for (String path in dependencies) {
// We need to tweak package: uris so that they reflect their devFS
// location.
if (path.startsWith('package:')) {
// Swap out package: for packages/ because we place all package
// sources under packages/.
path = path.replaceFirst('package:', 'packages/');
}
_dartDependencies.add(path);
}
} catch (error) {
printStatus('Error detected in application source code:', emphasis: true);
printError('$error');
......
......@@ -21,8 +21,8 @@ void main() {
DartDependencySetBuilder builder =
new DartDependencySetBuilder(mainPath, testPath, packagesPath);
Set<String> dependencies = builder.build();
expect(dependencies.contains(mainPath), isTrue);
expect(dependencies.contains(fs.path.join(testPath, 'foo.dart')), isTrue);
expect(dependencies.contains('main.dart'), isTrue);
expect(dependencies.contains('foo.dart'), isTrue);
});
testUsingContext('syntax_error', () {
final String testPath = fs.path.join(dataPath, 'syntax_error');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment