diff --git a/AGENTS.md b/AGENTS.md index a85732521..e828c5142 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -208,7 +208,7 @@ These hold for the framework and every app built on it. Hit one as a runtime err `webjs create [--template api] [--db sqlite|postgres] [--runtime node|bun] [--skip-ci]` scaffolds an app (full-stack by default, `api` for a backend with no UI); always scaffold rather than hand-roll, and keep app data in the wired-up database, never JSON files. The other commands: `webjs dev`, `start`, `test`, `check [--rules] [--json]`, `ci`, `routes`, `mcp`, `status`, `stop`, `verify`, `source `, `doctor`, `audit`, `types`, `regenerate`, `typecheck`, `db`, `ui`, `vendor`, `version`, `help [command]`. `webjs help ` prints each one's options; the docs site has the full reference. Run this repo's own apps (`website/`, `gallery/`, `examples/blog/`) from inside them with their `npm run dev` / `npm start`. -`webjs check` carries correctness rules only (code that is wrong to ship), run unconditionally; anything a sensible app could do differently is a convention in prose, not a check. Run it from INSIDE an app, never from the repo root. +`webjs check` carries correctness rules only (code that is wrong to ship), run unconditionally; anything a sensible app could do differently is a convention in prose, not a check. Run it from INSIDE an app, never from the repo root. It skips `node_modules` and any sub-folder with its own `package.json` (a separate package such as an Expo app in `mobile/`, #1700). --- diff --git a/packages/server/AGENTS.md b/packages/server/AGENTS.md index a70b54212..7aca287ec 100644 --- a/packages/server/AGENTS.md +++ b/packages/server/AGENTS.md @@ -124,7 +124,7 @@ with metadata, Suspense, streaming) for HTML, or `api.js` / | `check/rules-route-modules.js` | The `webjs check` rules for the line between server-only route modules and browser code (#1686): `no-component-in-route-module`, `no-event-binding-in-route-module` (a binding on a real open tag only, so escaped sample text is quiet), `no-route-module-import-in-browser-module` and `no-node-builtin-in-browser-module` (`import type` ignored). Run from `checkConventions` with the context `checkServerImportInBrowserModule` already computed. Tests: `test/check/route-module-rules.test.js` | | `route-report.js` | `analyzeRouteModules(appDir)` (#1686): builds the module graph, the route table and the component scan once and returns `{ analysed, skipped, routeModules, orphans, summary }`, sorted and app-relative. Each route row is `{ file, kind, emits, hints }`: the route module's kind, the component frontier the boot loads in its place (`route-frontier.js`), and the `routeModuleBrowserHints` for its source. `orphans` lists every `class X extends WebComponent` the scanner cannot see (no registration call, or a computed tag), which never upgrades. Consumed by `webjs doctor` (`ROUTE_BROWSER_CODE`, `ORPHAN_COMPONENTS`). A report, not a build | | `js-scan.js` | Shared lexical scanners (`redactStringsAndTemplates`, `redactToPlaceholders`, `extractWebComponentClassBodies`, `matchClosingBrace`) used by `check.js`, `component-scanner.js`, `client-signals.js` and `lazy-components.js`. **`matchClosingBrace` keeps a STACK of frames, not one flat depth counter** (#1307): a `${` hole is a code context nested inside a template, so it pushes its own frame and the `}` that closes it pops back into the template instead of counting toward the block being matched. The flat counter incremented at `${` and never decremented (the closing `}` arrived while still in template state), so a class body containing an interpolated template was unmatchable. It stayed latent because every caller passes a position-preserving MASK in which holes are already blanked, so none ever fed it a `${`. `redactToPlaceholders` (#634) masks comments and replaces each string / template body with a `__STR___` placeholder (originals returned in a `literals` array, `${...}` holes scanned as code), so the component scanner and the import scanners see a real top-level `register(...)` / `import` while an identical token shown inside a code-sample string is inert | -| `fs-walk.js` | Async recursive directory walker | +| `fs-walk.js` | Async recursive directory walker. Always skips dot-entries and `node_modules`. `walk(dir, filter, { skipNestedPackages: true })` also skips any directory below `dir` holding its own `package.json` (a separate package, such as an Expo app in `mobile/`), which every walk of `webjs check` sets (#1700). The router, action and component scans do not set it. Tests: the nested-package and fs-walk cases in `test/check/check.test.js`, and `test/check/foreign-imports.test.js`. | | `logger.js` | `defaultLogger` (JSON-shaped in prod, pretty in dev) | ## Public exports diff --git a/packages/server/src/check/rules-typescript.js b/packages/server/src/check/rules-typescript.js index 5cae70b04..d7b48416e 100644 --- a/packages/server/src/check/rules-typescript.js +++ b/packages/server/src/check/rules-typescript.js @@ -139,7 +139,9 @@ export async function checkNoNonErasableTypescript(appDir, violations) { // decorator scan alone: no JavaScript runtime WebJs serves to runs // decorators either, so `@customElement` in a .js component is the same // load-time SyntaxError. - for await (const abs of walk(appDir, (p) => /\.m?[jt]s$/.test(p))) { + // A nested package (its own package.json) is skipped, the same boundary + // the runner's file walk draws (#1700). + for await (const abs of walk(appDir, (p) => /\.m?[jt]s$/.test(p), { skipNestedPackages: true })) { // Skip anything inside node_modules or common build / cache dirs. const relPath = relative(appDir, abs); if ( diff --git a/packages/server/src/check/runner-support.js b/packages/server/src/check/runner-support.js index f0641b741..a6c908065 100644 --- a/packages/server/src/check/runner-support.js +++ b/packages/server/src/check/runner-support.js @@ -7,7 +7,6 @@ */ import { readFile, stat } from 'node:fs/promises'; import { join, relative } from 'node:path'; -import { walk } from '../fs-walk.js'; import { buildModuleGraph, transitiveDeps } from '../module-graph.js'; import { scanComponents } from '../component-scanner.js'; import { buildRouteTable } from '../router.js'; diff --git a/packages/server/src/check/runner.js b/packages/server/src/check/runner.js index dc927fd57..f6dfe7d94 100644 --- a/packages/server/src/check/runner.js +++ b/packages/server/src/check/runner.js @@ -86,7 +86,10 @@ export async function checkConventions(appDir) { // positives. /** @type {{ abs: string, rel: string, content: string, scan: string }[]} */ const files = []; - for await (const abs of walk(appDir, (p) => /\.m?[jt]sx?$/.test(p))) { + // A sub-folder with its own package.json is another package (an Expo app + // in mobile/, say), not part of this app, so it is skipped like + // node_modules (#1700). + for await (const abs of walk(appDir, (p) => /\.m?[jt]sx?$/.test(p), { skipNestedPackages: true })) { const rel = relative(appDir, abs); let content; try { diff --git a/packages/server/src/fs-walk.js b/packages/server/src/fs-walk.js index bcffc5c6d..a20945eca 100644 --- a/packages/server/src/fs-walk.js +++ b/packages/server/src/fs-walk.js @@ -1,26 +1,57 @@ -import { readdir, stat } from 'node:fs/promises'; +import { readdir } from 'node:fs/promises'; import { join } from 'node:path'; /** - * Recursively walk a directory, yielding absolute file paths. + * @typedef {object} WalkOptions + * @property {boolean} [skipNestedPackages] Skip any directory BELOW `dir` + * that holds its own `package.json`, and everything under it. Such a + * directory is a separate package (an Expo app in `mobile/`, a workspace + * member), the same boundary npm, Bun workspaces and TypeScript project + * references draw. The root's own `package.json` is never a boundary. + * `webjs check` sets it (#1700); the router, action and component scans do + * not. + */ + +/** + * Recursively walk a directory, yielding absolute file paths. Dot-entries and + * `node_modules` are always skipped. * * @param {string} dir * @param {(path: string) => boolean} [filter] + * @param {WalkOptions} [options] * @returns {AsyncGenerator} */ -export async function* walk(dir, filter) { +export async function* walk(dir, filter, options = {}) { + yield* walkDir(dir, filter, options, true); +} + +/** + * @param {string} dir + * @param {((path: string) => boolean) | undefined} filter + * @param {WalkOptions} options + * @param {boolean} isRoot + * @returns {AsyncGenerator} + */ +async function* walkDir(dir, filter, options, isRoot) { let entries; try { entries = await readdir(dir, { withFileTypes: true }); } catch { return; } + if ( + options.skipNestedPackages && + !isRoot && + entries.some((e) => e.name === 'package.json' && e.isFile()) + ) { + return; + } for (const entry of entries) { if (entry.name.startsWith('.')) continue; if (entry.name === 'node_modules') continue; const full = join(dir, entry.name); if (entry.isDirectory()) { - yield* walk(full, filter); + yield* walkDir(full, filter, options, false); } else if (entry.isFile()) { if (!filter || filter(full)) yield full; } diff --git a/packages/server/test/check/check.test.js b/packages/server/test/check/check.test.js index e65d94106..e02e67d56 100644 --- a/packages/server/test/check/check.test.js +++ b/packages/server/test/check/check.test.js @@ -1770,3 +1770,52 @@ test('no-next-runtime-guard: flags process.env.NEXT_RUNTIME', async () => { await rm(appDir, { recursive: true, force: true }); } }); + +test('nested package: every check walk skips a sub-folder with its own package.json (#1700)', async () => { + const appDir = await makeTempApp(); + try { + await writeFile(join(appDir, 'package.json'), '{"name":"api","type":"module"}'); + // The app's own enum (the TS walk) and the nested package's enum, JSX and + // React import (the TS walk and the runner's file walk). + await writeFileEnsureDir(join(appDir, 'lib', 'status.ts'), `export enum Status { On = 'on' }\n`); + await writeFileEnsureDir(join(appDir, 'mobile', 'src', 'theme.ts'), `export enum Mode { Light, Dark }\n`); + await writeFileEnsureDir( + join(appDir, 'mobile', 'src', 'ui.tsx'), + `import { useState } from 'react';\nexport const Button = () =>