Skip to content

Update FormatTool. -w bug fix and support language version#466

Merged
btr-rmconsole-2[bot] merged 6 commits intomasterfrom
handle_removed_dart_style_cli_option
Mar 16, 2026
Merged

Update FormatTool. -w bug fix and support language version#466
btr-rmconsole-2[bot] merged 6 commits intomasterfrom
handle_removed_dart_style_cli_option

Conversation

@robbecker-wf
Copy link
Member

@robbecker-wf robbecker-wf commented Mar 13, 2026

Summary

In version 3 of dart_style the -w argument was removed from the CLI but dart_dev still uses it under Dart 3 because it can resolve to the newer dart_style version. dart_style changelog link https://pub.dev/packages/dart_style/changelog#:~:text=Remove%20the%20old,the%20CLI%20options.

While fixing this to support dart_style 3, I realized that the constructor for DartFormatter now takes a required languageVersion parameter and the CLI takes the --languageVersion argument. So in the spirit of supporting dart_style 3+ properly, this PR now conditionally uses the -w argument when dart_style is < 3 and provides the ability to configure the language version all the way through from config.dart for the FormatTool

note: MOST code changes are dart formatting changes. I've comment the PR where there are non-formatting changes.

Documentation improvements:

  • Added a section to doc/tools/format-tool.md explaining how to configure the formatter language version using dart_dev.

ArgResults? argResults,
List<String>? configuredFormatterArgs,
bool passWriteArgForOverwrite = true,
String? languageVersion,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's a non-formatting change

defaultMode: defaultMode,
exclude: exclude,
formatter: formatter,
languageVersion: languageVersion,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's a non-formatting change

/// `--language-version`.
///
/// If null, `latest` will be used when dart_dev decides the flag is needed.
String? languageVersion;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's a non-formatting change

FormatMode? mode, {
ArgResults? argResults,
List<String>? configuredFormatterArgs,
String? languageVersion,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's a non-formatting change. added languageVersion

// 1. Mode flag(s), if configured
if (mode == FormatMode.check) ...['-o', 'none', '--set-exit-if-changed'],
if (mode == FormatMode.dryRun) ...['-o', 'none'],
if (languageVersion != null) '--language-version=$languageVersion',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's a non-formatting change

FormatMode? defaultMode,
List<Glob>? exclude,
Formatter? formatter,
String? languageVersion,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's a non-formatting change

formatter,
dartStyleSupportsWriteArg,
configuredLanguageVersion: languageVersion,
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's a non-formatting change. determine if we should use -w and what the language version to use is.

mode,
argResults: context.argResults,
configuredFormatterArgs: configuredFormatterArgs,
languageVersion: formatterLanguageVersion,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass in the language version

argResults: context.argResults,
configuredFormatterArgs: configuredFormatterArgs,
passWriteArgForOverwrite: dartStyleSupportsWriteArg,
languageVersion: formatterLanguageVersion,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass in language version and -w usage

@@ -515,14 +580,99 @@ ProcessDeclaration buildFormatProcess([Formatter? formatter]) {
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new util methods

///
/// Unless [verbose] is true, the list of inputs will be abbreviated to avoid an
/// unnecessarily long log.
void logCommand(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rest is formatting below here

@@ -13,10 +13,10 @@ import '../utils/assert_no_positional_args_nor_args_after_separator.dart';
/// Use [DevTool.fromFunction] to create [FunctionTool] instances.
class FunctionTool extends DevTool {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only formatting changes in this file

@@ -28,19 +28,22 @@ class OverReactFormatTool extends DevTool {
Iterable<String> paths = context.argResults?.rest ?? [];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only formatting changes in this file

@@ -27,12 +27,15 @@ final _log = Logger('Process');
/// It is also possible to run this tool directly in a dart script:
/// ProcessTool(exe, args).run();
class ProcessTool extends DevTool {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only formatting changes in this file

@@ -50,48 +50,66 @@ class TestTool extends DevTool {
@override
final ArgParser argParser = ArgParser()
..addSeparator('======== Selecting Tests')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only formatting changes in this file

final ArgParser argParser = ArgParser()
..addFlag('ignore-infos', help: 'Ignore any info level issues.');

@override
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only formatting changes in this file

@@ -64,27 +64,40 @@ class WebdevServeTool extends DevTool {

@override
final ArgParser argParser = ArgParser()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only formatting changes in this file

@@ -0,0 +1,4 @@
name: has_dart_style_v2
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new test fixture

@@ -0,0 +1,4 @@
name: has_dart_style_v3
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new test fixture

orderedEquals(['a', 'b', '-w']));
expect(
buildArgs(['a', 'b'], FormatMode.overwrite),
orderedEquals(['a', 'b', '-w']),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test with -w

);
});

test('mode=overwrite without write arg', () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test without -w

);
});

test('adds latest language version flag when configured', () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 tests to test the new language version config

);
});

test('adds latest language version flag when configured', () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 new tests

expect(execution.directiveOrganization, isNull);
});

test(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new tests for dart_style

final execution = buildExecution(
context,
configuredFormatterArgs: ['--fix', '--follow-links'],
formatter: Formatter.dartFormat,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updates to test for language version under dart 3

test('<=5 inputs and verbose=false', () async {
expect(Logger.root.onRecord,
emitsThrough(infoLogOf(contains('dartfmt -x -y a b'))));
expect(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format changes below

);
});

test('detects languageVersion', () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new test

String formatToolCascadeSrc({String formatter = 'dartfmt'}) =>
String formatToolCascadeSrc({
String formatter = 'dartfmt',
String? languageVersion,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add language version

);
});

test(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new test

@@ -0,0 +1,3 @@
void main() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new test fixture

);
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get the language version from config

failedToReconstructFormatterArgs,
failedToParseFormatterArgs,
failedToParseLineLength,
failedToParseLanguageVersion,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new error

}
}
return null;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new util function

@robbecker-wf robbecker-wf marked this pull request as ready for review March 16, 2026 20:37
@robbecker-wf robbecker-wf requested a review from a team as a code owner March 16, 2026 20:37
@robbecker-wf robbecker-wf changed the title Handle removed dart style cli option Update FormatTool. -w bug fix and support language version Mar 16, 2026
@robbecker-wf
Copy link
Member Author

QA +1 CI passes on linux/windows and dart 2 and 3
manual testing shows it works. Was able to format a dart 2 codebase under dart 3.
@Workiva/release-management-p

Copy link
Contributor

@rmconsole-wf rmconsole-wf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 from RM

@btr-rmconsole-2 btr-rmconsole-2 bot merged commit 09dfe3e into master Mar 16, 2026
7 checks passed
@btr-rmconsole-2 btr-rmconsole-2 bot deleted the handle_removed_dart_style_cli_option branch March 16, 2026 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants