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 "TypeError: opts.node.rangeBy is not a function" with PostCSS 8.4.4 #5766

Closed
fomenkogregory opened this issue Dec 7, 2021 · 71 comments · Fixed by #6244
Closed

Fix "TypeError: opts.node.rangeBy is not a function" with PostCSS 8.4.4 #5766

fomenkogregory opened this issue Dec 7, 2021 · 71 comments · Fixed by #6244
Labels
status: wip is being worked on by someone type: bug a problem with a feature or rule

Comments

@fomenkogregory
Copy link

fomenkogregory commented Dec 7, 2021

What steps are needed to reproduce the bug?

folder structure:
package.json
.stylelintrc.json
styles.scss (empty file)

package.json:

{
  "name": "untitled2",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "lint": "npx stylelint '**/*.scss'"
  },
  "devDependencies": {
    "stylelint": "^14.1.0",
    "stylelint-config-standard-scss": "^3.0.0"
  }
}

npm i
npm run lint

What Stylelint configuration is needed to reproduce the bug?

{
  "extends": "stylelint-config-standard-scss"
}

How did you run Stylelint?

npx stylelint '**/*.scss'

Which version of Stylelint are you using?

^14.1.0

What did you expect to happen?

I expect stylelint to show list of errors.

What actually happened?

TypeError: opts.node.rangeBy is not a function
at new Warning (node_modules/postcss/lib/warning.js:9:29)
at Result.warn (node_modules/postcss/lib/result.js:26:19)
at report (node_modules/stylelint/lib/utils/report.js:103:9)
at node_modules/stylelint/lib/rules/no-empty-source/index.js:28:3
at node_modules/stylelint/lib/lintPostcssResult.js:112:8
at Array.map (<anonymous>)
at lintPostcssResult (node_modules/stylelint/lib/lintPostcssResult.js:103:18)
at lintSource (node_modules/stylelint/lib/lintSource.js:88:8)
at async node_modules/stylelint/lib/standalone.js:218:27
at async Promise.all (index 0)

Process finished with exit code 1

The problem is in this line

image

Does the bug relate to non-standard syntax?

No

Proposal to fix the bug

Works correctly with exactly 8.3.11 version of postcss.
I compared the code in warning.js between these versions of postcss (8.3.11 and 8.4.4), they changed the method name. :(

Proposal: change dependencies of stylelint to use exactly 8.3.11 version.

@ai
Copy link
Member

ai commented Dec 7, 2021

Could it happen because of the different PostCSS versions at chimera’s AST containing nodes from different PostCSS versions?

Can you look npm ls for different PostCSS versions?

@vankop
Copy link
Member

vankop commented Dec 7, 2021

Can you look npm ls for different PostCSS versions?

In our case we have a problem using https://github.com/ismay/stylelint-no-unsupported-browser-features/blob/master/package.json this package has postcss as a dependency

@fomenkogregory
Copy link
Author

fomenkogregory commented Dec 7, 2021

this is npm ls from this example

└─┬ stylelint@14.0.1
  └── postcss@8.4.4 

and this is from another project (new angular 13 project) (works fine)

└─┬ stylelint@14.0.1
  ├─┬ postcss-safe-parser@6.0.0
   └── postcss@8.3.6 deduped
  └── postcss@8.3.11

@p-m-p
Copy link

p-m-p commented Dec 7, 2021

I'm also seeing this on the no-empty-source rule, same versions as @fomenkogregory installed today.

@ybiquitous
Copy link
Member

ybiquitous commented Dec 8, 2021

I've created a reproduction repository, but such a TypeError never occurs. 🤔
Is there something I missed?

https://github.com/ybiquitous/stylelint-issue-5766/runs/4451383596?check_suite_focus=true#step:6:1

image

@ybiquitous ybiquitous added status: needs investigation triage needs further investigation type: bug a problem with a feature or rule labels Dec 8, 2021
@ai
Copy link
Member

ai commented Dec 8, 2021

@fomenkogregory can you try to update dependencies by running npm update or yarn upgrade to test that it is happens because of different PostCSS versions?

@fomenkogregory
Copy link
Author

fomenkogregory commented Dec 8, 2021

I've created a reproduction repository, but such a TypeError never occurs. 🤔 Is there something I missed?

https://github.com/ybiquitous/stylelint-issue-5766/runs/4451383596?check_suite_focus=true#step:6:1

image

thanks! I'll try to clone your repo and see if it will work on my side. Then I will compare lock file and node_modules and to see what's differs.

@BroFox86
Copy link

BroFox86 commented Dec 8, 2021

In my case I just added PostCSS to my project by npm i postcss and the error from the Stylelint VSC plugin was gone.

@lregla
Copy link

lregla commented Dec 9, 2021

Also started with a fresh install of stylelint@14.1.0 and stylelint-config-standard-scss@3.0.0. Ran into the same error described above. Installed postcss@8.4.4 as @BroFox86 did, and the error is gone.

@ai
Copy link
Member

ai commented Dec 9, 2021

Thanks for the help.

I will try to find solution during this weekend.

@jeddy3 jeddy3 removed the type: bug a problem with a feature or rule label Dec 9, 2021
@fomenkogregory
Copy link
Author

fomenkogregory commented Dec 9, 2021

Also started with a fresh install of stylelint@14.1.0 and stylelint-config-standard-scss@3.0.0. Ran into the same error described above. Installed postcss@8.4.4 as @BroFox86 did, and the error is gone.

And moreover, you can then uninstall postcss and all works fine for current and all future projects. I wasn't been able to reproduce the issue for any new projects as well.

@RiZKiT
Copy link

RiZKiT commented Dec 20, 2021

Seems to be related to/a duplicate of: #5756

wmfgerrit pushed a commit to wikimedia/design-codex that referenced this issue Jan 12, 2022
Add a custom Vite plugin that generates RTL versions of all built .css
files. This causes our Codex build to generate both codex.style.css and
codex.style-rtl.css.

Pin postcss at v8.3.11 to prevent stylelint from crashing, see
stylelint/stylelint#5766

Bug: T295189
Change-Id: I4b7203375b8f8a40dc8c24086544dc7a011e88bf
@Yohandah
Copy link

Yohandah commented Feb 1, 2022

When will it ship ?

@ai
Copy link
Member

ai commented Feb 1, 2022

@Yohandah the solution was not found. Please help in investigation and send PR if you want to fix it sooner.

@Yohandah
Copy link

Yohandah commented Feb 1, 2022

Sorry I read too fast I thought a PR was ready.

A weird thing happened while I was looking for a fix.

I removed "stylelint-config-prettier" from my extends array, the error was gone, then I re-added it to see if the errors comes back, and it's completely gone I can't reproduce it.

@ijcnvv
Copy link

ijcnvv commented Feb 1, 2022

totally uninstalling postcss & stylelint with their plugins helped me. I Installed postcss at first and then stylelint and have no errors now

@Yohandah
Copy link

Yohandah commented Feb 2, 2022

It seems the error only happens when I comment using // instead of /* ?

Screenshare.-.2022-02-02.10_57_16.AM.mp4

@KeithGillette
Copy link

Installing postcss@latest (8.4.6 at my testing) then uninstalling it prevented the TypeError: opts.node.rangeBy is not a function error upon updating to stylelint@14.2.0 in our project, just as @fomenkogregory found. 🤷🏻‍♂️

@jeddy3 jeddy3 changed the title Stylelint doesn't work with postcss 8.4.4 Fix "TypeError: opts.node.rangeBy is not a function" with PostCSS 8.4.4 Feb 10, 2022
@ThomasHenrissat
Copy link

It seems the error only happens when I comment using // instead of /* ?

I can confirm this. When I removed the // comments from my file the error stopped showing up.

@JounQin
Copy link
Member

JounQin commented Aug 6, 2022

@ai I'm not so familiar with the source codes from postcss, and I don't quite understand where node.rangeBy will be normalized back? Can you point it more specifically?

@ai
Copy link
Member

ai commented Aug 6, 2022

@JounQin Container.rebuild replaces prototype of every nodes object of AST. So even if parse returned old AST, rebuild will update it to me version.

@JounQin
Copy link
Member

JounQin commented Aug 6, 2022

@ai It's still unclear where node.rangeBy would be patched by Container.rebuild() to me. 😅

@snebjorn
Copy link

snebjorn commented Aug 6, 2022

@JounQin The assignment of the rangeBy function is done here https://github.com/postcss/postcss/blob/main/lib/container.js#L415-L423 via Object.setPrototypeOf()

However the node that is throwing the error is of type root which the function doesn't handle.

  if (node.type === 'atrule') {
    Object.setPrototypeOf(node, AtRule.prototype)
  } else if (node.type === 'rule') {
    Object.setPrototypeOf(node, Rule.prototype)
  } else if (node.type === 'decl') {
    Object.setPrototypeOf(node, Declaration.prototype)
  } else if (node.type === 'comment') {
    Object.setPrototypeOf(node, Comment.prototype)
  }

The function is being called for the root type.
image

@ai could that be the issue?

@ai
Copy link
Member

ai commented Aug 6, 2022

@snebjorn exactly! I added the fix postcss/postcss@f289e17

I released a fix in PostCSS 8.4.15. Can we try it?

@snebjorn
Copy link

snebjorn commented Aug 6, 2022

Not sure where Container.registerRoot is getting called from. But I'm getting Root as undefined when I update the postcss version inside stylelint.

TypeError: Cannot read properties of undefined (reading 'prototype')
    at Container.rebuild (~\styelint-no-rangeBy\node_modules\stylelint\node_modules\postcss\lib\container.js:428:38)

Dependency tree

styelint-no-rangeBy@ ~\styelint-no-rangeBy
├─┬ postcss-less@6.0.0
│ └── postcss@8.3.5 deduped
├── postcss@8.3.5
└─┬ stylelint@14.9.1
  ├─┬ @csstools/selector-specificity@2.0.2
  │ └── postcss@8.4.15 deduped
  ├─┬ postcss-safe-parser@6.0.0
  │ └── postcss@8.4.15 deduped
  └── postcss@8.4.15

@ai
Copy link
Member

ai commented Aug 6, 2022

@snebjorn sorry, can you try again with PostCSS 8.4.16?

It was a small typo and we have not tests for AST migration hack.

@snebjorn
Copy link

snebjorn commented Aug 6, 2022

Gentlemen, we have a winner!

❯ npm run stylelint

> stylelint
> stylelint src/**/*.less


src/foo.less
 2:22  ✖  Unexpected whitespace at end of line  no-eol-whitespace

@ai
Copy link
Member

ai commented Aug 6, 2022

Now let’s bump Stylelint with the latest PostCSS version and ask every user just to update stylelint in their package.json.

Thanks to @JounQin and @snebjorn for solving this very complex bug.

@snebjorn
Copy link

snebjorn commented Aug 6, 2022

Thanks for the quick responses. It kept me motivated :)
It's been a pleasure!

@ybiquitous
Copy link
Member

@snebjorn @JounQin @ai
I appreciate your work very much! 👏🏼

@DreamShaded
Copy link

DreamShaded commented Aug 8, 2022

hi! so users have to wait stylelint v14.9.2, yep?)

update: no, i've just updated postcss to 8.4.16 and it all works *___* thx a lot guys!

@ismay
Copy link
Contributor

ismay commented Sep 9, 2022

This bug has affected a stylelint plugin I maintain and I'm a little confused by it. My lib has a peerdep on stylelint >=13.0.0, and a direct dep on postcss 8.x. I'm recommending my users to upgrade to stylelint v14.10.0 or above to resolve this as recommended in the comment by ai above.

However, it's still affecting users of my plugin that are on stylelint v13. Would the only way to address this for users of stylelint 13 be to backport a fix to stylelint v13? And is that a feasible solution?

Because I'm thinking of just restricting my peerdep range to stylelint v14 just to resolve this for users of my lib, but I want to make sure that I understand exactly what's going on.

@JounQin
Copy link
Member

JounQin commented Sep 9, 2022

@ismay No, only postcss needs to be upgraded to v8.4.16.

@ismay
Copy link
Contributor

ismay commented Sep 9, 2022

@ismay No, only postcss needs to be upgraded to v8.4.16.

@JounQin Sorry, it's not really clear to me to what part of my comment you're responding. Are you referring to the recommendation I've made to my users, or to the fix that could be made to stylelint v13?

@JounQin
Copy link
Member

JounQin commented Sep 9, 2022

@ismay I mean it's not required to upgrade to v14, you only need to upgrade the postcss dep on your side.

gopherbot pushed a commit to golang/pkgsite that referenced this issue Dec 12, 2022
Upgraded postcss to fix a type error when running
stylelint --fix. See stylelint/stylelint#5766.

Fixed the fileglob for CSS files and made simple fixes
for stylelint errors.

Change-Id: If838a740f97532b9b29ad16fb6aeb5de72ed49d1
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/455956
Run-TryBot: Jamal Carvalho <jamal@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
@zengshine
Copy link

Now let’s bump Stylelint with the latest PostCSS version and ask every user just to update stylelint in their package.json.

Thanks to @JounQin and @snebjorn for solving this very complex bug.

respect!

@AndyOGo
Copy link
Contributor

AndyOGo commented Feb 10, 2023

I face that type error again after upgrading to v15

TypeError: node.rangeBy is not a function
at Object.report (/stylelint-declaration-strict-value/node_modules/stylelint/lib/utils/report.js:35:34)

@AndyOGo AndyOGo mentioned this issue Feb 10, 2023
6 tasks
@ybiquitous
Copy link
Member

@AndyOGo That error should never occur if you are using the latest PostCSS. Please recheck your dependency tree.

@AndyOGo
Copy link
Contributor

AndyOGo commented Feb 11, 2023

@ybiquitous

Edit:
Never mind, it seems to be caused by stylelint-test-rule-tape.


Indeed, I'm surprised too.
I'm getting postcss from stylelint and it is the latest of 8.4.21.

npm ls postcss
└─┬ stylelint@15.0.0
  ├─┬ @csstools/selector-specificity@2.1.1
  │ └── postcss@8.4.21 deduped
  ├─┬ postcss-safe-parser@6.0.0
  │ └── postcss@8.4.21 deduped
  └── postcss@8.4.21

FYI:
That is my PR to upgrade my plugin to v15
AndyOGo/stylelint-declaration-strict-value#310

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: wip is being worked on by someone type: bug a problem with a feature or rule