microsoft / rushstack

Monorepo for tools developed by the Rush Stack community
https://rushstack.io/
Other
5.82k stars 593 forks source link

[heft] Not clear how to replace/unset inherited arrays from "extends" config files #4125

Open DavidRieman opened 1 year ago

DavidRieman commented 1 year ago

Summary

In a custom-jest.config.json we can use "extends" to inherit a lot of good defaults as needed for rushstack. However, it's not clear how to replace a property like testMatch; instead the arrays are merged together, forcing us to run undesired tests. It's also not clear how to just unset such a property - for example, the testRegex and testMatch properties are mutually exclusive, so projects inheriting from the base config cannot choose to use testRegex at all.

Repro steps

Expected: No tests run. Results: All the usual tests run.

Details

This makes it very hard to use a multiple-config strategy to separate, say, unit tests from integration tests, while still leveraging the jest-shared.config.json.

To me it seems weird that trying to override a property merges parts of the property instead. It took deep debugging of jest to figure out why the tests were still running. I have to wonder how many other subtle bugs arise from these issues, or if I'm somehow in the minority for thinking a { prop: [] } would override the base { prop: [] }.

That said, if I'm scanning through the right code (ConfigurationFile.ts?) there does appear to be code trying to make choices about what things to merge and what to replace/etc. It's not clear to me ATM whether it is possible/expected for us to control these behaviors from somewhere. If so, could someone point me to the documentation?

Standard questions

Please answer these questions to help us investigate your issue more quickly:

Question Answer
@rushstack/heft version? ^0.48.7
Operating system? Windows
Would you consider contributing a PR? Yes if trivial
Node.js version (node -v)? v16.15.0
iclanton commented 1 year ago

This needs to be documented better, but you can include a magic property in the child file (the one that has the "extends" property) that looks like this "$testRegex.inheritanceType": "replace" that will change the inheritance behavior from "append" to "replace" for that property.

There currently isn't a way to ignore a property from a parent config file, but it wouldn't be too difficult to add another case to that property.

DavidRieman commented 1 year ago

"$testMatch.inheritanceType": "replace" worked for me. We can consider this a documentation bug since it is a useful but undiscoverable feature.

octogonz commented 1 year ago

Initial documentation is now in the heft.json property inheritance directives section from our What's New in Heft 0.51 blog post.

I will start on the heft website doc updates next week.

GongT commented 2 weeks ago

I can't add other fields (I want add "$phaseDependencies.inheritanceType": "append",), can not pass json-schema check:

Error: Resolved configuration object does not match schema: Error: JSON validation failed:
/data/DevelopmentRoot/github.com/gongt/baobao/@internal/dualstack-rig/build/heft.json

Error: #/phasesByName/build
       must NOT have additional properties: $phaseDependencies.inheritanceType