language-tools: `astro check` reports that a used function is not used
What version of astro are you using?
1.9.2
Are you using an SSR adapter? If so, which one?
Node
What package manager are you using?
pnpm
What operating system are you using?
Linux
Describe the Bug
Running astro check on:
---
function noSuchRecipe(): Response {
return new Response("<p>No such recipe</p>", {
status: 404,
statusText: "No such recipe",
headers: {'content-type': 'text/html'},
});
}
// Grab this recipe.
const { username, slug } = Astro.params;
if (!slug || !username) {
return noSuchRecipe();
}
---
<p></p>
Returns:
/home/projects/github-urvrsi/src/pages/index.astro:2:10 Hint: 'noSuchRecipe' is declared but its value is never read.
but running the file actually shows the “No such recipe” output.
Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-urvrsi?file=src/pages/index.astro
Participation
- I am willing to submit a pull request for this issue.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 31
- Comments: 30 (8 by maintainers)
Commits related to this issue
- Avoid `astro check` bug in recipe page. See https://github.com/withastro/astro/issues/5869. — committed to jyasskin/cookbook by jyasskin 4 years ago
- Avoid an `astro check` bug. See https://github.com/withastro/astro/issues/5869. — committed to vlach-cookbook/cookbook by jyasskin 4 years ago
- refact: add console.logs to avoid false warnings Known astro-lang issue => https://github.com/withastro/language-tools/issues/476 — committed to taverasmisael/taverasmisael.com by taverasmisael 3 years ago
- chore: Update node, pnpm and all dependencies (#30) * core: update node, pnpm and green/yellow deps * core: update all core deps * format: run prettier Also fix prettier by adding the astro ... — committed to taverasmisael/taverasmisael.com by taverasmisael 3 years ago
- Update dependencies Includes bumping astrojs/check which resolves https://github.com/withastro/language-tools/issues/476 — committed to omidantilong/astro-tenant by omidantilong 2 years ago
Re-opening this as I’m about to revert the fix. It did work for most cases, but some cases broke it horribly and led to confusing errors. Currently trying to work on a fix, but it is quite tricky.
Apologies for the inconvenience!
I’m working on a fix here https://github.com/withastro/compiler/pull/1028
@romanstetsyk
_ prefix marks a variable or function as internal and doesn’t trigger an unused warning. What’s the reason why astro check doesn’t see it as used? That, I don’t know.
@OrlandoBricenoB I believe in your case
astro checkgoes through the built code in the dist folder, which is a separate issueNo, I’m only working on making it so top-level returns don’t trigger an error in editor tooling + astro check, no changes to Astro itself.
I had someting very similar to OP:
My workaround for now was to rename
noSuchRecipeto_noSuchRecipeto silence the warning.🎉 thanks @Princesseuh!
I’m using this workaround until there’s a fix. It’s a little bit of extra code but allows me to keep
astro checkin place.I create a function that I can pass the function that
astro checkis flagging to. Doing so means that the checker now believes the function to be used.Just chiming in to say I’ve also encountered this, and it is very odd. In my case, I was able to suppress the TS error in the IDE (in VSCode at least) by re-assigning the import:
It’s more than a little clunky, but keeps the editor happy. However,
astro checkwill still thinkfancyResponseis declared but never read.I’ve not spent a huge amount of time with Astro, but so far I’m liking it - this is the first real ickiness I’ve encountered ❤️