Fix crash when fixing file not owned by current user
See original GitHub issueWhen running --fix
stylelint attempts to chown its temp file back to the owner of the original file. This fails and causes stylelint to crash when the chown can not be completed. Other linting tools like prettier are able to rewrite files not owned by the current user so I would expect stylelint to be able to as well.
The Error is Error: EPERM: operation not permitted, chown '/Users/benscott/projects/stylelint-fixfail/style.css.4151106271'
I discovered this bug because I am running stylelint --fix
as part of some integration tests for a tool that wraps stylelint and my tests failed on CI as the initial checkout of the files is performed by the root
user but the tests (and thus stylelint --fix
) is ran by the circleci
user.
Which rule, if any, is the bug related to?
All autofixable rules.
What CSS / configuration is needed to reproduce the bug?
In a new folder, create:
A package.json:
{
"name": "stylelint-fixfail",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "stylelint '*.css'",
"lint:fix": "stylelint '*.css' --fix",
"prettier": "prettier '*.css' --write"
},
"author": "",
"license": "ISC",
"dependencies": {
"prettier": "1.14.2",
"stylelint": "9.4.0"
},
"stylelint": {
"rules": {
"selector-pseudo-element-colon-notation": "double"
}
}
}
and a style.css
:
.foo:before {top: 0;}
This css violates the selector-pseudo-element-colon-notation
rule and autofixing should transform the file contents to be .foo::before {top: 0;}
(note the double colon).
Which version of stylelint are you using?
9.4.0
Reproduction steps
npm install
- Run
npm run lint:fix
and note that style.css is reformatted correctly, then reset the file back to its original content sudo chown root style.css && sudo chmod 666 style.css
to change the ownership of the file to be not-the-current-user but still be writable by all users.- Run
npm run lint:fix
Get the error messageError: EPERM: operation not permitted, chown '/Users/benscott/projects/stylelint-fixfail/style.css.4151106271'
and the file is not autoformatted - Run
npm run prettier
and note that prettier can successfully reformat the file and maintain its ownership without crashing.
Does the bug relate to non-standard syntax (e.g. SCSS, Less etc.)?
It affects all syntaxes
What did you expect to happen?
The file to be autofixed successfully.
What actually happened (e.g. what warnings or errors did you get)?
The file was not autofixed. The following error was raised:
Error: EPERM: operation not permitted, chown '/Users/benscott/projects/stylelint-fixfail/style.css.4151106271'
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:7 (4 by maintainers)
From #2992:
If we switch to
fs.writeFileSync
, there will be at most one corrupt file in a cli run. I think it is acceptable, consider that crashes or unexpected poweroff should be rare. I assumed thatctrl + c
will not preemptfs.writeFileSync
, right?/cc @kevinsawicki can you create minimum reproducible test repo? Maybey we can find other way to fix it, thanks