trails/eslint.config.js
Ullrich Schäfer 10deae88c9
fix: extensionless server-side imports + lint rule to catch them
## Symptom

Grafana's \`app-crash-log\` alert fires on every deploy. The Loki query
that backs it (\`ERR_|FATAL|uncaughtException|…\`) matches:

  Error [ERR_MODULE_NOT_FOUND]: Cannot find module
  '/app/apps/journal/app/lib/logger.server' imported from
  '/app/apps/journal/app/lib/email.server.ts'

A real bug, not a false positive: \`email.server.ts\` line 2 was
\`import { logger } from \"./logger.server\"\` — no extension.

## Why typecheck didn't catch it

\`tsconfig.base.json\` sets \`moduleResolution: \"bundler\"\`. The bundler
resolver accepts extensionless relative imports because Vite / esbuild
/ webpack resolve them at build time. TypeScript was happy.

Production runs \`node --experimental-strip-types server.ts\`, which
uses Node's NodeNext ESM resolver — strict about extensions. The two
resolvers disagree silently for files Node loads directly (server.ts,
\`.server.ts\` modules dynamically imported from it, and jobs).

## Fix

1. **Added \`.ts\` extensions to the 4 broken imports** I could find:
   - \`apps/journal/app/lib/email.server.ts\` → \`./logger.server.ts\`
     (the actual deploy-blocker)
   - \`apps/planner/app/lib/brouter.ts\` → \`./route-merge.ts\`, \`./http.server.ts\`
   - \`apps/planner/app/lib/use-nearby-pois.ts\` → \`./overpass.ts\`
   - \`packages/map/src/MapView.tsx\` → \`./layers.ts\` (caught by the rule)

2. **Added \`eslint-plugin-import-x\` with \`import-x/extensions: always\`**,
   scoped to files Node actually executes raw:
   - \`apps/*/server.ts\`
   - \`apps/*/app/lib/**/*.server.ts\`
   - \`apps/*/app/jobs/**/*.ts\`
   - \`packages/*/src/**/*.{ts,tsx}\`

   Ignored: route-scoped \`*.server.ts\` files (bundled by Vite into the
   React Router build, never loaded by Node), and test files (Vitest's
   own resolver).

## What's still possible

- Non-\`.server.ts\` files in \`app/lib\` (e.g. \`legal.ts\`, \`actor-iri.ts\`)
  could still ship extensionless imports. They're not in the lint
  scope. If one breaks at runtime we can extend the glob — for now
  they tend to be tiny utility modules that don't import other
  relatives.
- The deeper fix would be \`moduleResolution: nodenext\` for server-side
  tsconfig, or bundling the server code so Node never sees raw \`.ts\`.
  Bigger surgery; the lint rule covers the failure mode for now.

Full repo: pnpm typecheck / lint / test all green after \`pnpm install\`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 08:08:19 +02:00

57 lines
2 KiB
JavaScript

import js from "@eslint/js";
import tseslint from "typescript-eslint";
import importX from "eslint-plugin-import-x";
import prettier from "eslint-config-prettier";
export default tseslint.config(
{ ignores: ["**/build/", "**/dist/", "**/.react-router/", "**/.expo/", "**/node_modules/", "**/metro.config.js"] },
js.configs.recommended,
...tseslint.configs.recommended,
{
rules: {
"@typescript-eslint/no-unused-vars": [
"error",
{ argsIgnorePattern: "^_", varsIgnorePattern: "^_", caughtErrorsIgnorePattern: "^_" },
],
},
},
// Require explicit file extensions on relative imports — but ONLY
// for files Node executes as raw .ts (server entrypoint, .server.ts
// modules, jobs, shared packages). Our tsconfig uses
// `moduleResolution: bundler` so TypeScript accepts extensionless
// imports everywhere; but `apps/*/server.ts` and the .server.ts /
// jobs files it loads run through Node's strict NodeNext resolver
// at runtime, which requires extensions. Without this rule the two
// resolvers diverge silently — that gap let `email.server.ts` ship
// a broken `./logger.server` import that crashed at boot.
//
// Client-side files (routes, hooks, components) are bundled by
// Vite and don't need explicit extensions.
{
files: [
"apps/*/server.ts",
"apps/*/app/lib/**/*.server.ts",
"apps/*/app/jobs/**/*.ts",
"packages/*/src/**/*.ts",
"packages/*/src/**/*.tsx",
],
ignores: [
// Route-scoped .server.ts files live under app/routes and are
// bundled by Vite into the React Router build — Node never
// loads them directly. Tests run under Vitest's own resolver
// and aren't shipped to Docker.
"apps/*/app/routes/**/*.server.ts",
"**/*.test.ts",
"**/*.test.tsx",
],
plugins: { "import-x": importX },
rules: {
"import-x/extensions": [
"error",
"always",
{ ignorePackages: true, checkTypeImports: true },
],
},
},
prettier,
);