Skip to content

Commit 5e5237a

Browse files
authored
[flutter_tools] Fix UNC path resolution in depfile parsing on Windows (flutter#191265)
Normalize root-relative Windows UNC paths starting with `\UNC\` in depfiles to standard UNC paths starting with `\\`. Also unify `SourceVisitor` depfile parsing to use `DepfileService`. Fixes flutter#43594 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent cbf4601 commit 5e5237a

3 files changed

Lines changed: 70 additions & 30 deletions

File tree

‎packages/flutter_tools/lib/src/build_system/depfile.dart‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class DepfileService {
104104
}
105105

106106
List<File> _processList(String rawText, [Directory? baseDirectory]) {
107+
final isWindows = _fileSystem.path.style.separator == r'\';
107108
return rawText
108109
// Put every file on right-hand side on the separate line
109110
.replaceAllMapped(_separatorExpr, (Match match) => '${match.group(1)}\n')
@@ -119,6 +120,9 @@ class DepfileService {
119120
.toSet()
120121
// Normalize the path before creating a file object.
121122
.map((String path) {
123+
if (isWindows && path.length >= 5 && path.substring(0, 5).toLowerCase() == r'\unc\') {
124+
path = r'\\' + path.substring(5);
125+
}
122126
if (baseDirectory != null && _fileSystem.path.isRelative(path)) {
123127
return _fileSystem.file(
124128
_fileSystem.path.normalize(_fileSystem.path.join(baseDirectory.path, path)),

‎packages/flutter_tools/lib/src/build_system/source.dart‎

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import '../base/file_system.dart';
77
import '../build_info.dart';
88
import '../project.dart';
99
import 'build_system.dart';
10+
import 'depfile.dart';
1011
import 'exceptions.dart';
1112

1213
//////////////////////////////////////////////////////////////////////
@@ -53,49 +54,25 @@ class SourceVisitor implements ResolvedFiles {
5354
bool get containsNewDepfile => _containsNewDepfile;
5455
var _containsNewDepfile = false;
5556

56-
/// Visit a depfile which contains both input and output files.
57+
/// Visit a depfile.
5758
///
58-
/// If the file is missing, this visitor is marked as [containsNewDepfile].
59-
/// This is used by the [Node] class to tell the [BuildSystem] to
59+
/// If the depfile does not exist, marks [containsNewDepfile] as true and
60+
/// returns. This is used by the [Node] class to tell the [BuildSystem] to
6061
/// defer hash computation until after executing the target.
61-
// depfile logic adopted from https://github.com/flutter/flutter/blob/7065e4330624a5a216c8ffbace0a462617dc1bf5/dev/devicelab/lib/framework/apk_utils.dart#L390
6262
void visitDepfile(String name) {
6363
final File depfile = environment.buildDir.childFile(name);
6464
if (!depfile.existsSync()) {
6565
_containsNewDepfile = true;
6666
return;
6767
}
68-
final String contents = depfile.readAsStringSync();
69-
final List<String> colonSeparated = contents.split(': ');
70-
if (colonSeparated.length != 2) {
71-
environment.logger.printError('Invalid depfile: ${depfile.path}');
72-
return;
73-
}
68+
final Depfile parsedDepfile = environment.depFileService.parse(depfile);
7469
if (inputs) {
75-
sources.addAll(_processList(colonSeparated[1].trim()));
70+
sources.addAll(parsedDepfile.inputs);
7671
} else {
77-
sources.addAll(_processList(colonSeparated[0].trim()));
72+
sources.addAll(parsedDepfile.outputs);
7873
}
7974
}
8075

81-
final _separatorExpr = RegExp(r'([^\\]) ');
82-
final _escapeExpr = RegExp(r'\\(.)');
83-
84-
Iterable<File> _processList(String rawText) {
85-
return rawText
86-
// Put every file on right-hand side on the separate line
87-
.replaceAllMapped(_separatorExpr, (Match match) => '${match.group(1)}\n')
88-
.split('\n')
89-
// Expand escape sequences, so that '\ ', for example,ß becomes ' '
90-
.map<String>(
91-
(String path) =>
92-
path.replaceAllMapped(_escapeExpr, (Match match) => match.group(1)!).trim(),
93-
)
94-
.where((String path) => path.isNotEmpty)
95-
.toSet()
96-
.map(environment.fileSystem.file);
97-
}
98-
9976
/// Visit a [Source] which contains a file URL.
10077
///
10178
/// The URL may include constants defined in an [Environment]. If

‎packages/flutter_tools/test/general.shard/build_system/depfile_test.dart‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,65 @@ C:\\a1.txt C:\\a2/a3.txt: C:\\b1.txt C:\\b2/b3.txt
8282
]);
8383
});
8484

85+
testWithoutContext('Can parse depfile with windows UNC file paths', () {
86+
final FileSystem fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
87+
final depfileService = DepfileService(logger: BufferLogger.test(), fileSystem: fileSystem);
88+
final File depfileSource = fileSystem.file('example.d')
89+
..writeAsStringSync(r'''
90+
\\\\server\\share\\a1.txt: \\\\server\\share\\b1.txt
91+
''');
92+
final Depfile depfile = depfileService.parse(depfileSource);
93+
94+
expect(depfile.inputs.map((File e) => e.path).toList(), <String>[r'\\server\share\b1.txt']);
95+
expect(depfile.outputs.map((File e) => e.path).toList(), <String>[r'\\server\share\a1.txt']);
96+
});
97+
98+
testWithoutContext('Can parse depfile with windows long UNC file paths', () {
99+
final FileSystem fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
100+
final depfileService = DepfileService(logger: BufferLogger.test(), fileSystem: fileSystem);
101+
final File depfileSource = fileSystem.file('example.d')
102+
..writeAsStringSync(r'''
103+
\\\\?\\UNC\\server\\share\\a1.txt: \\\\?\\UNC\\server\\share\\b1.txt
104+
''');
105+
final Depfile depfile = depfileService.parse(depfileSource);
106+
107+
expect(depfile.inputs.map((File e) => e.path).toList(), <String>[
108+
r'\\?\UNC\server\share\b1.txt',
109+
]);
110+
expect(depfile.outputs.map((File e) => e.path).toList(), <String>[
111+
r'\\?\UNC\server\share\a1.txt',
112+
]);
113+
});
114+
115+
testWithoutContext(
116+
'Can parse depfile with windows root-relative UNC file paths (issue 43594)',
117+
() {
118+
final FileSystem fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
119+
final depfileService = DepfileService(logger: BufferLogger.test(), fileSystem: fileSystem);
120+
final File depfileSource = fileSystem.file('example.d')
121+
..writeAsStringSync(r'''
122+
\\UNC\\server\\share\\a1.txt: \\UNC\\server\\share\\b1.txt
123+
''');
124+
final Depfile depfile = depfileService.parse(depfileSource);
125+
126+
expect(depfile.inputs.map((File e) => e.path).toList(), <String>[r'\\server\share\b1.txt']);
127+
expect(depfile.outputs.map((File e) => e.path).toList(), <String>[r'\\server\share\a1.txt']);
128+
},
129+
);
130+
131+
testWithoutContext('Can write depfile with windows UNC file paths', () {
132+
final FileSystem fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
133+
final depfileService = DepfileService(logger: BufferLogger.test(), fileSystem: fileSystem);
134+
final File inputFile = fileSystem.file(r'\\server\share\b1.txt');
135+
final File outputFile = fileSystem.file(r'\\server\share\a1.txt');
136+
final depfile = Depfile(<File>[inputFile], <File>[outputFile]);
137+
final File outputDepfile = fileSystem.file('depfile');
138+
depfileService.writeToFile(depfile, outputDepfile);
139+
140+
final String output = outputDepfile.readAsStringSync();
141+
expect(output, contains(r' \\\\server\\share\\a1.txt: \\\\server\\share\\b1.txt'));
142+
});
143+
85144
testWithoutContext(
86145
'Can escape depfile with windows file paths and spaces in directory names',
87146
() {

0 commit comments

Comments
 (0)