Skip to content
Open
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
26 changes: 22 additions & 4 deletions native_toolchain_rust/lib/src/dependency_discoverer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,27 @@ interface class DependencyDiscoverer {
/// coverage for incremental rebuilds.
Iterable<Uri> discover(String dependencyFilePath) {
logger.fine('Discovering dependencies in $dependencyFilePath');
return File(dependencyFilePath)
.readAsLinesSync()
.expand((line) => line.trim().split(' ').skip(1))
.map(path.toUri);
final rawContent = File(dependencyFilePath).readAsStringSync();
// Normalize line continuations (backslash followed by newline).
final normalized = rawContent.replaceAll(RegExp(r'\\\r?\n'), ' ');
final result = <Uri>[];
for (final line in normalized.split(RegExp(r'\r?\n'))) {
final trimmed = line.trim();
if (trimmed.isEmpty) continue;
final colonIndex = trimmed.indexOf(': ');
final depsString = colonIndex != -1
? trimmed.substring(colonIndex + 2)
: trimmed;
// Cargo/Make dep-info escapes spaces in file paths with a backslash ('\ ').
// Split only on whitespace that is not preceded by a backslash.
final rawTokens = depsString.split(RegExp(r'(?<!\\) +'));
for (final token in rawTokens) {
final unescaped = token.replaceAll(r'\ ', ' ').trim();
if (unescaped.isNotEmpty) {
result.add(path.toUri(unescaped));
}
}
}
return result;
}
}
24 changes: 24 additions & 0 deletions native_toolchain_rust/test/dependency_discoverer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ target/debug/librust_lib.a: /path/to/some/other/file.rs
expect(dependencies, isNot(contains(path.toUri(dependencyFilePath))));
});

test('discover correctly parses paths with escaped spaces and '
'line continuations', () {
final dependencyFilePath = path.join(tempDir.path, 'test.d');
File(dependencyFilePath).writeAsStringSync(
'target/debug/librust_lib.a: src/lib.rs '
r'C:\Users\John\ Doe\lib.rs'
'\n'
'target/debug/librust_lib.a: /path/with\\ space/mod.rs \\\n'
' /continuation/path.rs',
);

final dependencies = dependencyDiscoverer.discover(dependencyFilePath);

expect(
dependencies,
unorderedEquals([
path.toUri('src/lib.rs'),
path.toUri(r'C:\Users\John Doe\lib.rs'),
path.toUri('/path/with space/mod.rs'),
path.toUri('/continuation/path.rs'),
]),
);
});

test(
'discover throws an exception if the dependency file is not found',
() {
Expand Down
Loading