From fa44a1cf3779235280b2f32c8ad788327dd21835 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 11 Feb 2020 09:31:58 -0800 Subject: [PATCH] [flutter_tools] fix path escaping on in depfile generation (#50538) --- .../lib/src/build_system/depfile.dart | 12 ++++--- .../build_system/depfile_test.dart | 34 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/packages/flutter_tools/lib/src/build_system/depfile.dart b/packages/flutter_tools/lib/src/build_system/depfile.dart index 6ae60212b6..7a5070426d 100644 --- a/packages/flutter_tools/lib/src/build_system/depfile.dart +++ b/packages/flutter_tools/lib/src/build_system/depfile.dart @@ -74,11 +74,15 @@ class Depfile { void _writeFilesToBuffer(List files, StringBuffer buffer) { for (final File outputFile in files) { if (globals.platform.isWindows) { - // Paths in a depfile have to be escaped on windows. - final String escapedPath = outputFile.path.replaceAll(r'\', r'\\'); - buffer.write(' $escapedPath'); + // Foward slashes and spaces in a depfile have to be escaped on windows. + final String path = outputFile.path + .replaceAll(r'\', r'\\') + .replaceAll(r' ', r'\ '); + buffer.write(' $path'); } else { - buffer.write(' ${outputFile.path}'); + final String path = outputFile.path + .replaceAll(r' ', r'\ '); + buffer.write(' $path'); } } } diff --git a/packages/flutter_tools/test/general.shard/build_system/depfile_test.dart b/packages/flutter_tools/test/general.shard/build_system/depfile_test.dart index 60331fae83..1f31c4a4a3 100644 --- a/packages/flutter_tools/test/general.shard/build_system/depfile_test.dart +++ b/packages/flutter_tools/test/general.shard/build_system/depfile_test.dart @@ -6,6 +6,7 @@ import 'package:file/memory.dart'; import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/build_system/depfile.dart'; import 'package:flutter_tools/src/globals.dart' as globals; +import 'package:platform/platform.dart'; import '../../src/common.dart'; import '../../src/testbed.dart'; @@ -66,6 +67,39 @@ C:\\a.txt: C:\\b.txt FileSystem: () => MemoryFileSystem(style: FileSystemStyle.windows), })); + test('Can escape depfile with windows file paths and spaces in directory names', () => testbed.run(() { + final File inputFile = globals.fs.directory(r'Hello Flutter').childFile('a.txt').absolute + ..createSync(recursive: true); + final File outputFile = globals.fs.directory(r'Hello Flutter').childFile('b.txt').absolute + ..createSync(); + final Depfile depfile = Depfile([inputFile], [outputFile]); + final File outputDepfile = globals.fs.file('depfile'); + depfile.writeToFile(outputDepfile); + + expect(outputDepfile.readAsStringSync(), contains(r'C:\\Hello\ Flutter\\a.txt')); + expect(outputDepfile.readAsStringSync(), contains(r'C:\\Hello\ Flutter\\b.txt')); + }, overrides: { + FileSystem: () => MemoryFileSystem(style: FileSystemStyle.windows), + Platform: () => FakePlatform(operatingSystem: 'windows'), + })); + + test('Can escape depfile with spaces in directory names', () => testbed.run(() { + final File inputFile = globals.fs.directory(r'Hello Flutter').childFile('a.txt').absolute + ..createSync(recursive: true); + final File outputFile = globals.fs.directory(r'Hello Flutter').childFile('b.txt').absolute + ..createSync(); + final Depfile depfile = Depfile([inputFile], [outputFile]); + final File outputDepfile = globals.fs.file('depfile'); + depfile.writeToFile(outputDepfile); + + expect(outputDepfile.readAsStringSync(), contains(r'/Hello\ Flutter/a.txt')); + expect(outputDepfile.readAsStringSync(), contains(r'/Hello\ Flutter/b.txt')); + }, overrides: { + FileSystem: () => MemoryFileSystem(style: FileSystemStyle.posix), + Platform: () => FakePlatform(operatingSystem: 'linux'), + })); + + test('Resillient to weird whitespace', () => testbed.run(() { final File depfileSource = globals.fs.file('example.d')..writeAsStringSync(r''' a.txt