AgentSkillsCN

grafema-discovery-unbuilt-projects

修复 Grafema 在分析 0 个源文件时的异常——当 package.json 中的入口点指向不存在的 dist/ 或 build/ 目录时,此问题尤为突出。适用场景:(1) grafema analyze 即使源文件存在,却只产出 0 个模块、0 个函数;(2) grafema overview 仅显示插件节点;(3) 项目源码位于 src/ 目录,但入口点却指向未构建的 dist/;(4) 多源码目录的单体式项目(如 src/、hooks/src/、compat/src/)。根本原因:SimpleProjectDiscovery 会根据 package.json 中的 main/module/exports 字段来定位构建产物。解决方案:在 config.yaml 中显式添加 services 数组。

SKILL.md
--- frontmatter
name: grafema-discovery-unbuilt-projects
description: |
  Fix Grafema analyzing 0 source files when package.json entry points point to dist/ or
  build/ directories that don't exist. Use when: (1) grafema analyze produces 0 modules,
  0 functions despite source files existing, (2) grafema overview shows only plugin nodes,
  (3) project has source in src/ but entry points in dist/ (unbuilt), (4) monorepo-style
  projects with multiple source directories (e.g., src/, hooks/src/, compat/src/).
  Root cause: SimpleProjectDiscovery follows package.json main/module/exports fields which
  point to built output. Solution: add explicit services array in config.yaml.
author: Claude Code
version: 1.0.0
date: 2026-02-10

Grafema Discovery for Unbuilt Projects

Problem

Grafema's auto-discovery finds 0 source files when a project's package.json entry points (main, module, exports) point to built output directories (dist/, build/) that either don't exist or contain stale code. This is common in SWE-bench tasks where the project is checked out at a specific commit without running the build step.

Context / Trigger Conditions

  • grafema analyze completes but reports only ~29 nodes (all plugin metadata)
  • grafema overview shows: Modules: 0, Functions: 0, Variables: 0
  • Diagnostics log shows only grafema:plugin type nodes as disconnected
  • Source files exist in src/, hooks/src/, compat/src/, etc. but aren't indexed
  • package.json has entries like "main": "dist/preact.js" pointing to unbuilt output
  • Common in: Preact, React, Vue, Babel, and other framework codebases

Solution

Add explicit services array to .grafema/config.yaml:

yaml
services:
  - name: project-core
    path: "src"
    entrypoint: "src/index.js"
  - name: project-hooks
    path: "hooks/src"
    entrypoint: "hooks/src/index.js"
  - name: project-compat
    path: "compat/src"
    entrypoint: "compat/src/index.js"

Key rules:

  1. Services must be an array (not object/dict) — services: [{...}, {...}]
  2. Each service needs name, path, and entrypoint
  3. entrypoint must be a file path (e.g., src/index.js), not a directory
  4. path is the source directory for that service
  5. You can combine with include patterns for additional filtering

Common error: Using object syntax produces:

code
Error: Config error: services must be an array, got object

Verification

After updating config:

bash
grafema analyze --auto-start --clear

Expected: Node count should be in hundreds/thousands (not 29). grafema overview should show non-zero Modules, Functions, Variables.

Example: Preact Configuration

yaml
services:
  - name: preact-core
    path: "src"
    entrypoint: "src/index.js"
  - name: preact-hooks
    path: "hooks/src"
    entrypoint: "hooks/src/index.js"
  - name: preact-compat
    path: "compat/src"
    entrypoint: "compat/src/index.js"
  - name: preact-debug
    path: "debug/src"
    entrypoint: "debug/src/index.js"

plugins:
  discovery: []
  indexing:
    - JSModuleIndexer
  analysis:
    - JSASTAnalyzer
  enrichment:
    - MethodCallResolver
    - ArgumentParameterLinker
    - AliasTracker
    - ClosureCaptureEnricher
    - ImportExportLinker
    - PrefixEvaluator
  validation:
    - GraphConnectivityValidator

Result: 3799 nodes, 5190 edges, 30 modules, 186 functions (vs 29 nodes without services).

Notes

  • The discovery: [] in plugins is normal — it means no auto-discovery plugins run, which is fine when using explicit services
  • For SWE-bench tasks, check package.json entry points first. If they point to dist/, you'll need manual services config
  • Each SWE-bench repo needs its own pre-computed .grafema/ directory
  • Strip unnecessary plugins (Express, Socket.IO, Database analyzers) for frontend libraries to speed up analysis
  • The include pattern alone does NOT fix discovery — it only filters already-discovered files. You need services to tell Grafema WHERE to look.