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
[BUG] No matching files on filenames with brackets #676
Comments
To me it looks like the error happens during the linter task, since the path seems to be working fine:
If you run the command directly, does it work? It seems like prettier is treating the input filepath as a glob instead of a direct string, and is thus failing to match anything. |
Doesn't work
Work prettier --write "./path/to/\[file\].js"
Yeah that's what I think too & maybe the issue should be in prettier instead. |
Thanks for the input. I'll continue the discussion on the prettier repo. |
From the prettier issue:
As @ahmedelgabri replied:
|
Regarding quotes, this issue is relevant: #635. Maybe it should get another look. |
I found temp workaround: const quote = require('shell-quote').quote;
module.exports = {
'*.{js,jsx,ts,tsx}': filenames =>
filenames.reduce((commands, filename) => {
commands.push(quote(['prettier', '--write', filename]), quote(['git', 'add', filename]));
return commands;
}, [])
}; |
@langovoi maybe you can try to do this here quoting the |
@langovoi quoting seems to work for |
This is how I currently deal with the issue: #635 (comment). Not sure how to best solve it on the lint-staged side generically. There would probably need to be some detection that the command being executed is |
What's the status here? Running lint-staged@8.0.5 w/ node 10.15.3, but when i try the workaround #676 (comment), I get: |
I just created PR #698 for escaping certain characters from the file paths, and it works for prettier+brackets at least (in the integration test). |
I was using something similar to #676 (comment) but I feel this was broken by #706 (lint-staged 9.4.0). |
@millette that PR shouldn’t have affected task behaviour, only the generated title. If you can reproduce the regression, I can try to fix it. |
Hello there! .lintstagedrc.js
Main problem is.. And, using these commands for all staged files are cause massive cli logs. |
Or how the |
I'd love us to hide this complexity but we'd need a really comprehensive test suite for that. Ans also since it's tool related, maybe we could provide the function with escaped file names arguments. @iiroj thoughts? |
@okonet maybe a special command to scape characters? Imagine something like this on "lint-staged": {
"--escape *.{js,jsx}": [
"prettier --write",
"git add"
],
"*.{js,jsx}": [
"npm run lint:eslint",
"git add"
] the Or maybe: "lint-staged": {
"*.{js,jsx}": [
"--escape",
"prettier --write",
"npm run lint:eslint"
"git add"
] In this case, the Just brainstorming. The former could open doors for some other commands. I fixed my problem by following the suggestion in #676 (comment) |
Thanks for the workaround for prettier. Between that and upgrading to the latest version of const escape = require('shell-quote').quote
module.exports = {
'*.{ts,tsx,js,json,css}': filenames => [
...filenames.map(
filename => `prettier --check "${escape([filename])}"`
),
...filenames.map(filename => `git add "${filename}"`),
],
'*.{ts,tsx,js}': ['eslint'],
'*.{ts,tsx,css}': ['stylelint'],
} |
When its going to be fixed any idea |
This is not a simple issue to fix because of cross-platform support requirements. Ideally I would like to solve this by wrapping filepath arguments inside quotes. Unfortunately I can't estimate a timeline, because I'm not sure how to fix it yet. |
I cannot find a way to simply pass quoted strings to the spawned tasks. Execa seems to escape I even re-implemented what execa does using native I think the only way to get this working is by escaping individual characters like brackets, so #698 should be looked into again. |
Neither of above worked for us. We've found out that it's possible to bypass this problem by wraping square brackets with square brackets (matcher of any char in seq).
const escape = require('shell-quote').quote;
const isWin = process.platform === 'win32';
module.exports = {
'**/*.{js,jsx,ts,tsx,json}': filenames => {
const escapedFileNames = filenames
// this will wrap all "[" "]" square brackets with another square brackets ([ => [[]) so [...customer].tsx will be processed to [[]...customer[]].tsx
.map(filename => `"${isWin ? filename.replace(/\[|\]/g, '[$&]') : escape([filename])}"`)
.join(' ');
return [
`prettier --with-node-modules --ignore-path='./.gitignore' --write ${escapedFileNames}`,
`eslint --no-ignore --max-warnings=0 --fix ${filenames.map(f => `"${f}"`).join(' ')}`,
`git add ${escapedFileNames}`
];
},
'**/*.{json,md,mdx,css,html,yml,yaml,scss}': filenames => {
const escapedFileNames = filenames
.map(filename => `"${isWin ? filename.replace(/\[|\]/g, '[$&]') : escape([filename])}"`)
.join(' ');
return [
`prettier --with-node-modules --ignore-path='./.gitignore' --write ${escapedFileNames}`,
`git add ${escapedFileNames}`
];
}
}; |
This has been fixed in v2 of Prettier. Closing. |
it's not only prettier issue, it should escape correctly |
@thomaszdxsn Feel free to open a PR, any help is appreciated! So far we haven't been able to find a consistent cross-platform method for escaping paths that works with execa, with or without using shell. |
If someone still impacted by this especially on Windows and/or monorepos, take a look at this example: It's basically escaping the filenames using shell-quote like previously commented. Note that in this scenario ts, tsx, js, jsx are prettyfied through eslint-prettier-config (up to you to change). const escape = require('shell-quote').quote;
const { CLIEngine } = require('eslint');
const cli = new CLIEngine({});
const isWin = process.platform === 'win32';
const escapeFileNamesForPrettier = (filenames) =>
filenames
.map((filename) => `"${isWin ? filename : escape([filename])}"`)
.join(' ');
module.exports = {
'**/*.{js,jsx,ts,tsx}': (filenames) => {
return [
`eslint --rule 'react-hooks/exhaustive-deps: off' --max-warnings=25 --fix ${filenames
.filter((file) => !cli.isPathIgnored(file))
.map((f) => `"${f}"`)
.join(' ')}`,
];
},
'**/*.{json,md,mdx,css,html,yml,yaml,scss}': (filenames) => {
return [`prettier --write ${escapeFileNamesForPrettier(filenames)}`];
},
}; |
Description
This is related to Next.js new dynamic routes feature.
Steps to reproduce
package.json
:[]
.Debug Logs
expand to view
Environment
lint-staged
: 8.1.7The text was updated successfully, but these errors were encountered: