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

Most upvoted comments

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!

@romanstetsyk

Nice workaround. Why does it work though?

_ 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 check goes through the built code in the dist folder, which is a separate issue

@Princesseuh Amazing! I guess that means throw syntax will also be explicitly supported? I’ve been using that since a few weeks with my own middleware and I haven’t run into any difficulties. Would be nice to have it natively supported ❤️

No, 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:

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();
}

My workaround for now was to rename noSuchRecipe to _noSuchRecipe to 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 check in place.

I create a function that I can pass the function that astro check is flagging to. Doing so means that the checker now believes the function to be used.

export function avoidAstroTypeCheckBug(_: unknown) {
  // See https://github.com/withastro/language-tools/issues/476 for more context.
  return;
}

// In a .astro file
import { avoidAstroTypeCheckBug } from './avoidAstroTypeCheckBug';
import { createNotFoundResponse } from './createNotFoundResponse';

if (!someValue) {
  avoidAstroTypeCheckBug(createNotFoundResponse);
  return createNotFoundResponse();
}

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:

import someResponse from "../someFile"
const fancyResponse = someResponse
...
return fancyResponse()

It’s more than a little clunky, but keeps the editor happy. However, astro check will still think fancyResponse is 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 ❤️