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

nx format:write deletes tsconfig.base.json comments #28484

Open
1 of 4 tasks
privatenumber opened this issue Oct 17, 2024 · 0 comments
Open
1 of 4 tasks

nx format:write deletes tsconfig.base.json comments #28484

privatenumber opened this issue Oct 17, 2024 · 0 comments

Comments

@privatenumber
Copy link

privatenumber commented Oct 17, 2024

Current Behavior

If my root tsconfig contains comments, running nx format:write deletes them.

Expected Behavior

  1. For the comments to be preserved. This is a pretty serious concern because users may lose their work if they run nx format:write prior to committing. This is very dangerous.

  2. I think this should also be up to the appropriate codebase's formatting rules and Nx shouldn't enforce un-configurable formatting rules. And I believe with Nx 20, there's less emphasis on using tsconfig to resolve peer packages, so this feels unnecessary.

  3. If Nx really wants to impose formatting... Formatting is an extremely complex task and it should be delegated to the appropriate tools that have been thoroughly tested. For example, eslint-plugin-jsonc handles JSON key sorting well while preserving comments.

Also, it should be documented in the format:write docs that it imposes a sorting order to your tsconfig that's unconfigurable.

GitHub Repo

No response

Steps to Reproduce

  1. Add comments to the root tsconfig file (e.g. tsconfig.base.json)
  2. Run nx format:write
  3. Observe that the comments in the tsconfig file get deleted

Nx Report

Not relevant

Failure Logs

No response

Package Manager Version

N/A

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

This is happening because of this:

function sortTsConfig() {
try {
const tsconfigPath = getRootTsConfigPath();
const tsconfig = readJsonFile(tsconfigPath);
const sortedPaths = sortObjectByKeys(tsconfig.compilerOptions.paths);
tsconfig.compilerOptions.paths = sortedPaths;
writeJsonFile(tsconfigPath, tsconfig);
} catch (e) {
// catch noop
}
}

It reads the tsconfig JSON, parses it with a JSONC parser (which doesn't retain comment data), sorts it, and overrides the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant