# Autofix Gn Scope

> Fixes scope and variable errors in BUILD.gn files.

- Skill: `hyz0906/autofix-gn-scope` (Agent Skill)
- Install (CLI): `npx skillmds@latest add hyz0906/autofix-gn-scope`
- Raw SKILL.md: https://api.skillmd.com/api/skills/hyz0906/autofix-gn-scope/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: hyz0906 (https://skillmd.com/u/hyz0906)
- Updated: 2026-09-10
- Page: https://skillmd.com/skills/hyz0906/autofix-gn-scope

---


# GN Scope Skill

Fixes GN build file errors related to undefined variables or scope issues.

## Detection Patterns
- `Undefined identifier`
- `Assignment had no effect`
- `Can't load buildconfig`
- `Unknown variable`

## Strategy

1. **Identify Variable**: Find the undefined or misused variable.
2. **Check Scope**: Determine if it's a scope or declaration issue.
3. **Fix Definition**: Add declaration or fix scope reference.

## Instructions

### Step 1: Parse the Error
From: `Undefined identifier: my_sources`
Variable: `my_sources`

### Step 2: Common Fixes

**Undefined variable - add declaration:**
```gn
# Before
sources = my_sources  # ERROR: undefined

# After
my_sources = [ "main.cpp" ]
sources = my_sources
```

**Using variable before definition:**
```gn
# Before (error)
executable("app") {
  sources = common_sources  # ERROR
}
common_sources = [ "util.cpp" ]

# After (move declaration up)
common_sources = [ "util.cpp" ]
executable("app") {
  sources = common_sources
}
```

**Scope issue with inner blocks:**
```gn
# Variables defined in if blocks may not be visible outside
if (is_debug) {
  debug_flags = [ "-g" ]
}
# debug_flags might be undefined here if is_debug is false
```

### Step 3: GN Best Practices
- Declare variables before use
- Use `defined()` to check optional variables
- Import shared configs with `import("path")`

