Fails to match svelte route files that contain bracket parameters
In SvelteKit, page components use brackets to encode dynamic parameters for file-based routing e.g. ./src/routes/[id].svelte.
When running dprint fmt ./src/routes/[id].svelte, it prints the message:
> dprint fmt ./src/routes/[id].svelte
No files found to format with the specified plugins. You may want to try using `dprint output-file-paths` to see which files it's finding.
But running dprint output-file-paths shows these files as expected:
> dprint output-file-paths
...
/path/to/src/routes/[id].svelte
Here's my configuration:
{
"typescript": {
"lineWidth": 100,
"quoteStyle": "preferSingle"
},
"prettier": {
"associations": [
"**/*.{svelte}"
],
"singleQuote": true,
"trailingComma": "all",
"printWidth": 100
},
"includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs,json,md,svelte}"],
"excludes": [
"**/node_modules",
"**/*-lock.json",
".svelte-kit"
],
"plugins": [
"https://plugins.dprint.dev/typescript-0.70.0.wasm",
"https://plugins.dprint.dev/json-0.15.3.wasm",
"https://plugins.dprint.dev/markdown-0.13.3.wasm",
"https://plugins.dprint.dev/prettier-0.10.0.json@ca9a97de84cbb2cd60534eb72c0455f3ca8704743917569ace70499136cf5c9c"
]
}
It seems like the glob is working as expected since it does correctly output file paths with brackets. But when formatting, it fails to match. Regular svelte files work fine.
Are you on Windows? I just found some bugs specific to the Windows implementation, but otherwise the following should work:
dprint fmt "./src/routes/[[]id[]].svelte"
dprint output-file-paths "./src/routes/[[]id[]].svelte"
Basically the square brackets need to be escaped because the cli only accepts file patterns at the moment. Maybe I can try to get back a mix of file paths and file patterns where it will treat a pattern as a file if it exists.
Is there a specific reason to format the file individually? Just running dprint fmt should hopefully be fast enough.
@dsherret that does indeed work — thank you!
My use case/the reason this was an issue is the usage of lint-staged. This passes the staged files individually to the command specified by default. However, dprint is definitely fast enough for the size of my project where I can just run dprint fmt directly in the pre-commit hook.
In the long term, it would be nice to be able to leverage lint-staged easily so we can minimize (and localize) any formatting to changed files so that my commit hook latency doesn't scale with the size of the repo.
That said it looks like it might be possible today by configuring a function on lint-staged to escape the brackets before passing to dprint.
I'm thinking to add some special behaviour for it to auto-escape [ and ] if the specified file exists with that. The chance of someone wanting to use it as a glob when the file exists is probably zero.