[CP-stable]🐛 Use consist slashes when generating dep files (#169630)

This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request)
Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request.

### Issue Link:
What is the link to the issue this cherry-pick is addressing?

https://github.com/flutter/flutter/issues/163591

### Changelog Description:

Normalizes file paths in every depfile, especially on Windows. It eliminates the inconsistency that can occur when other codes find the file paths are different and produce unexpected results.

### Impact Description:

The most noticeable impact so far is that people are unable to build flavored Android packages on Windows repeatedly until the next clean.

### Workaround:
Is there a workaround for this issue?

The workaround is to manually patch the project's gradle script: https://github.com/flutter/flutter/issues/163591#issuecomment-2887039609
From my experience, the patch is not always working and is hard to maintain.

### Risk:
What is the risk level of this cherry-pick?

### Test Coverage:
Are you confident that your fix is well-tested by automated tests?

### Validation Steps:
What are the steps to validate that this fix works?

Follow the *steps to reproduce* section in https://github.com/flutter/flutter/issues/163591#issue-2862606263.
This commit is contained in:
flutteractionsbot 2025-05-30 15:11:06 -07:00 committed by GitHub
parent 1091508939
commit 04f9ead567
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 19 deletions

View File

@ -65,16 +65,19 @@ class DepfileService {
} }
void _writeFilesToBuffer(List<File> files, StringBuffer buffer) { void _writeFilesToBuffer(List<File> files, StringBuffer buffer) {
final bool backslash = _fileSystem.path.style.separator == r'\';
for (final File outputFile in files) { for (final File outputFile in files) {
if (_fileSystem.path.style.separator == r'\') { String path = _fileSystem.path.normalize(outputFile.path);
// backslashes and spaces in a depfile have to be escaped if the if (backslash) {
// platform separator is a backslash. // Backslashes in a depfile have to be escaped if the platform separator is a backslash.
final String path = outputFile.path.replaceAll(r'\', r'\\').replaceAll(r' ', r'\ '); path = path.replaceAll(r'\', r'\\');
buffer.write(' $path');
} else { } else {
final String path = outputFile.path.replaceAll(r' ', r'\ '); // Convert all path separators to forward slashes.
buffer.write(' $path'); path = path.replaceAll(r'\', r'/');
} }
// Escape spaces.
path = path.replaceAll(r' ', r'\ ');
buffer.write(' $path');
} }
} }
@ -92,7 +95,8 @@ class DepfileService {
// The tool doesn't write duplicates to these lists. This call is an attempt to // The tool doesn't write duplicates to these lists. This call is an attempt to
// be resilient to the outputs of other tools which write or user edits to depfiles. // be resilient to the outputs of other tools which write or user edits to depfiles.
.toSet() .toSet()
.map(_fileSystem.file) // Normalize the path before creating a file object.
.map((String path) => _fileSystem.file(_fileSystem.path.normalize(path)))
.toList(); .toList();
} }
} }

View File

@ -17,6 +17,7 @@ void main() {
fileSystem = MemoryFileSystem.test(); fileSystem = MemoryFileSystem.test();
depfileService = DepfileService(logger: BufferLogger.test(), fileSystem: fileSystem); depfileService = DepfileService(logger: BufferLogger.test(), fileSystem: fileSystem);
}); });
testWithoutContext('Can parse depfile from file', () { testWithoutContext('Can parse depfile from file', () {
final File depfileSource = fileSystem.file('example.d')..writeAsStringSync(''' final File depfileSource = fileSystem.file('example.d')..writeAsStringSync('''
a.txt: b.txt a.txt: b.txt
@ -48,22 +49,34 @@ a.txt c.txt d.txt: b.txt
}); });
testWithoutContext('Can parse depfile with windows file paths', () { testWithoutContext('Can parse depfile with windows file paths', () {
fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows); final FileSystem fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
depfileService = DepfileService(logger: BufferLogger.test(), fileSystem: fileSystem); final DepfileService depfileService = DepfileService(
logger: BufferLogger.test(),
fileSystem: fileSystem,
);
final File depfileSource = fileSystem.file('example.d')..writeAsStringSync(r''' final File depfileSource = fileSystem.file('example.d')..writeAsStringSync(r'''
C:\\a.txt: C:\\b.txt C:\\a1.txt C:\\a2/a3.txt: C:\\b1.txt C:\\b2/b3.txt
'''); ''');
final Depfile depfile = depfileService.parse(depfileSource); final Depfile depfile = depfileService.parse(depfileSource);
expect(depfile.inputs.single.path, r'C:\b.txt'); expect(depfile.inputs.map((File e) => e.path).toList(), <String>[
expect(depfile.outputs.single.path, r'C:\a.txt'); r'C:\b1.txt',
r'C:\b2\b3.txt',
]);
expect(depfile.outputs.map((File e) => e.path).toList(), <String>[
r'C:\a1.txt',
r'C:\a2\a3.txt',
]);
}); });
testWithoutContext( testWithoutContext(
'Can escape depfile with windows file paths and spaces in directory names', 'Can escape depfile with windows file paths and spaces in directory names',
() { () {
fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows); final FileSystem fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
depfileService = DepfileService(logger: BufferLogger.test(), fileSystem: fileSystem); final DepfileService depfileService = DepfileService(
logger: BufferLogger.test(),
fileSystem: fileSystem,
);
final File inputFile = final File inputFile =
fileSystem.directory(r'Hello Flutter').childFile('a.txt').absolute fileSystem.directory(r'Hello Flutter').childFile('a.txt').absolute
..createSync(recursive: true); ..createSync(recursive: true);
@ -73,8 +86,9 @@ C:\\a.txt: C:\\b.txt
final File outputDepfile = fileSystem.file('depfile'); final File outputDepfile = fileSystem.file('depfile');
depfileService.writeToFile(depfile, outputDepfile); depfileService.writeToFile(depfile, outputDepfile);
expect(outputDepfile.readAsStringSync(), contains(r'C:\\Hello\ Flutter\\a.txt')); final String output = outputDepfile.readAsStringSync();
expect(outputDepfile.readAsStringSync(), contains(r'C:\\Hello\ Flutter\\b.txt')); expect(output, contains(r'C:\\Hello\ Flutter\\a.txt'));
expect(output, contains(r'C:\\Hello\ Flutter\\b.txt'));
}, },
); );
@ -88,8 +102,46 @@ C:\\a.txt: C:\\b.txt
final File outputDepfile = fileSystem.file('depfile'); final File outputDepfile = fileSystem.file('depfile');
depfileService.writeToFile(depfile, outputDepfile); depfileService.writeToFile(depfile, outputDepfile);
expect(outputDepfile.readAsStringSync(), contains(r'/Hello\ Flutter/a.txt')); final String output = outputDepfile.readAsStringSync();
expect(outputDepfile.readAsStringSync(), contains(r'/Hello\ Flutter/b.txt')); expect(output, contains(r'/Hello\ Flutter/a.txt'));
expect(output, contains(r'/Hello\ Flutter/b.txt'));
});
testWithoutContext('Can produce normalized paths', () {
final List<(FileSystemStyle style, String input, String output, List<String> expects)> pairs =
<(FileSystemStyle style, String input, String output, List<String> expects)>[
(
FileSystemStyle.posix,
r'Hello Flutter\a.txt',
r'Hello Flutter\b.txt',
<String>[r'/Hello\ Flutter/a.txt', r'/Hello\ Flutter/b.txt'],
),
(
FileSystemStyle.windows,
r'Hello Flutter/a.txt',
r'Hello Flutter/b.txt',
<String>[r'\\Hello\ Flutter\\a.txt', r'\\Hello\ Flutter\\b.txt'],
),
];
for (final (FileSystemStyle style, String input, String output, List<String> expects)
in pairs) {
final FileSystem fileSystem = MemoryFileSystem.test(style: style);
final DepfileService depfileService = DepfileService(
logger: BufferLogger.test(),
fileSystem: fileSystem,
);
final File inputFile = fileSystem.file(input).absolute..createSync(recursive: true);
final File outputFile = fileSystem.file(output).absolute..createSync();
final Depfile depfile = Depfile(<File>[inputFile], <File>[outputFile]);
final File outputDepfile = fileSystem.file('depfile');
depfileService.writeToFile(depfile, outputDepfile);
final String outputString = outputDepfile.readAsStringSync();
for (final String path in expects) {
expect(outputString, contains(path));
}
}
}); });
testWithoutContext('Resilient to weird whitespace', () { testWithoutContext('Resilient to weird whitespace', () {