Skip to content

Conversation

@thkim1011
Copy link
Contributor

@thkim1011 thkim1011 commented Aug 8, 2022

See title. This adds a --format flag to the flutter gen-l10n command. We do this by returning the list of generated files from LocalizationsGenerator.writeOutputFiles and doing post processing from the generate_localizations.dart file. This should also be the way of adding any additional post processing steps to the gen-l10n command going forward.

Also, something else to note is that all the tests in generate_localizations_test.dart has been calling the command twice. I didn't think that the check for exitStatus was necessary, so I removed them.

Fixes #98122

Relevant PRs: #108563, #98701.

@flutter-dashboard flutter-dashboard bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Aug 8, 2022
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@thkim1011
Copy link
Contributor Author

@christopherfujino @HansMuller can I get a quick look at the changes just to make sure this makes sense before I go and update generate_localizations_test.dart?

Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

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

LGTM

);
argParser.addFlag(
'format',
help: 'When specified, the `dart format` command is run after generating the files.'
Copy link
Contributor

Choose a reason for hiding this comment

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

"the files" is OK, since the reader is unlikely to think that we're referring to .arb files (or whatever), but it might be a clearer to clarify this a little.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

@cedvdb
Copy link
Contributor

cedvdb commented Aug 9, 2022

I'm confused why this is opt-in and even optional. In what situation wouldn't you need the file to be formatted like the rest of the project ? How will this play out with vs code auto gen of localization on save ?

@christopherfujino
Copy link
Contributor

I'm confused why this is opt-in and even optional. In what situation wouldn't you need the file to be formatted like the rest of the project ? How will this play out with vs code auto gen of localization on save ?

  1. if you don't use dart format on your codebase
  2. if we don't make this opt-in, it will be a breaking change to customers who check in their generated files.


final FileSystem _fileSystem;
final Logger _logger;
final Artifacts? _artifacts;
Copy link
Contributor

Choose a reason for hiding this comment

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

does this need to be nullable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, since globals.artifacts is nullable.

}
final String dartBinary = _artifacts!.getHostArtifact(HostArtifact.engineDartBinary).path;
final List<String> command = <String>[dartBinary, 'format', ...outputFileList];
final Process process = await _processManager.start(command);
Copy link
Contributor

Choose a reason for hiding this comment

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

If you don't need to interact with the sub-process, calling .run() is cleaner:

final ProcessResult result = await _processManager.run(command);
if (result.exitCode != 0) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

logger: globals.logger,
artifacts: globals.artifacts,
processManager: globals.processManager,

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

Copy link
Contributor

@christopherfujino christopherfujino left a comment

Choose a reason for hiding this comment

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

This approach looks good to me

@cedvdb
Copy link
Contributor

cedvdb commented Aug 9, 2022

  1. if you don't use dart format on your codebase
  2. if we don't make this opt-in, it will be a breaking change to customers who check in their generated files.

How can formatting be a breaking change ? It stills obeys whatever analys options.yaml that's there right ? so any CI dart analyse will still pass

  • how will auto generation be handled here ? (ctrl + s instead of running the command)

@thkim1011
Copy link
Contributor Author

Not necessarily breaking, but it would be a bit annoying/confusing having to commit unexpected changes due to formatting changes.

For automatic generation, I'll add a format option to the yaml config.

@AlexV525
Copy link
Member

I think #108563 has already done what this PR has done. And we have another test case issue with test timeout that need to be resolved. Also, @thkim1011 is on the review list.

} on L10nException catch (e) {
throwToolExit(e.message);
// All other post processing.
if (boolArg('format') ?? false) {
Copy link
Member

Choose a reason for hiding this comment

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

This will only apply to the format when using flutter tools directly. Generalizing is also included in building targets.

@AlexV525
Copy link
Member

  1. if you don't use dart format on your codebase

If you don't, #103414 should also be considered as a breaking change.

  1. if we don't make this opt-in, it will be a breaking change to customers who check in their generated files.

Generated files can be affected only once after they update their SDK. We do have a couple of patches during SDK updates like ignoring frame rates limitation when building iOS apps.

@cedvdb
Copy link
Contributor

cedvdb commented Aug 10, 2022

Not necessarily breaking, but it would be a bit annoying/confusing having to commit unexpected changes due to formatting changes.

That's only valid for the first localization gen and only because it did not originally obey formatting rules.

@thkim1011
Copy link
Contributor Author

I think #108563 has already done what this PR has done. And we have another test case issue with test timeout that need to be resolved. Also, @thkim1011 is on the review list.

@AlexV525 is there a specific reason why we should pass in artifacts and processManager into LocalizationsGenerator and doing the formatting within the LocalizationsGenerator class in your PR? I think this is the only difference between our PRs? I feel that any sort of post processing on the generated files don't really belong in the LocalizationsGenerator class, but I'm not completely opposed to your PR either.

fileSystem = MemoryFileSystem.test();
logger = BufferLogger.test();
artifacts = Artifacts.test();
processManager = const LocalProcessManager();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this running real sub-processes? If so, it will need a real file system, and should be moved to the commands.shard/permable directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed this so that we use a FakeProcessManager instead.

@AlexV525
Copy link
Member

I feel that any sort of post processing on the generated files don't really belong in the LocalizationsGenerator class

IMO it belongs to the generator, something like who generates them should format them. In #108563 the generator obtain the artifacts and the process manager not only from the context runner, but also from a build target's environment.

throwToolExit(e.message);
// All other post processing.
if (format) {
if (_artifacts == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this would be a programming error, no? When is artifacts null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha, I just checked the rest of the commands in executable.dart and they seem to just pass in artifacts as artifacts! so I'll do that.

Copy link
Contributor

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

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

LGTM with nit

@AlexV525
Copy link
Member

I don't see any tests that verified if generated files are well-formatted.

@jonahwilliams
Copy link
Contributor

Whether or not a file is formatted is something that dartfmt knows, but its not the tools responsibility to test. If we've tested that dartfmt has been invoked that is good enough for me

@thkim1011 thkim1011 merged commit 646666f into flutter:master Aug 19, 2022
@thkim1011 thkim1011 deleted the tae/add-format branch August 19, 2022 21:03
github-merge-queue bot pushed a commit that referenced this pull request Apr 30, 2025
…#167029)

Migration of Localization Generation [Option
1](https://docs.flutter.dev/release/breaking-changes/flutter-generate-i10n-source#migration-guide)
breaks `dart format . --set-exit-if-changed` checks (as commonly used in
pipelines), as the generated file does not comply the `dart format`
rules.

This PR:
- Formats the generated localization files by default
- In contrast to [this
comment](#109171 (comment))
I don't see it as breaking as the files are generated dynamically on
every change and there's no need in migrating them. Also any tests
should not fail as either the format was done manually afterwards
anyways, or the formatting was not checked at all (in third party apps /
libraries) as the output had not a correct format in any use case of
checking via `dart format`.
- Add tests for `no-format`
- Add option to add arbitrary arguments to the `dart format` command to
allow customized formatting

Closes #167982
Contributes to:
#102983 (comment)

*If you had to change anything in the [flutter/tests] repo, include a
link to the migration guide as per the [breaking change policy].*

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [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.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[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
flutteractionsbot pushed a commit to flutteractionsbot/flutter that referenced this pull request Apr 30, 2025
…102983) (flutter#167029)

Migration of Localization Generation [Option
1](https://docs.flutter.dev/release/breaking-changes/flutter-generate-i10n-source#migration-guide)
breaks `dart format . --set-exit-if-changed` checks (as commonly used in
pipelines), as the generated file does not comply the `dart format`
rules.

This PR:
- Formats the generated localization files by default
- In contrast to [this
comment](flutter#109171 (comment))
I don't see it as breaking as the files are generated dynamically on
every change and there's no need in migrating them. Also any tests
should not fail as either the format was done manually afterwards
anyways, or the formatting was not checked at all (in third party apps /
libraries) as the output had not a correct format in any use case of
checking via `dart format`.
- Add tests for `no-format`
- Add option to add arbitrary arguments to the `dart format` command to
allow customized formatting

Closes flutter#167982
Contributes to:
flutter#102983 (comment)

*If you had to change anything in the [flutter/tests] repo, include a
link to the migration guide as per the [breaking change policy].*

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [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.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[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
romanejaquez pushed a commit to romanejaquez/flutter that referenced this pull request Aug 14, 2025
…102983) (flutter#167029)

Migration of Localization Generation [Option
1](https://docs.flutter.dev/release/breaking-changes/flutter-generate-i10n-source#migration-guide)
breaks `dart format . --set-exit-if-changed` checks (as commonly used in
pipelines), as the generated file does not comply the `dart format`
rules.

This PR:
- Formats the generated localization files by default
- In contrast to [this
comment](flutter#109171 (comment))
I don't see it as breaking as the files are generated dynamically on
every change and there's no need in migrating them. Also any tests
should not fail as either the format was done manually afterwards
anyways, or the formatting was not checked at all (in third party apps /
libraries) as the output had not a correct format in any use case of
checking via `dart format`.
- Add tests for `no-format`
- Add option to add arbitrary arguments to the `dart format` command to
allow customized formatting

Closes flutter#167982
Contributes to:
flutter#102983 (comment)

*If you had to change anything in the [flutter/tests] repo, include a
link to the migration guide as per the [breaking change policy].*

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [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.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[gen-l10n] Generated l10n files are not formatted with dart format

6 participants