@@ -104,8 +104,8 @@ class FormatCommand extends PackageCommand {
104104 print ('These files are not formatted correctly (see diff below):' );
105105 LineSplitter .split (stdout).map ((String line) => ' $line ' ).forEach (print);
106106
107- print ('\n To fix run "pub global activate flutter_plugin_tools && '
108- 'pub global run flutter_plugin_tools format" or copy-paste '
107+ print ('\n To fix run "dart pub global activate flutter_plugin_tools && '
108+ 'dart pub global run flutter_plugin_tools format" or copy-paste '
109109 'this command into your terminal:' );
110110
111111 final io.ProcessResult diff = await processRunner.run (
@@ -128,16 +128,11 @@ class FormatCommand extends PackageCommand {
128128 final Iterable <String > clangFiles = _getPathsWithExtensions (
129129 files, < String > {'.h' , '.m' , '.mm' , '.cc' , '.cpp' });
130130 if (clangFiles.isNotEmpty) {
131- final String clangFormat = getStringArg ('clang-format' );
132- if (! await _hasDependency (clangFormat)) {
133- printError ('Unable to run "clang-format". Make sure that it is in your '
134- 'path, or provide a full path with --clang-format.' );
135- throw ToolExit (_exitDependencyMissing);
136- }
131+ final String clangFormat = await _findValidClangFormat ();
137132
138133 print ('Formatting .cc, .cpp, .h, .m, and .mm files...' );
139134 final int exitCode = await _runBatched (
140- getStringArg ( 'clang-format' ) , < String > ['-i' , '--style=file' ],
135+ clangFormat , < String > ['-i' , '--style=file' ],
141136 files: clangFiles);
142137 if (exitCode != 0 ) {
143138 printError (
@@ -147,6 +142,26 @@ class FormatCommand extends PackageCommand {
147142 }
148143 }
149144
145+ Future <String > _findValidClangFormat () async {
146+ final String clangFormatArg = getStringArg ('clang-format' );
147+ if (await _hasDependency (clangFormatArg)) {
148+ return clangFormatArg;
149+ }
150+
151+ // There is a known issue where "chromium/depot_tools/clang-format"
152+ // fails with "Problem while looking for clang-format in Chromium source tree".
153+ // Loop through all "clang-format"s in PATH until a working one is found,
154+ // for example "/usr/local/bin/clang-format" or a "brew" installed version.
155+ for (final String clangFormatPath in await _whichAll ('clang-format' )) {
156+ if (await _hasDependency (clangFormatPath)) {
157+ return clangFormatPath;
158+ }
159+ }
160+ printError ('Unable to run "clang-format". Make sure that it is in your '
161+ 'path, or provide a full path with --clang-format.' );
162+ throw ToolExit (_exitDependencyMissing);
163+ }
164+
150165 Future <void > _formatJava (
151166 Iterable <String > files, String googleFormatterPath) async {
152167 final Iterable <String > javaFiles =
@@ -279,6 +294,26 @@ class FormatCommand extends PackageCommand {
279294 return true ;
280295 }
281296
297+ /// Returns all instances of [command] executable found on user path.
298+ Future <List <String >> _whichAll (String command) async {
299+ try {
300+ final io.ProcessResult result =
301+ await processRunner.run ('which' , < String > ['-a' , command]);
302+
303+ if (result.exitCode != 0 ) {
304+ return < String > [];
305+ }
306+
307+ final String stdout = result.stdout.trim () as String ;
308+ if (stdout.isEmpty) {
309+ return < String > [];
310+ }
311+ return LineSplitter .split (stdout).toList ();
312+ } on io.ProcessException {
313+ return < String > [];
314+ }
315+ }
316+
282317 /// Runs [command] on [arguments] on all of the files in [files] , batched as
283318 /// necessary to avoid OS command-line length limits.
284319 ///
0 commit comments