Skip to content

Unexpectedly formatting in new style #1617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
goderbauer opened this issue Dec 6, 2024 · 2 comments
Closed

Unexpectedly formatting in new style #1617

goderbauer opened this issue Dec 6, 2024 · 2 comments

Comments

@goderbauer
Copy link

goderbauer commented Dec 6, 2024

Given the following setup:

myproject/pubspec.yaml:

name: test
version: 0.0.1
publish_to: none

environment:
  sdk: ">=3.0.0 <4.0.0"

myproject/lib/foo.dart:

enum foo {
  entry1,
  entry2,
}

I would have expected that running dart format . in the myproject directory formats foo.dart in the old style without removing/adding any trailing commas because the lower SDK bound in the pubspec.yaml is below 3.7. However, it actually uses the new format style and removes the trailing comma:

enum foo { entry1, entry2 }

Observation: After running dart pub update the formatter actually respects the given sdk constraints and uses the old style:

enum foo {
  entry1,
  entry2,
}

This caused an actual breakage in flutter's assets-for-api-docs repository where the CI was all of the sudden (without bumping the lower SDK constraints) expecting everything to be formatted in the new style: https://github.com/flutter/assets-for-api-docs/actions/runs/11847189657/job/33016375039

Expectation: The formatter uses the correct style based on the sdk constrains in the pubspec.yaml even if dart pub update hasn't been run to avoid breaking CI this way.

I observed this behavior with the formatter from Dart SDK version 3.7.0-219.0.dev.

If it doesn't reproduce: delete the .dart_tools directory and pubpsec.lock file.

@goderbauer
Copy link
Author

cc @Piinks, who noticed this breakage
cc @munificent

@munificent
Copy link
Member

Oof, yeah, this is a nasty UX pitfall. I'm going to close this as a duplicate of #1597 which has a lot more context.

Sorry for the breakage. I'm still trying to figure out the best way for the formatter to help users avoid this problem without adding unbounded complexity to how it interacts with the file system.

@munificent munificent closed this as not planned Won't fix, can't repro, duplicate, stale Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants