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

Fix incorrect config option for Node.js API with extends and overrides #8030

Merged
merged 3 commits into from
Oct 9, 2024

Conversation

ybiquitous
Copy link
Member

@ybiquitous ybiquitous commented Oct 9, 2024

Which issue, if any, is this issue related to?

Closes #8014
(This bug was introduced in #7826)

Is there anything in the PR that needs further explanation?

The bug's root cause is present in caching in the getConfigForFile() function. The current logic depends on the overrides property like this:

// If config has overrides the resulting config might be different for some files.
// Cache results only if resulted config is the same for all linted files.
if (cached && !optionsConfig.overrides) {
return cached;
}

But, in the case that overrides is only in nested config files, optionsConfig.overrides is falsy, and then an incorrect cached config is returned unexpectedly, for example:

standalone({
  config: {
    extends: './with-overrides.js',
    rules: {},
  },
});
// with-overrides.js
export default {
  overrides: [ 
    { 
      files: ["*.css"],
      rules: {
        'block-no-empty': true,
      },
    },
  ],
};

So, this PR uses optionsConfig and filePath for caching keys, instead of optionsConfig.overrides. For that purpose, we have to change the structure of the cache data like this:

- Map<Config, Promise<CosmiconfigResult>>
+ Map<Config, Map<string, CosmiconfigResult>>

This comment was marked as resolved.

const { results } = await standalone({
files: [path.join(fixturesPath, 'style.css')],
config: {
extends: path.join(fixturesPath, 'extends-in-overrides.json'),
Copy link
Member Author

@ybiquitous ybiquitous Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] Here's the content of extends-in-overrides.json:

{
"overrides": [
{
"files": ["*.css"],
"extends": "./extending-simple-rule.json"
}
]
}

And extending-simple-rule.json:

{
"extends": "./simple-rule",
"rules": {
"block-no-empty": true
},
"overrides": [
{
"files": ["*.css"],
"rules": {
"color-named": "never"
}
}
]
}

@ybiquitous ybiquitous changed the title Fix config option for Node.js API with extends and overrides Fix incorrect config option for Node.js API with extends and overrides Oct 9, 2024
@ybiquitous ybiquitous marked this pull request as ready for review October 9, 2024 14:58
Copy link
Member

@romainmenke romainmenke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great @ybiquitous
Thank your for investigating and finding a solution 🙇

@ybiquitous ybiquitous merged commit ab2141e into main Oct 9, 2024
17 checks passed
@ybiquitous ybiquitous deleted the issue-8014 branch October 9, 2024 15:44
@ybiquitous
Copy link
Member Author

ybiquitous commented Oct 9, 2024

@romainmenke Thanks for the quick review!

I just triggered the workflow in the ecosystem test, so let's see the result!
https://github.com/stylelint/stylelint-ecosystem-tester/actions/runs/11258475686

EDIT: The test is now passed! 😄

@romainmenke
Copy link
Member

Awesome! Thank you for working on this bug!

if (optionsConfig !== undefined) {
const cached = stylelint._specifiedConfigCache.get(optionsConfig);
if (optionsConfig) {
const filePathAsCacheKey = filePath ?? '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question

Is it safe as a fallback cache key?
i.e. I would have expected a test in getConfigForFile.test.mjs with filePath === undefined.

I am not sure if cachedForFiles.set(filePathAsCacheKey, augmentedResult); should be set if filePathAsCacheKey === ''.

Copy link
Member Author

@ybiquitous ybiquitous Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's safe. Such a test isn't always necessary since getConfigForFile() calls without filePath are already performed at various places. Other higher-level test cases cover. Of course, I'm not against having a case in getConfigForFile.test.mjs too.

https://github.com/search?q=repo%3Astylelint%2Fstylelint+%22getConfigForFile%28%22+path%3A*.mjs&type=code

We can see also the coverage passes the line of cachedForFiles.set():

https://app.codecov.io/gh/stylelint/stylelint/commit/3fca68706b744b18df53a2691dbd164508cd5d32/blob/lib/getConfigForFile.mjs

Copy link
Member

@Mouvedia Mouvedia Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are sure that we can't have a case where we get the wrong cached from cachedForFiles.get('') because filePath was undefined more than once for getConfigForFile calls with different stylelint._options.config, then we can move on.
I would need to investigate further to tell if it can actually happen.
It seemed unsafe to me at first glance.

@Mouvedia

This comment was marked as resolved.

@ybiquitous

This comment was marked as resolved.

@Mouvedia
Copy link
Member

Mouvedia commented Oct 11, 2024

Since it is now using filePath instead of stylelint._options.config as the key, it means that the
const postcssResult = await lintSource(stylelint, { in the const getStylelintResults = absoluteFilePaths.map(async (absoluteFilepath) => { loop of standalone.mjs won't get the cached "augmented" config for the second file and forward because we get a new filePathAsCacheKey for each iteration.
i.e. it will run const augmentedResult = await augmentConfigFull(stylelint, … and skip if (cached) { which wasn't the original purpose of that if IMO

configForFile = await getConfigForFile(stylelint, configSearchPath, inputFilePath);

What I am getting at is that 16.10.0 is probably slower than 16.9.0 if you have pass more than one file as CLI parameters.
I think the cache key should be something common to all the files being passed.

@ybiquitous Am I correct? Or am I missing something obvious?

renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Oct 12, 2024
| datasource | package   | from   | to      |
| ---------- | --------- | ------ | ------- |
| npm        | stylelint | 16.9.0 | 16.10.0 |


## [v16.10.0](https://github.com/stylelint/stylelint/blob/HEAD/CHANGELOG.md#16100)

-   Changed: `shorthand-property-no-redundant-values` reported ranges ([#8008](stylelint/stylelint#8008)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Added: `formatter` configuration property ([#7826](stylelint/stylelint#7826)) ([@emmacharp](https://github.com/emmacharp)).
-   Added: `validate` option to Node.js API and `--validate` CLI flag ([#8009](stylelint/stylelint#8009)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Added: fixed section to the output of the `verbose` formatter ([#7985](stylelint/stylelint#7985)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Added: support for `string | RegExp` to `ignoreValues` option of `value-no-vendor-prefix` ([#7969](stylelint/stylelint#7969)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Added: support for `string` to `ignoreLonghands` option of `declaration-block-no-redundant-longhand-properties` ([#7971](stylelint/stylelint#7971)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Fixed: `at-rule-no-unknown` false positives for `@position-try` ([#7968](stylelint/stylelint#7968)) ([@yelizsevinc](https://github.com/yelizsevinc)).
-   Fixed: `declaration-block-no-redundant-longhand-properties` autofix for `grid-column`/`grid-row` ([#8023](stylelint/stylelint#8023)) ([@ryo-manba](https://github.com/ryo-manba)).
-   Fixed: `declaration-property-value-no-unknown` false negatives/positives via [`[email protected]`](https://github.com/csstree/csstree/releases/tag/v3.0.0) ([#7993](stylelint/stylelint#7993)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Fixed: `declaration-property-value-no-unknown` reported ranges and message ([#8003](stylelint/stylelint#8003)) ([@romainmenke](https://github.com/romainmenke)).
-   Fixed: `keyframe-selector-notation` reported ranges ([#7992](stylelint/stylelint#7992)) ([@romainmenke](https://github.com/romainmenke)).
-   Fixed: `length-zero-no-unit` false positives for Sass variables ([#8035](stylelint/stylelint#8035)) ([@ryo-manba](https://github.com/ryo-manba)).
-   Fixed: `no-descending-specificity` reported ranges ([#7996](stylelint/stylelint#7996)) ([@romainmenke](https://github.com/romainmenke)).
-   Fixed: `selector-anb-no-unmatchable` reported ranges and message ([#8001](stylelint/stylelint#8001)) ([@romainmenke](https://github.com/romainmenke)).
-   Fixed: `string` formatter handling of log symbols for non-Unicode terminals ([#7981](stylelint/stylelint#7981)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Fixed: built `.cjs` files to address Rollup vulnerability ([#8010](stylelint/stylelint#8010)) ([@ybiquitous](https://github.com/ybiquitous)).
-   Fixed: incorrect `config` option for Node.js API with `extends` and `overrides` ([#8030](stylelint/stylelint#8030)) ([@ybiquitous](https://github.com/ybiquitous)).
-   Fixed: stricter validation for the options of `declaration-property-max-values`, `declaration-property-value-no-unknown` and `number-max-precision` ([#7975](stylelint/stylelint#7975)) ([@Mouvedia](https://github.com/Mouvedia)).
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Oct 12, 2024
| datasource | package   | from   | to      |
| ---------- | --------- | ------ | ------- |
| npm        | stylelint | 16.9.0 | 16.10.0 |


## [v16.10.0](https://github.com/stylelint/stylelint/blob/HEAD/CHANGELOG.md#16100)

-   Changed: `shorthand-property-no-redundant-values` reported ranges ([#8008](stylelint/stylelint#8008)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Added: `formatter` configuration property ([#7826](stylelint/stylelint#7826)) ([@emmacharp](https://github.com/emmacharp)).
-   Added: `validate` option to Node.js API and `--validate` CLI flag ([#8009](stylelint/stylelint#8009)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Added: fixed section to the output of the `verbose` formatter ([#7985](stylelint/stylelint#7985)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Added: support for `string | RegExp` to `ignoreValues` option of `value-no-vendor-prefix` ([#7969](stylelint/stylelint#7969)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Added: support for `string` to `ignoreLonghands` option of `declaration-block-no-redundant-longhand-properties` ([#7971](stylelint/stylelint#7971)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Fixed: `at-rule-no-unknown` false positives for `@position-try` ([#7968](stylelint/stylelint#7968)) ([@yelizsevinc](https://github.com/yelizsevinc)).
-   Fixed: `declaration-block-no-redundant-longhand-properties` autofix for `grid-column`/`grid-row` ([#8023](stylelint/stylelint#8023)) ([@ryo-manba](https://github.com/ryo-manba)).
-   Fixed: `declaration-property-value-no-unknown` false negatives/positives via [`[email protected]`](https://github.com/csstree/csstree/releases/tag/v3.0.0) ([#7993](stylelint/stylelint#7993)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Fixed: `declaration-property-value-no-unknown` reported ranges and message ([#8003](stylelint/stylelint#8003)) ([@romainmenke](https://github.com/romainmenke)).
-   Fixed: `keyframe-selector-notation` reported ranges ([#7992](stylelint/stylelint#7992)) ([@romainmenke](https://github.com/romainmenke)).
-   Fixed: `length-zero-no-unit` false positives for Sass variables ([#8035](stylelint/stylelint#8035)) ([@ryo-manba](https://github.com/ryo-manba)).
-   Fixed: `no-descending-specificity` reported ranges ([#7996](stylelint/stylelint#7996)) ([@romainmenke](https://github.com/romainmenke)).
-   Fixed: `selector-anb-no-unmatchable` reported ranges and message ([#8001](stylelint/stylelint#8001)) ([@romainmenke](https://github.com/romainmenke)).
-   Fixed: `string` formatter handling of log symbols for non-Unicode terminals ([#7981](stylelint/stylelint#7981)) ([@Mouvedia](https://github.com/Mouvedia)).
-   Fixed: built `.cjs` files to address Rollup vulnerability ([#8010](stylelint/stylelint#8010)) ([@ybiquitous](https://github.com/ybiquitous)).
-   Fixed: incorrect `config` option for Node.js API with `extends` and `overrides` ([#8030](stylelint/stylelint#8030)) ([@ybiquitous](https://github.com/ybiquitous)).
-   Fixed: stricter validation for the options of `declaration-property-max-values`, `declaration-property-value-no-unknown` and `number-max-precision` ([#7975](stylelint/stylelint#7975)) ([@Mouvedia](https://github.com/Mouvedia)).
@michwads
Copy link

michwads commented Oct 14, 2024

We are using stylelint with our vite project — and something in 16.10.0 is causing errors for us: "Internal server error: Cannot read properties of undefined (reading 'config')"

Current setup is that we have a .stylelintrc.yml that extends our global .stylelintrc.json; but we keep getting this error even though 16.9.0 works just fine! This is the only part of the changelog referencing a config > so starting here.

@ybiquitous
Copy link
Member Author

@michwads @romainmenke Thanks for the report. I'll look into #8044 when I have time.

@ybiquitous
Copy link
Member Author

What I am getting at is that 16.10.0 is probably slower than 16.9.0 if you have pass more than one file as CLI parameters.
I think the cache key should be something common to all the files being passed.

@Mouvedia Yes, it's possible to slow down. But I think it's unavoidable to return a correct config because loading configs depends on file paths by cascading.

And, I think its impact is limited because there are fewer cases to use the config property, instead of config files.

If you're interested in benchmarks, could you open a new issue about the topic, instead of continuing the discussion in this closed PR?

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

Successfully merging this pull request may close these issues.

Fix incorrect config when using standalone Stylelint
4 participants