fix(core): fix formatFiles schematic to write changes to correct work… #7944
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
…space config file
Current Behavior
formatFiles
schematic and makes changes to bothangular.json
andworkspace.json
, the changes toworkspace.json
will be written to disk to the correct config file but not the changes toangular.json
. For example, if a schematic creates a library using@nrwl/workspace:library
and then runsformatFiles
, the changes for the library configuration will not be written toangular.json
.@rarmatei has a reproduction repo in #7863
Here is the sequence of events leading to this issue as I understand them:
The the example repo built by @rarmatei , the
data-access-lib
schematic calls the@nrwl/workspace:lib
schematic which correctly updates theangular.json
file in theTree
.The schematic then calls the
formatFiles
schematic. Part of this schematic includes a function namedupdateWorkspaceJsonToMatchFormatVersion
which takes care of updating the workspace configuration to match the format of the version it's using. This function incorrectly decides thatworkspace.json
is the workspace configuration file and overwrites it in the tree with a formatted version. This incorrect logic is due to bothworkspace.json
andangular.json
being present in the schematic'sTree
. At this point, bothangular.json
andworkspace.json
may have been modified. If the format is already correct, the actual contents of theworkspace.json
file may not have been modified.formatFiles
then continues to pick up any modified files in theTree
and runs Prettier over them. At this point, bothangular.json
andworkspace.json
are flagged for changes and Prettier is run on them. This leavesangular.json
with the lib settings andworkspace.json
with only formatting changes.The changes are then ready to be committed to disk. The
NxScopedHost
class takes care of this to map the changes from the schematic'sTree
to the actual file system. It correctly determines that the workspace config file isangular.json
. Two write commands are submitted to it, one forangular.json
andworkspace.json
. Both of these write commands are mapped to write to theangular.json
file. Becauseworkspace.json
is written last, it wins, andangular.json
is overwritten with just the formatting changes instead of the new library config.@AgentEnder and I discussed that the root cause may be somewhere in the
ngcli-adapter.js
file. I'm not sure if that's the case any more. It seems to be correctly determining the right configuration file to write to, but it's being given confusing write commands. By the time the two different write commands reachNxScopedHost
, is there enough information to actually tell which update is "right?" Should it merge them together? This seems difficult to do by the time you write to disk, but I'm happy to be wrong about that. It seems like you would want to stop something like this earlier, like in the actualTree
class so that it won't recordwrite
actions for bothangular.json
andworkspace.json
and instead would pick the right one.This PR fixes the
updateWorkspaceJsonToMatchFormatVersion
function that is used by theformatFiles
schematic. There's another version of this function inpackages/workspace/src/command-line/format.ts
used by the command line that correctly picks up the configuration file. Instead of using theTree
, it uses the file system to determine the file name of the configuration file. We can then ensure that changes are only ever requested on the correct workspace config file.Expected Behavior
It should properly format the file without reverting the changes.
Related Issue(s)
Fixes #7863