nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
22.68k stars 2.26k forks source link

Weird behavior of nx format:check / format:write #13675

Open DanielKucal opened 1 year ago

DanielKucal commented 1 year ago

Current Behavior

So it all started when I was setting up CI on Azure DevOps starting from official example, which includes npx nx format:check command. It appeared to be very troublesome, because initially it was throwing errors for .yml, .yaml, .md, .json files and even for .prettierrc 🤔 After ignoring all those file types and running (npx) nx format:write to verify everything works as it should, I can still see that tsconfig.base.json file is formatted. And here the really weird part begins: I have *.json ignore pattern in .prettierignore, also tried with **.json and **/*.json and even added tsconfig.base.json explicitly. It hasn't help. Then I added

    {
      "files": "*.json",
      "excludeFiles": ["tsconfig.base.json"]
    },

to .prettierrc "overrides" section - still the same! tsconfig.base.json is not being excluded 😱 And the worst part is npx nx format:check command fails on CI (ubuntu-latest) while working on my computer (MacOS) I went through https://github.com/nrwl/nx/issues/5611 and https://github.com/nrwl/nx/issues/4159 and saw that people have similar problems with the format:check command on CI, but haven't found any working solution...

Expected Behavior

I'd expect both format:check and format:write commands to take my ignore statements into account.

Github Repo

No response

Steps to Reproduce

  1. Have inline libs definition"lib": ["es2017", "dom"], in tsconfig.json
  2. Ignore all *.json files
  3. Run (npx) nx format:write

Nx Report

% nx report

 >  NX   Running global Nx CLI with PNPM may have issues.

   Prefer to use "pnpm" (https://pnpm.io/cli/exec) to execute commands in this workspace.
    TIP  create a shortcut such as: alias pnx="pnpm nx --"

 >  NX   Report complete - copy this into the issue template

   Node : 16.14.2
   OS   : darwin x64
   pnpm : 7.18.1

   nx : 15.2.4
   @nrwl/angular : 15.2.4
   @nrwl/cypress : 15.2.4
   @nrwl/detox : Not Found
   @nrwl/devkit : Not Found
   @nrwl/esbuild : Not Found
   @nrwl/eslint-plugin-nx : 15.2.4
   @nrwl/expo : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 15.2.4
   @nrwl/js : Not Found
   @nrwl/linter : 15.2.4
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : Not Found
   @nrwl/nx-cloud : 15.0.2
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/rollup : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : 15.2.4
   @nrwl/web : Not Found
   @nrwl/webpack : Not Found
   @nrwl/workspace : 15.2.4
   typescript : 4.8.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:
         @storybook/angular: 6.5.14

Failure Logs

Starting: CmdLine
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.212.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
Script contents:
npx nx format:check
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/60256e61-ab93-4c8a-9298-823f89b157c1.sh
fatal: Not a valid object name main
fatal: No such ref: 'main'
apps/host-e2e/project.json
apps/host/project.json
decorate-angular-cli.js
libs/core/.eslintrc.json
libs/core/project.json
libs/core/tsconfig.lib.json
libs/core/tsconfig.spec.json
libs/ui/.eslintrc.json
libs/ui/.storybook/tsconfig.json
libs/ui/project.json
libs/ui/tsconfig.lib.json
libs/ui/tsconfig.spec.json
nx.json
##[error]Bash exited with code '1'.
Finishing: CmdLine

EDIT: after excluding all those files and running "Reformat with prettier" on decorate-angular-cli.js, the command finally worked (now it fails on the next one). Still running nx format:write failed to solve the issue.

Additional Information

No response

minuz commented 1 year ago

@DanielKucal By any chance you have developers running windows and macos? It might be an issue with line endings...

UPDATE: https://prettier.io/docs/en/options.html#end-of-line https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings

DanielKucal commented 1 year ago

@minuz we're all on MacOS

hpfs74 commented 1 year ago

Any update on this?

zestsystem commented 1 year ago

Having this issue. I'm on MacOS as well.

Ionshard commented 1 year ago

Yes, I have found no way to ignore files from nx format:check --all we have a project that generates API code from a swagger.json and all the files are .gitignored and yet they still fail the format check. I have tried adding them to .nxignore as well but they continue to fail.

connorjs commented 8 months ago

Maybe unrelated, but here it goes.

I was looking at the nx format implementation due to the lack of documentation on it. (Should I use it or keep using my own format command with prettier.)

I noticed two things, both may relate to this issue.

  1. It directly executes prettier without an option for --ignore-path which I use for .gitignore. Note: Maybe this is fine b/c it’s using the Nx graph.

  2. It has specific TS config logic in the implementation, which may not be symmetric (check and write different).

Again, unsure if this helps, but maybe y’all could scan through the implementation (it’s short) and see if that gives you insight to the issue you’re seeing.

warrendugan commented 5 months ago

.vscode > settings.json

{
  "editor.formatOnSave": true,
  "[json]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
}

I've found adding this helps VS Code understand what I want it to do. I'm not entirely sure this solves your problem but my problem landed me here and I found a solution so I wanted to share.

My issue was CTRL + S would override what nx format:write was doing and vice versa