Skip to content
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

feat(sage-monorepo): configure Prettier as the default formatter #2793

Merged

Conversation

tschaffter
Copy link
Member

@tschaffter tschaffter commented Sep 9, 2024

Closes #2789
Fixes #2797
Depends on #2796

Description

  1. Configure Prettier as the default formatter with format on save enabled in VS Code
  2. Ensure that formatting files on save in VS Code and applying Prettier with its CLI generate the same results
  3. Format all the files in the monorepo with Prettier (except Python files)

Changelog

  • Configure Prettier as the default formatter for all file types except Python
  • Fix a bug where VS Code ignores the default formatter (JSON and YAML files)
  • Add Prettier-java plugin
  • Remove Spotless and all its references
  • Patch nx format to support the Java plugin for Prettier
  • Format all the files in the monorepo with Prettier
  • Remove React packages that suddenly conflicted with *api-docs:build tasks

Future work

  • Require files to be formatted before committing (using pre-commit git hook)

References

Preview

Check the format of the files considered as affected by Nx compared to the main branch. Nx allows to pass options to modify this default behavior, e.g. to check only uncommitted files. Using Nx's format command allows to save time compared to running directly Prettier on all the workspace files.

nx format:check

Format the affected files:

nx format

@tschaffter
Copy link
Member Author

tschaffter commented Sep 9, 2024

nx format:check does not detect changes to Java files

Consider these two files that had typos added to their format:

modified:   apps/openchallenges/app/src/main.ts
modified:   apps/openchallenges/challenge-service/src/main/java/org/sagebionetworks/openchallenges/challenge/service/ChallengeServiceApplication.java

Nx sees that the TS file do not pass the check:

$ nx format:check --files apps/openchallenges/app/src/main.ts --verbose

 NX   The following files are not formatted correctly based on your Prettier configuration

- Run "nx format:write" and commit the resulting diff to fix these files.
- Please note, Prettier does not support a native way to diff the output of its check logic (https://github.com/prettier/prettier/issues/6885).

apps/openchallenges/app/src/main.ts
$ echo $?
1

I can also confirm that the following file types are correctly picked up by nx format:check:

  • YAML

But it fails to check the format when pointed to the Java file:

$ nx format:check --files apps/openchallenges/challenge-service/src/main/java/org/sagebionetworks/openchallenges/challenge/service/ChallengeServiceApplication.java --verbose
$ echo $?
0

Here is the Nx format script:

Here is the Prettier command that nx format:check will run. The above Java file is not included.

node "/workspaces/sage-monorepo/node_modules/.pnpm/[email protected]/node_modules/prettier/bin/prettier.cjs" --list-different ".vscode/settings.json" "apps/model-ad/api/project.json" "apps/openchallenges/challenge-service/project.json" "apps/openchallenges/challenge-to-elasticsearch-service/project.json" "apps/openchallenges/config-server/project.json" "apps/openchallenges/image-service/project.json" "apps/openchallenges/kaggle-to-kafka-service/project.json" "apps/openchallenges/organization-service/project.json" "apps/openchallenges/service-registry/project.json" "apps/openchallenges/user-service/project.json" "docs/_archive/linters-and-formatters.md" "libs/openchallenges/app-config-data/project.json" "libs/openchallenges/kafka-admin/project.json" "libs/openchallenges/kafka-consumer/project.json" "libs/openchallenges/kafka-producer/project.json" "package.json" "pnpm-lock.yaml" "apps/openchallenges/app/src/main.ts" "apps/openchallenges/challenge-service/src/main/resources/application.yml" "mkdocs.yml" ".prettierrc.yml"

The issue is because prettier.getSupportInfo() does not see the Prettier plugin for Java:

Solution

I created a patch with these steps: https://pnpm.io/cli/patch

See the files changed of this PR for the actual patch.

@tschaffter
Copy link
Member Author

Removing Prettier plugin for eslint

        "prettier/prettier": [
          "error",
          {
            "tabWidth": 2,
            "useTabs": false,
            "singleQuote": true,
            "printWidth": 80
          }
        ],

Copy link

dpulls bot commented Sep 10, 2024

🎉 All dependencies have been resolved !

@tschaffter tschaffter marked this pull request as ready for review September 10, 2024 15:43
@tschaffter tschaffter merged commit 9bfc0e9 into Sage-Bionetworks:main Sep 10, 2024
13 of 14 checks passed
@tschaffter tschaffter deleted the sage-monorepo/format-with-prettier branch September 10, 2024 19:18
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

Successfully merging this pull request may close these issues.

[Bug] Tasks *api-docs:build fail [Story] Check ESLint consistency between IDE and CLI output
1 participant