Skip to content

77 game-dev skills for AI coding agents — Godot, Unity, Unreal, Phaser, PixiJS, three.js, Bevy, pygame, LÖVE, Roblox. Portable SKILL.md Agent Skills (the format Anthropic launched as Claude Skills), with a router that loads the right skill for your engine and task. Runs in Claude Code, Cursor, Kiro, Codex, Copilot, Gemini CLI and more.

Add Unity .meta and missing-script checker to unity-csharp-scripting

Kiro Crewf963124bbcb6
diff --git a/skills/unity/unity-csharp-scripting/SKILL.md b/skills/unity/unity-csharp-scripting/SKILL.md
index 453adb1..6ef1fe8 100644
--- a/skills/unity/unity-csharp-scripting/SKILL.md
+++ b/skills/unity/unity-csharp-scripting/SKILL.md
@@ -42,6 +42,16 @@ plumbing*, not those subsystems.
start them with `StartCoroutine` and stop them deterministically.
6. **Verify in Play mode**: check the Console for null-reference exceptions, confirm values
in the Inspector update as expected, and watch the Profiler if `Update` is hot.
+7. **Check `.meta` files before you finish.** When you create, move, rename or delete
+ scripts or assets outside the Editor, always move a file together with its `.meta`
+ (a new `.meta` means a new GUID and broken references). Then run
+ `python scripts/check_unity_project.py <project-dir>` (`--json` for machine-readable
+ output), fix every `ERROR` (duplicate or malformed GUIDs, files moved without their
+ `.meta`, missing script references) and re-run until it exits 0. A missing `.meta` on a
+ brand-new file is only a warning; Unity creates it on the next import. Run the script
+ path relative to this skill directory, or resolve the installed skill path first. It
+ needs Python 3.10+ and nothing else, reads only text-serialized assets, and does not
+ replace opening the project in Unity.
## Patterns
@@ -128,6 +138,8 @@ private System.Collections.IEnumerator FlashThenHide()
- For the full event-execution-order table and advanced coroutine patterns (custom
`CustomYieldInstruction`, stopping by handle, `WaitUntil`/`WaitWhile`), read
`references/lifecycle-and-coroutines.md`.
+- `scripts/check_unity_project.py` — read-only `.meta`/GUID and missing-script checker;
+ `--help` lists every check and the exit codes.
- Primary docs: Unity Manual "Event function execution order"
(`https://docs.unity3d.com/Manual/execution-order.html`) and `ScriptReference/MonoBehaviour`.
diff --git a/skills/unity/unity-csharp-scripting/scripts/check_unity_project.py b/skills/unity/unity-csharp-scripting/scripts/check_unity_project.py
new file mode 100644
index 0000000..15d1d3b
--- /dev/null
+++ b/skills/unity/unity-csharp-scripting/scripts/check_unity_project.py
@@ -0,0 +1,915 @@
+#!/usr/bin/env python3
+"""Check a Unity 6 project for broken .meta files and missing script references.
+
+Run it after creating, moving, renaming or deleting files outside the Unity
+Editor. Unity has not imported those changes yet, so this looks for the edits
+that will break the project on the next import:
+
+ERRORS (the project will really break)
+ duplicate-guid Two .meta files share a GUID (usually a copied .meta).
+ moved-without-meta A .meta was left behind while its asset moved or was
+ renamed. Unity would give the asset a new GUID and every
+ reference to it would break ("Missing script").
+ malformed-meta A .meta has no valid 32-hex-digit `guid:` line.
+ missing-script A text-serialized scene/prefab/asset has an
+ `m_Script: {fileID: 11500000, guid: X, type: 3}` reference
+ and no .meta in Assets/, Packages/ or the package cache
+ has GUID X. Reported as a warning instead when some
+ packages could not be read (for example when
+ Library/PackageCache does not exist yet).
+
+WARNINGS
+ missing-meta An asset or folder has no .meta yet. This is normal for
+ a new file: Unity creates the .meta on the next import.
+ It does not make --strict fail.
+ orphan-meta A .meta whose asset or folder is gone.
+ meta-name-case A .meta matches its asset only when case is ignored.
+ class-name-mismatch A file declares a MonoBehaviour/ScriptableObject subclass
+ whose name differs from the file name, so Unity cannot
+ attach it.
+
+Only Assets/ and the embedded packages in Packages/ are checked. Library/,
+Temp/, Logs/ and similar folders are never walked, except that .meta GUIDs are
+read from Library/PackageCache and from local `file:` packages so their scripts
+count as present. Files Unity ignores during import (names starting with `.`
+outside Assets/StreamingAssets, names ending with `~`, files and folders named
+`cvs`, `.tmp` files, hidden folders) are skipped. Binary-serialized files (not
+starting with `%YAML`) are skipped.
+
+Standard library only. Read-only: nothing is ever written.
+"""
+
+from __future__ import annotations
+
+import argparse
+import json
+import os
+import re
+import stat
+import sys
+from dataclasses import dataclass, field
+from pathlib import Path
+from typing import Callable
+
+TOOL = "check_unity_project"
+SCHEMA = 1
+
+# Text-serialized Unity files that can hold MonoBehaviour/ScriptableObject
+# documents with an m_Script reference.
+YAML_EXTENSIONS = frozenset({".unity", ".prefab", ".asset", ".controller", ".playable"})
+
+# fileID Unity uses for the MonoScript inside a .cs script asset. Scripts that
+# live in DLLs use other fileIDs, which are not checked.
+MONO_SCRIPT_FILE_ID = "11500000"
+
+# Built-in resources use GUIDs such as 0000000000000000e000000000000000 and
+# 0000000000000000f000000000000000; they have no .meta in the project.
+BUILTIN_GUID_PREFIX = "0" * 16
+
+GUID_LINE_RE = re.compile(r"^guid:\s*(\S*)\s*$")
+GUID_RE = re.compile(r"^[0-9a-fA-F]{32}$")
+FOLDER_LINE_RE = re.compile(r"^folderAsset:\s*yes\s*$")
+YAML_DOC_RE = re.compile(r"^--- !u!(\d+) &(-?\d+)")
+YAML_NAME_RE = re.compile(r"^ m_Name:\s*(.*?)\s*$")
+YAML_GAMEOBJECT_RE = re.compile(r"^ m_GameObject:\s*\{\s*fileID:\s*(-?\d+)")
+YAML_SCRIPT_RE = re.compile(
+ r"m_Script:\s*\{\s*fileID:\s*(-?\d+)\s*,\s*guid:\s*([0-9a-fA-F]{32})\s*,\s*type:\s*(\d+)\s*\}"
+)
+YAML_ANY_GUID_RE = re.compile(r"guid:\s*([0-9a-fA-F]{32})")
+# Text assets can hold single lines of hundreds of MB (mesh or terrain data);
+# they are read in pieces of this size so memory stays flat.
+YAML_CHUNK_BYTES = 64 * 1024
+
+# C# comments and string/char literals, removed before looking for classes.
+CS_NOISE_RE = re.compile(
+ r"//[^\n]*"
+ r"|/\*.*?\*/"
+ r"|@\"(?:[^\"]|\"\")*\""
+ r"|\$?\"(?:\\.|[^\"\\\n])*\""
+ r"|'(?:\\.|[^'\\\n])*'",
+ re.S,
+)
+CS_CLASS_RE = re.compile(
+ r"(?P<mods>(?:\b(?:public|internal|private|protected|sealed|abstract|static|partial|unsafe|new|file)\s+)*)"
+ r"\bclass\s+@?(?P<name>[A-Za-z_]\w*)\s*(?P<generic><[^>{]*>)?\s*:\s*"
+ r"(?:global::)?(?:UnityEngine\s*\.\s*)?(?P<base>MonoBehaviour|ScriptableObject)\b"
+)
+CS_BRACE_RE = re.compile(r"\bnamespace\s+[\w.]+\s*\{|\{|\}")
+CS_MAX_BYTES = 2 * 1024 * 1024
+
+WINDOWS_HIDDEN = getattr(stat, "FILE_ATTRIBUTE_HIDDEN", 2)
+
+
+@dataclass
+class Issue:
+ level: str # "error" or "warning"
+ code: str
+ file: str
+ line: int | None
+ message: str
+ fix: str
+
+ def as_json(self) -> dict:
+ return {"code": self.code, "file": self.file, "line": self.line, "message": self.message, "fix": self.fix}
+
+
+@dataclass
+class Meta:
+ rel: str # posix path of the .meta, relative to the project root
+ dir_rel: str
+ asset_name: str
+ guid: str | None
+ guid_line: int | None
+ is_folder: bool
+ problem: str | None = None # set when the .meta is malformed
+
+
+@dataclass
+class Missing:
+ """An asset or folder that has no .meta next to it."""
+
+ rel: str
+ dir_rel: str
+ name: str
+ is_dir: bool
+ reportable: bool # False when it sits inside a folder that is itself new
+ files_inside: int = 0
+ paired: bool = False
+
+
+@dataclass
+class Scan:
+ root: Path
+ files_checked: int = 0
+ metas: list[Meta] = field(default_factory=list)
+ orphans: list[tuple[Meta, bool]] = field(default_factory=list) # (meta, asset exists but is hidden)
+ missing: list[Missing] = field(default_factory=list)
+ yaml_files: list[Path] = field(default_factory=list)
+ cs_files: list[Path] = field(default_factory=list)
+ issues: list[Issue] = field(default_factory=list)
+
+ def rel(self, path: Path) -> str:
+ try:
+ return path.relative_to(self.root).as_posix()
+ except ValueError:
+ return Path(os.path.relpath(path, self.root)).as_posix()
+
+ def add(self, level: str, code: str, file: str, line: int | None, message: str, fix: str) -> None:
+ self.issues.append(Issue(level, code, file, line, message, fix))
+
+
+# ---------------------------------------------------------------- file walking
+
+
+def is_hidden(entry: os.DirEntry, in_streaming_assets: bool) -> bool:
+ """Apply Unity's import rules for files and folders it ignores."""
+ name = entry.name
+ try:
+ is_dir = entry.is_dir()
+ except OSError:
+ return True
+ if name.endswith("~") or name.lower() == "cvs":
+ return True
+ if name.startswith(".") and not in_streaming_assets:
+ return True
+ if not is_dir and name.lower().endswith(".tmp"):
+ return True
+ if is_dir and os.name == "nt":
+ try:
+ if entry.stat().st_file_attributes & WINDOWS_HIDDEN: # type: ignore[attr-defined]
+ return True
+ except (OSError, AttributeError):
+ pass
+ return False
+
+
+def list_dir(path: Path, in_streaming_assets: bool) -> tuple[list[str], list[str], set[str]]:
+ """Return (visible files, visible folders, every name on disk), sorted."""
+ files: list[str] = []
+ dirs: list[str] = []
+ every: set[str] = set()
+ try:
+ entries = list(os.scandir(path))
+ except OSError:
+ return files, dirs, every
+ for entry in entries:
+ every.add(entry.name)
+ if is_hidden(entry, in_streaming_assets):
+ continue
+ try:
+ if entry.is_dir():
+ dirs.append(entry.name)
+ elif entry.is_file():
+ files.append(entry.name)
+ except OSError:
+ continue
+ return sorted(files), sorted(dirs), every
+
+
+def first_visit(folder: Path, seen: set[str]) -> bool:
+ try:
+ key = os.path.realpath(folder)
+ except OSError:
+ return False
+ if key in seen:
+ return False
+ seen.add(key)
+ return True
+
+
+def read_meta(path: Path) -> tuple[str | None, int | None, bool, str | None]:
+ """Return (guid, guid line, is folder, problem) for one .meta file."""
+ guid = None
+ guid_line = None
+ is_folder = False
+ problem = None
+ try:
+ with path.open("r", encoding="utf-8-sig", errors="replace") as handle:
+ for number, raw in enumerate(handle, start=1):
+ line = raw.rstrip("\r\n")
+ if guid is None:
+ match = GUID_LINE_RE.match(line)
+ if match:
+ guid_line = number
+ value = match.group(1)
+ if not GUID_RE.match(value) or set(value) == {"0"}:
+ problem = f"invalid guid '{value}' (expected 32 hex digits)"
+ else:
+ guid = value.lower()
+ if FOLDER_LINE_RE.match(line):
+ is_folder = True
+ # The header (fileFormatVersion, guid, folderAsset) is at the top.
+ if guid_line is not None and number >= 4:
+ break
+ if number >= 40 and guid_line is None:
+ break
+ except OSError as exc:
+ return None, None, False, f"could not be read ({exc.strerror or exc})"
+ if guid_line is None:
+ problem = "has no 'guid:' line"
+ return guid, guid_line, is_folder, problem
+
+
+def walk_linted(scan: Scan, top: Path) -> None:
+ """Walk Assets/ or one embedded package and record metas and assets."""
+ streaming_root = (scan.root / "Assets" / "StreamingAssets").as_posix()
+ # Stack of (folder, top-most new folder above it, or None).
+ stack: list[tuple[Path, Missing | None]] = [(top, None)]
+ seen: set[str] = set()
+ while stack:
+ folder, new_parent = stack.pop()
+ if not first_visit(folder, seen):
+ continue # a symlink loop
+ folder_posix = folder.as_posix()
+ in_streaming = folder_posix == streaming_root or folder_posix.startswith(streaming_root + "/")
+ files, dirs, every = list_dir(folder, in_streaming)
+ dir_rel = scan.rel(folder)
+ scan.files_checked += len(files)
+ if new_parent is not None:
+ new_parent.files_inside += sum(1 for name in files if not name.endswith(".meta"))
+
+ metas = {name[: -len(".meta")]: name for name in files if name.endswith(".meta") and len(name) > 5}
+ assets = [(name, False) for name in files if not name.endswith(".meta")] + [(name, True) for name in dirs]
+ asset_names = {name for name, _ in assets}
+
+ missing_here: dict[str, Missing] = {}
+ for name, is_dir in assets:
+ if name in metas:
+ continue
+ item = Missing(
+ rel=f"{dir_rel}/{name}",
+ dir_rel=dir_rel,
+ name=name,
+ is_dir=is_dir,
+ reportable=new_parent is None,
+ )
+ missing_here[name] = item
+ scan.missing.append(item)
+
+ lowered_missing = {name.lower(): item for name, item in missing_here.items()}
+ for asset_name, meta_name in metas.items():
+ guid, guid_line, is_folder, problem = read_meta(folder / meta_name)
+ meta = Meta(
+ rel=f"{dir_rel}/{meta_name}",
+ dir_rel=dir_rel,
+ asset_name=asset_name,
+ guid=guid,
+ guid_line=guid_line,
+ is_folder=is_folder,
+ problem=problem,
+ )
+ scan.metas.append(meta)
+ if asset_name in asset_names:
+ continue
+ case_match = lowered_missing.get(asset_name.lower())
+ if case_match is not None and not case_match.paired:
+ case_match.paired = True
+ scan.add(
+ "warning",
+ "meta-name-case",
+ meta.rel,
+ None,
+ f"'{meta_name}' only matches '{case_match.name}' when case is ignored; "
+ "this works on Windows and macOS but the asset loses its GUID on case-sensitive file systems",
+ f"rename '{meta_name}' to '{case_match.name}.meta'",
+ )
+ continue
+ scan.orphans.append((meta, asset_name in every))
+
+ for name, path_name in ((n, folder / n) for n in files):
+ suffix = Path(name).suffix.lower()
+ if suffix in YAML_EXTENSIONS:
+ scan.yaml_files.append(path_name)
+ elif suffix == ".cs":
+ scan.cs_files.append(path_name)
+
+ for name in reversed(dirs):
+ child_new = new_parent
+ if child_new is None and name in missing_here:
+ child_new = missing_here[name]
+ stack.append((folder / name, child_new))
+
+
+def collect_external_guids(path: Path, into: dict[str, str], root: Path) -> None:
+ """Read GUIDs from a package that is not checked itself (cache or local folder)."""
+ stack = [path]
+ seen: set[str] = set()
+ while stack:
+ folder = stack.pop()
+ if not first_visit(folder, seen):
+ continue
+ files, dirs, _ = list_dir(folder, False)
+ for name in files:
+ if name.endswith(".meta"):
+ guid, _, _, _ = read_meta(folder / name)
+ if guid and guid not in into:
+ meta_path = folder / name
+ try:
+ into[guid] = meta_path.relative_to(root).as_posix()
+ except ValueError:
+ into[guid] = Path(os.path.relpath(meta_path, root)).as_posix()
+ stack.extend(folder / name for name in dirs)
+
+
+# ------------------------------------------------------------------- packages
+
+
+def load_json(path: Path) -> dict | None:
+ try:
+ with path.open("r", encoding="utf-8-sig") as handle:
+ data = json.load(handle)
+ except (OSError, ValueError):
+ return None
+ return data if isinstance(data, dict) else None
+
+
+@dataclass
+class Packages:
+ embedded: dict[str, Path] = field(default_factory=dict) # package name -> folder in Packages/
+ external_guids: dict[str, str] = field(default_factory=dict) # guid -> .meta path
+ unscanned: list[str] = field(default_factory=list)
+ note: str = ""
+
+
+def find_packages(root: Path) -> Packages:
+ result = Packages()
+ packages_dir = root / "Packages"
+ if packages_dir.is_dir():
+ _, dirs, _ = list_dir(packages_dir, False)
+ for name in dirs:
+ package_json = packages_dir / name / "package.json"
+ if package_json.is_file():
+ data = load_json(package_json) or {}
+ package_name = data.get("name") if isinstance(data.get("name"), str) else name
+ result.embedded[package_name] = packages_dir / name
+
+ manifest = load_json(packages_dir / "manifest.json")
+ lock = load_json(packages_dir / "packages-lock.json")
+ wanted: dict[str, str] = {} # package name -> version or file: reference
+ if lock and isinstance(lock.get("dependencies"), dict):
+ for name, info in lock["dependencies"].items():
+ if isinstance(info, dict):
+ version = str(info.get("version", ""))
+ if info.get("source") == "embedded":
+ version = "embedded"
+ wanted[name] = version
+ if manifest and isinstance(manifest.get("dependencies"), dict):
+ for name, version in manifest["dependencies"].items():
+ wanted.setdefault(name, str(version))
+ if manifest is None:
+ result.note = "Packages/manifest.json is missing or unreadable, so package scripts could not be checked"
+
+ cache_dir = root / "Library" / "PackageCache"
+ cache_by_name: dict[str, list[Path]] = {}
+ if cache_dir.is_dir():
+ _, dirs, _ = list_dir(cache_dir, False)
+ for name in dirs:
+ cache_by_name.setdefault(name.split("@", 1)[0], []).append(cache_dir / name)
+
+ local_dirs: dict[str, Path] = {}
+ for name, version in sorted(wanted.items()):
+ if name.startswith("com.unity.modules."):
+ continue # built-in engine modules ship no script assets
+ if name in result.embedded or version == "embedded":
+ if name not in result.embedded:
+ result.unscanned.append(name)
+ continue
+ if version.startswith("file:"):
+ target = Path(version[len("file:"):])
+ if not target.is_absolute():
+ target = packages_dir / target
+ if target.is_dir() and (target / "package.json").is_file():
+ local_dirs[name] = target
+ continue
+ # A file: tarball (.tgz) is unpacked into Library/PackageCache.
+ if name not in cache_by_name:
+ result.unscanned.append(name)
+
+ for name, folders in sorted(cache_by_name.items()):
+ if name in result.embedded or name in local_dirs:
+ continue # a stale cache copy of an embedded or local package
+ for folder in folders:
+ collect_external_guids(folder, result.external_guids, root)
+ embedded_dirs = {os.path.realpath(folder) for folder in result.embedded.values()}
+ for name, folder in sorted(local_dirs.items()):
+ if os.path.realpath(folder) in embedded_dirs:
+ continue # already checked as an embedded package
+ collect_external_guids(folder, result.external_guids, root)
+
+ if result.unscanned and not result.note:
+ shown = ", ".join(result.unscanned[:5]) + (", ..." if len(result.unscanned) > 5 else "")
+ where = "Library/PackageCache is missing" if not cache_dir.is_dir() else "they are not in Library/PackageCache"
+ result.note = f"could not read packages {shown} ({where})"
+ return result
+
+
+# --------------------------------------------------------------------- checks
+
+
+def check_metas(scan: Scan, packages: Packages) -> tuple[set[str], dict[str, str]]:
+ """Report malformed, duplicate, orphaned and moved metas.
+
+ Returns (GUIDs whose asset exists, orphan GUID -> orphan .meta path).
+ """
+ live: dict[str, list[Meta]] = {}
+ orphan_metas = {meta.rel for meta, _ in scan.orphans}
+ for meta in scan.metas:
+ if meta.problem:
+ scan.add(
+ "error",
+ "malformed-meta",
+ meta.rel,
+ meta.guid_line,
+ f"'{Path(meta.rel).name}' {meta.problem}",
+ "restore this .meta from version control; if there is no good copy, delete it and Unity "
+ "creates a new one (references to the old GUID stay broken)",
+ )
+ continue
+ if meta.rel not in orphan_metas and meta.guid:
+ live.setdefault(meta.guid, []).append(meta)
+
+ for guid, group in sorted(live.items()):
+ group.sort(key=lambda m: m.rel)
+ first = group[0]
+ for other in group[1:]:
+ scan.add(
+ "error",
+ "duplicate-guid",
+ other.rel,
+ other.guid_line,
+ f"GUID {guid} is also used by '{first.rel}'; one of the two .meta files is a copy",
+ "delete the .meta of the asset that is the copy (keep the original's) so Unity gives the copy "
+ "a new GUID on import",
+ )
+ external = packages.external_guids.get(guid)
+ if external:
+ scan.add(
+ "error",
+ "duplicate-guid",
+ first.rel,
+ first.guid_line,
+ f"GUID {guid} is also used by package file '{external}'",
+ "this .meta was copied from the package; delete it so Unity gives the asset a new GUID",
+ )
+
+ by_dir: dict[str, list[Missing]] = {}
+ by_name: dict[str, list[Missing]] = {}
+ for item in scan.missing:
+ if item.paired:
+ continue
+ by_dir.setdefault(item.dir_rel, []).append(item)
+ by_name.setdefault(item.name, []).append(item)
+
+ orphan_guids: dict[str, str] = {}
+ for meta, asset_hidden in scan.orphans:
+ if meta.problem:
+ continue
+ if meta.guid:
+ orphan_guids.setdefault(meta.guid, meta.rel)
+ asset_rel = f"{meta.dir_rel}/{meta.asset_name}"
+ if asset_hidden:
+ scan.add(
+ "warning",
+ "orphan-meta",
+ meta.rel,
+ None,
+ f"'{meta.asset_name}' exists but Unity ignores it (hidden name), so this .meta is unused",
+ "delete this .meta, or rename the asset so Unity imports it",
+ )
+ continue
+
+ candidates = [
+ item
+ for item in by_dir.get(meta.dir_rel, [])
+ if item.is_dir == meta.is_folder
+ and (item.is_dir or Path(item.name).suffix.lower() == Path(meta.asset_name).suffix.lower())
+ ]
+ candidates += [
+ item
+ for item in by_name.get(meta.asset_name, [])
+ if item.dir_rel != meta.dir_rel and item.is_dir == meta.is_folder
+ ]
+ kind = "folder" if meta.is_folder else "asset"
+ if len(candidates) == 1:
+ target = candidates[0]
+ target.paired = True
+ scan.add(
+ "error",
+ "moved-without-meta",
+ target.rel,
+ None,
+ f"'{target.rel}' has no .meta, and '{meta.rel}' (GUID {meta.guid}) was left behind; "
+ f"this {kind} looks moved or renamed without its .meta, so Unity would give it a new GUID "
+ "and break every reference to it",
+ f"move '{meta.rel}' to '{target.rel}.meta' (do not create a new .meta)",
+ )
+ continue
+ if candidates:
+ names = ", ".join(f"'{item.rel}'" for item in candidates[:3])
+ more = f" and {len(candidates) - 3} more" if len(candidates) > 3 else ""
+ scan.add(
+ "error",
+ "moved-without-meta",
+ meta.rel,
+ None,
+ f"'{meta.rel}' (GUID {meta.guid}) has lost its {kind}, and {names}{more} have no .meta; "
+ "one of them looks moved or renamed without this .meta",
+ "move this .meta next to the file it belongs to and rename it to '<file name>.meta'; "
+ "if the old asset was deleted on purpose, delete this .meta",
+ )
+ continue
+
+ twin = live.get(meta.guid or "")
+ if twin:
+ scan.add(
+ "warning",
+ "orphan-meta",
+ meta.rel,
+ None,
+ f"'{meta.asset_name}' is gone and GUID {meta.guid} already belongs to '{twin[0].rel}'",
+ "delete this leftover .meta",
+ )
+ elif meta.is_folder:
+ scan.add(
+ "warning",
+ "orphan-meta",
+ meta.rel,
+ None,
+ f"folder '{asset_rel}' is gone but its .meta remains; Unity will recreate the folder empty",
+ "if you deleted the folder on purpose, delete this .meta too; if you moved or renamed it, "
+ "move this .meta next to the folder and rename it to match",
+ )
+ else:
+ scan.add(
+ "warning",
+ "orphan-meta",
+ meta.rel,
+ None,
+ f"asset '{asset_rel}' is gone but its .meta remains (GUID {meta.guid}); "
+ "Unity deletes it on the next import",
+ "if you deleted the asset on purpose, delete this .meta too; if you moved or renamed it, "
+ "move this .meta next to the asset and rename it to match",
+ )
+
+ for item in scan.missing:
+ if item.paired or not item.reportable:
+ continue
+ if item.is_dir:
+ count = item.files_inside
+ inside = f" ({count} file{'' if count == 1 else 's'} inside)" if count else ""
+ scan.add(
+ "warning",
+ "missing-meta",
+ item.rel,
+ None,
+ f"folder has no .meta yet{inside}; this is normal for a new folder, "
+ "Unity creates the .meta files on the next import",
+ "nothing to do for a new folder; if it was moved or renamed, move its old .meta next to it instead",
+ )
+ else:
+ scan.add(
+ "warning",
+ "missing-meta",
+ item.rel,
+ None,
+ "no .meta yet; this is normal for a new file, Unity creates it on the next import",
+ "nothing to do for a new file; if it was moved or renamed, move its old .meta next to it instead",
+ )
+
+ return set(live), orphan_guids
+
+
+def is_yaml(path: Path) -> bool:
+ try:
+ with path.open("rb") as handle:
+ head = handle.read(8)
+ except OSError:
+ return False
+ if head.startswith(b"\xef\xbb\xbf"):
+ head = head[3:]
+ return head.startswith(b"%YAML")
+
+
+def check_scripts(scan: Scan, known: set[str], orphan_guids: dict[str, str], packages: Packages) -> None:
+ """Report m_Script references to MonoScript GUIDs that no .meta provides."""
+ for path in scan.yaml_files:
+ if not is_yaml(path):
+ continue # binary serialization
+ rel = scan.rel(path)
+ names: dict[str, str] = {} # GameObject fileID -> m_Name
+ found: dict[str, list] = {} # guid -> [first line, count, set of GameObject ids]
+ doc_class = ""
+ doc_id = ""
+ doc_gameobject = ""
+ try:
+ with path.open("rb") as handle:
+ number = 0
+ at_line_start = True
+ while True:
+ chunk = handle.readline(YAML_CHUNK_BYTES)
+ if not chunk:
+ break
+ starts_line = at_line_start
+ at_line_start = chunk.endswith(b"\n")
+ if not starts_line:
+ continue # the rest of a very long line, such as a mesh data blob
+ number += 1
+ raw = chunk.decode("utf-8", errors="replace")
+ if number == 1:
+ raw = raw.lstrip("\ufeff")
+ if raw.startswith("---"):
+ header = YAML_DOC_RE.match(raw)
+ doc_class, doc_id = (header.group(1), header.group(2)) if header else ("", "")
+ doc_gameobject = ""
+ continue
+ if doc_class == "1":
+ match = YAML_NAME_RE.match(raw.rstrip("\r\n"))
+ if match:
+ names[doc_id] = match.group(1).strip("'\"")
+ continue
+ if "m_GameObject:" in raw:
+ match = YAML_GAMEOBJECT_RE.match(raw)
+ if match:
+ doc_gameobject = match.group(1)
+ continue
+ if "m_Script:" not in raw:
+ continue
+ match = YAML_SCRIPT_RE.search(raw)
+ if not match:
+ continue
+ file_id, guid, ref_type = match.group(1), match.group(2).lower(), match.group(3)
+ if file_id != MONO_SCRIPT_FILE_ID or ref_type != "3":
+ continue
+ if guid.startswith(BUILTIN_GUID_PREFIX) or guid in known:
+ continue
+ entry = found.setdefault(guid, [number, 0, set()])
+ entry[1] += 1
+ if doc_gameobject and doc_gameobject != "0":
+ entry[2].add(doc_gameobject)
+ except OSError:
+ continue
+
+ for guid, (line, count, objects) in found.items():
+ owners = {names[o] for o in objects if o in names}
+ where = ""
+ if owners:
+ shown = ", ".join(f"'{n}'" for n in sorted(owners)[:3])
+ where = f" on {shown}"
+ times = f" ({count} components)" if count > 1 else ""
+ message = f"script reference{where}{times} points to GUID {guid}, which no .meta in the project has"
+ orphan = orphan_guids.get(guid)
+ if orphan:
+ message += f"; its .meta '{orphan}' is left over while the script file is gone"
+ fix = (
+ f"if the .cs file was moved or renamed, put its original .meta (guid: {guid}) back next to it; "
+ "otherwise restore the script, or remove the missing component in the Unity Editor"
+ )
+ if packages.note:
+ scan.add(
+ "warning",
+ "missing-script",
+ rel,
+ line,
+ f"{message} (unconfirmed: {packages.note})",
+ "open the project in Unity once so the packages are downloaded, then run this again; " + fix,
+ )
+ else:
+ scan.add("error", "missing-script", rel, line, message, fix)
+
+
+def strip_cs_noise(text: str) -> str:
+ """Blank out comments and string literals, keeping line numbers."""
+ return CS_NOISE_RE.sub(lambda m: "\n" * m.group(0).count("\n") + " ", text)
+
+
+def top_level_positions(code: str) -> Callable[[int], bool]:
+ """Return a function telling whether an offset is outside every class body."""
+ events: list[tuple[int, str]] = []
+ for match in CS_BRACE_RE.finditer(code):
+ token = match.group(0)
+ events.append((match.start(), "ns" if token.startswith("namespace") else token))
+
+ def is_top_level(offset: int) -> bool:
+ stack: list[str] = []
+ for position, token in events:
+ if position >= offset:
+ break
+ if token == "}":
+ if stack:
+ stack.pop()
+ else:
+ stack.append(token)
+ return all(item == "ns" for item in stack)
+
+ return is_top_level
+
+
+def check_class_names(scan: Scan) -> None:
+ """Warn when a MonoBehaviour/ScriptableObject class does not match its file name."""
+ for path in scan.cs_files:
+ try:
+ if path.stat().st_size > CS_MAX_BYTES:
+ continue
+ text = path.read_text(encoding="utf-8-sig", errors="replace")
+ except OSError:
+ continue
+ code = strip_cs_noise(text.replace("\r\n", "\n"))
+ stem = path.stem
+ is_top_level = top_level_positions(code)
+ candidates = []
+ matched = False
+ for match in CS_CLASS_RE.finditer(code):
+ if not is_top_level(match.start("name")):
+ continue
+ if match.group("name") == stem:
+ matched = True
+ break
+ mods = match.group("mods").split()
+ if "abstract" in mods or "partial" in mods or "static" in mods or match.group("generic"):
+ continue # cannot be attached, or may be split across files
+ candidates.append(match)
+ if matched or not candidates:
+ continue
+ match = candidates[0]
+ name, base = match.group("name"), match.group("base")
+ line = code.count("\n", 0, match.start("name")) + 1
+ scan.add(
+ "warning",
+ "class-name-mismatch",
+ scan.rel(path),
+ line,
+ f"class '{name}' derives from {base} but the file is '{path.name}'; "
+ "Unity only links the script to a class with the same name as the file",
+ f"rename the file to '{name}.cs' (and its .meta to '{name}.cs.meta'), or rename the class to '{stem}'",
+ )
+
+
+# ------------------------------------------------------------------------ CLI
+
+
+def find_project_root(start: Path) -> Path | None:
+ for candidate in (start, *start.parents):
+ if (candidate / "Assets").is_dir() and (candidate / "ProjectSettings").is_dir():
+ return candidate
+ return None
+
+
+def run(root: Path) -> Scan:
+ scan = Scan(root=root)
+ packages = find_packages(root)
+ walk_linted(scan, root / "Assets")
+ for folder in sorted(packages.embedded.values()):
+ walk_linted(scan, folder)
+ known, orphan_guids = check_metas(scan, packages)
+ known |= set(packages.external_guids)
+ check_scripts(scan, known, orphan_guids, packages)
+ check_class_names(scan)
+ return scan
+
+
+def sort_key(issue: Issue) -> tuple:
+ return (0 if issue.level == "error" else 1, issue.file, issue.line or 0, issue.code)
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ prog="check_unity_project.py",
+ description=(
+ "Read-only check of a Unity 6 project for .meta mistakes made outside the Editor:\n"
+ "duplicate or malformed GUIDs, assets moved or renamed without their .meta, leftover\n"
+ ".meta files, missing script references in text scenes/prefabs/assets, and\n"
+ "MonoBehaviour/ScriptableObject classes whose name differs from the file name."
+ ),
+ epilog=(
+ "example:\n"
+ " python scripts/check_unity_project.py path/to/MyGame\n"
+ " python scripts/check_unity_project.py . --json --max-issues 200\n\n"
+ "exit codes: 0 no errors (warnings allowed), 1 errors found (or warnings with --strict),\n"
+ " 2 usage problem or not a Unity project"
+ ),
+ formatter_class=argparse.RawDescriptionHelpFormatter,
+ )
+ parser.add_argument(
+ "project_dir",
+ nargs="?",
+ default=".",
+ metavar="PROJECT_DIR",
+ help="Unity project folder (the one holding Assets/ and ProjectSettings/), or any folder inside it. "
+ "Default: current folder.",
+ )
+ parser.add_argument("--json", action="store_true", help="print one JSON object instead of text")
+ parser.add_argument(
+ "--strict",
+ action="store_true",
+ help="exit 1 when there are warnings too (a new file's missing .meta does not count)",
+ )
+ parser.add_argument(
+ "--max-issues",
+ type=int,
+ default=50,
+ metavar="N",
+ help="print at most N issues, errors first (default: 50; 0 = no limit)",
+ )
+ return parser
+
+
+def main(argv: list[str] | None = None) -> int:
+ for stream in (sys.stdout, sys.stderr):
+ if hasattr(stream, "reconfigure"):
+ stream.reconfigure(errors="replace") # non-ASCII paths on a legacy Windows console
+ parser = build_parser()
+ args = parser.parse_args(argv)
+ if args.max_issues < 0:
+ parser.error("--max-issues must be 0 or more")
+
+ start = Path(args.project_dir).expanduser()
+ if not start.is_dir():
+ print(f"error: '{args.project_dir}' is not a folder", file=sys.stderr)
+ return 2
+ root = find_project_root(start.resolve())
+ if root is None:
+ print(
+ f"error: '{args.project_dir}' is not inside a Unity project "
+ "(no folder with both Assets/ and ProjectSettings/ found)",
+ file=sys.stderr,
+ )
+ return 2
+
+ scan = run(root)
+ issues = sorted(scan.issues, key=sort_key)
+ errors = [i for i in issues if i.level == "error"]
+ warnings = [i for i in issues if i.level == "warning"]
+ limit = args.max_issues or len(issues)
+ shown = issues[:limit]
+ truncated = len(shown) < len(issues)
+
+ if args.json:
+ payload = {
+ "tool": TOOL,
+ "schema": SCHEMA,
+ "root": root.as_posix(),
+ "files_checked": scan.files_checked,
+ "error_count": len(errors),
+ "warning_count": len(warnings),
+ "errors": [i.as_json() for i in shown if i.level == "error"],
+ "warnings": [i.as_json() for i in shown if i.level == "warning"],
+ "truncated": truncated,
+ }
+ print(json.dumps(payload, indent=2))
+ else:
+ for issue in shown:
+ location = f"{issue.file}:{issue.line}" if issue.line else issue.file
+ print(f"{issue.level.upper()} {location}: {issue.message}")
+ print(f" fix: {issue.fix}")
+ if truncated:
+ print(f"... and {len(issues) - len(shown)} more")
+ print(f"Summary: {len(errors)} errors, {len(warnings)} warnings in {scan.files_checked} files checked")
+
+ # A new file's missing .meta needs no action (Unity creates it), so it never fails --strict.
+ if errors or (args.strict and any(w.code != "missing-meta" for w in warnings)):
+ return 1
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/tests/test_check_unity_project.py b/tests/test_check_unity_project.py
new file mode 100644
index 0000000..4a1944d
--- /dev/null
+++ b/tests/test_check_unity_project.py
@@ -0,0 +1,439 @@
+"""End-to-end tests for the unity-csharp-scripting project checker."""
+
+from __future__ import annotations
+
+import json
+import subprocess
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+
+
+ROOT = Path(__file__).resolve().parents[1]
+CHECKER = ROOT / "skills" / "unity" / "unity-csharp-scripting" / "scripts" / "check_unity_project.py"
+
+GUID_PLAYER = "a79441f348de89743a2939f4d699eac1"
+GUID_ENEMY = "0f5a4c1e2b3d4e5f60718293a4b5c6d7"
+GUID_SPRITE = "11112222333344445555666677778888"
+GUID_FOLDER = "99990000aaaabbbbccccddddeeeeffff"
+GUID_GONE = "deadbeefdeadbeefdeadbeefdeadbeef"
+GUID_PACKAGE = "c0ffee00c0ffee00c0ffee00c0ffee00"
+
+MODULES_ONLY = {"dependencies": {"com.unity.modules.physics": "1.0.0"}}
+
+
+def meta_text(guid: str, folder: bool = False) -> str:
+ if folder:
+ return f"fileFormatVersion: 2\nguid: {guid}\nfolderAsset: yes\nDefaultImporter:\n externalObjects: {{}}\n"
+ return (
+ f"fileFormatVersion: 2\nguid: {guid}\nMonoImporter:\n externalObjects: {{}}\n"
+ " serializedVersion: 2\n defaultReferences: []\n executionOrder: 0\n"
+ )
+
+
+def behaviour(name: str) -> str:
+ return f"using UnityEngine;\n\npublic class {name} : MonoBehaviour\n{{\n private void Update() {{ }}\n}}\n"
+
+
+def scene(script_guid: str, file_id: str = "11500000", object_name: str = "Player") -> str:
+ return (
+ "%YAML 1.1\n%TAG !u! tag:unity3d.com,2011:\n"
+ "--- !u!1 &100\nGameObject:\n m_ObjectHideFlags: 0\n"
+ f" m_Name: {object_name}\n"
+ "--- !u!114 &200\nMonoBehaviour:\n m_ObjectHideFlags: 0\n"
+ " m_GameObject: {fileID: 100}\n m_Enabled: 1\n"
+ f" m_Script: {{fileID: {file_id}, guid: {script_guid}, type: 3}}\n"
+ )
+
+
+class CheckUnityProjectTests(unittest.TestCase):
+ def setUp(self) -> None:
+ self.temp_dir = tempfile.TemporaryDirectory()
+ self.root = Path(self.temp_dir.name) / "MyGame"
+ (self.root / "Assets").mkdir(parents=True)
+ (self.root / "ProjectSettings").mkdir()
+ self.write("Packages/manifest.json", json.dumps(MODULES_ONLY))
+ # A small clean project: one folder, one script, one scene using it.
+ self.write("Assets/Scripts.meta", meta_text(GUID_FOLDER, folder=True))
+ self.write("Assets/Scripts/Player.cs", behaviour("Player"))
+ self.write("Assets/Scripts/Player.cs.meta", meta_text(GUID_PLAYER))
+ self.write("Assets/Main.unity", scene(GUID_PLAYER))
+ self.write("Assets/Main.unity.meta", meta_text("1234567890abcdef1234567890abcdef"))
+
+ def tearDown(self) -> None:
+ self.temp_dir.cleanup()
+
+ # helpers
+
+ def write(self, rel: str, text: str, newline: str = "\n", bom: bool = False) -> Path:
+ path = self.root / rel
+ path.parent.mkdir(parents=True, exist_ok=True)
+ data = text.replace("\n", newline).encode("utf-8")
+ path.write_bytes((b"\xef\xbb\xbf" if bom else b"") + data)
+ return path
+
+ def run_tool(self, *args: str, cwd: Path | None = None) -> subprocess.CompletedProcess[str]:
+ return subprocess.run(
+ [sys.executable, str(CHECKER), *args],
+ check=False,
+ capture_output=True,
+ text=True,
+ cwd=cwd,
+ )
+
+ def check(self, *args: str) -> tuple[int, dict]:
+ result = self.run_tool(str(self.root), "--json", "--max-issues", "0", *args)
+ self.assertIn(result.returncode, (0, 1), result.stderr)
+ return result.returncode, json.loads(result.stdout)
+
+ def codes(self, payload: dict, kind: str) -> list[str]:
+ return [issue["code"] for issue in payload[kind]]
+
+ # clean project and CLI
+
+ def test_clean_project_passes(self) -> None:
+ result = self.run_tool(str(self.root))
+ self.assertEqual(result.returncode, 0, result.stdout)
+ self.assertIn("Summary: 0 errors, 0 warnings in 5 files checked", result.stdout)
+
+ def test_json_shape(self) -> None:
+ self.write("Assets/New.txt", "hello")
+ code, payload = self.check()
+ self.assertEqual(code, 0)
+ self.assertEqual(
+ set(payload), {"tool", "schema", "root", "files_checked", "error_count", "warning_count", "errors", "warnings", "truncated"}
+ )
+ self.assertEqual(payload["tool"], "check_unity_project")
+ self.assertEqual(payload["schema"], 1)
+ self.assertFalse(payload["truncated"])
+ issue = payload["warnings"][0]
+ self.assertEqual(set(issue), {"code", "file", "line", "message", "fix"})
+ self.assertEqual(issue["file"], "Assets/New.txt")
+ self.assertIsNone(issue["line"])
+
+ def test_not_a_unity_project_exits_2(self) -> None:
+ other = Path(self.temp_dir.name) / "plain"
+ other.mkdir()
+ result = self.run_tool(str(other))
+ self.assertEqual(result.returncode, 2)
+ self.assertIn("not inside a Unity project", result.stderr)
+ self.assertEqual(self.run_tool(str(other / "missing")).returncode, 2)
+
+ def test_finds_root_from_a_subfolder_and_defaults_to_cwd(self) -> None:
+ result = self.run_tool(str(self.root / "Assets" / "Scripts"))
+ self.assertEqual(result.returncode, 0, result.stdout)
+ result = self.run_tool(cwd=self.root)
+ self.assertEqual(result.returncode, 0, result.stdout)
+ self.assertIn("Summary: 0 errors", result.stdout)
+
+ def test_help_has_an_example(self) -> None:
+ result = self.run_tool("--help")
+ self.assertEqual(result.returncode, 0)
+ self.assertIn("check_unity_project.py path/to/MyGame", result.stdout)
+
+ def test_never_writes_anything(self) -> None:
+ self.write("Assets/New.cs", behaviour("New"))
+ self.write("Assets/Old.cs.meta", meta_text(GUID_GONE))
+ before = sorted((p.as_posix(), p.stat().st_mtime_ns) for p in self.root.rglob("*"))
+ self.run_tool(str(self.root))
+ after = sorted((p.as_posix(), p.stat().st_mtime_ns) for p in self.root.rglob("*"))
+ self.assertEqual(before, after)
+
+ def test_max_issues_truncates(self) -> None:
+ for index in range(5):
+ self.write(f"Assets/new_{index}.txt", "x")
+ result = self.run_tool(str(self.root), "--max-issues", "2")
+ self.assertEqual(result.returncode, 0)
+ self.assertEqual(result.stdout.count("WARNING "), 2)
+ self.assertIn("... and 3 more", result.stdout)
+ self.assertIn("Summary: 0 errors, 5 warnings", result.stdout)
+ payload = json.loads(self.run_tool(str(self.root), "--json", "--max-issues", "2").stdout)
+ self.assertTrue(payload["truncated"])
+ self.assertEqual(len(payload["warnings"]), 2)
+
+ def test_errors_print_before_warnings(self) -> None:
+ self.write("Assets/aaa.txt", "new file")
+ self.write("Assets/zzz.txt.meta", "fileFormatVersion: 2\n")
+ result = self.run_tool(str(self.root))
+ self.assertEqual(result.returncode, 1)
+ lines = [line for line in result.stdout.splitlines() if line.startswith(("ERROR", "WARNING"))]
+ self.assertTrue(lines[0].startswith("ERROR Assets/zzz.txt.meta: "), lines)
+ self.assertTrue(lines[1].startswith("WARNING Assets/aaa.txt: "), lines)
+ self.assertIn(" fix: ", result.stdout)
+
+ # missing .meta and moves
+
+ def test_new_file_without_meta_is_only_a_warning(self) -> None:
+ self.write("Assets/Scripts/Enemy.cs", behaviour("Enemy"))
+ code, payload = self.check()
+ self.assertEqual(code, 0)
+ self.assertEqual(payload["errors"], [])
+ self.assertEqual(self.codes(payload, "warnings"), ["missing-meta"])
+ self.assertIn("normal for a new file", payload["warnings"][0]["message"])
+ # Nothing to do for a new file, so --strict does not fail on it ...
+ strict = self.run_tool(str(self.root), "--strict")
+ self.assertEqual(strict.returncode, 0, strict.stdout)
+ # ... but it still fails on any other warning.
+ self.write("Assets/Old.png.meta", meta_text(GUID_GONE))
+ strict = self.run_tool(str(self.root), "--strict")
+ self.assertEqual(strict.returncode, 1, strict.stdout)
+
+ def test_new_folder_is_reported_once(self) -> None:
+ self.write("Assets/Art/Hero.png", "png")
+ self.write("Assets/Art/Sub/Tile.png", "png")
+ code, payload = self.check()
+ self.assertEqual(code, 0)
+ self.assertEqual([w["file"] for w in payload["warnings"]], ["Assets/Art"])
+ self.assertIn("2 files inside", payload["warnings"][0]["message"])
+
+ def test_rename_without_meta_is_an_error(self) -> None:
+ (self.root / "Assets/Scripts/Player.cs").rename(self.root / "Assets/Scripts/Hero.cs")
+ code, payload = self.check()
+ self.assertEqual(code, 1)
+ moved = [e for e in payload["errors"] if e["code"] == "moved-without-meta"]
+ self.assertEqual(len(moved), 1)
+ self.assertEqual(moved[0]["file"], "Assets/Scripts/Hero.cs")
+ self.assertIn("Assets/Scripts/Player.cs.meta", moved[0]["fix"])
+ self.assertIn("Assets/Scripts/Hero.cs.meta", moved[0]["fix"])
+ # The scene still points at the old GUID, whose .meta is now a leftover.
+ missing = [e for e in payload["errors"] if e["code"] == "missing-script"]
+ self.assertEqual(len(missing), 1)
+ self.assertIn("Player.cs.meta", missing[0]["message"])
+ # The renamed file is not also reported as an ordinary new file.
+ self.assertNotIn("missing-meta", self.codes(payload, "warnings"))
+
+ def test_move_to_another_folder_without_meta_is_an_error(self) -> None:
+ self.write("Assets/Gameplay.meta", meta_text("abababababababababababababababab", folder=True))
+ (self.root / "Assets/Gameplay").mkdir()
+ (self.root / "Assets/Scripts/Player.cs").rename(self.root / "Assets/Gameplay/Player.cs")
+ code, payload = self.check()
+ self.assertEqual(code, 1)
+ moved = [e for e in payload["errors"] if e["code"] == "moved-without-meta"]
+ self.assertEqual([e["file"] for e in moved], ["Assets/Gameplay/Player.cs"])
+
+ def test_folder_renamed_without_meta_is_an_error(self) -> None:
+ (self.root / "Assets/Scripts").rename(self.root / "Assets/Code")
+ code, payload = self.check()
+ self.assertEqual(code, 1)
+ moved = [e for e in payload["errors"] if e["code"] == "moved-without-meta"]
+ self.assertEqual([e["file"] for e in moved], ["Assets/Code"])
+ self.assertIn("Assets/Scripts.meta", moved[0]["fix"])
+
+ def test_orphan_meta_alone_is_a_warning(self) -> None:
+ self.write("Assets/Old.png.meta", meta_text(GUID_GONE))
+ self.write("Assets/OldFolder.meta", meta_text(GUID_SPRITE, folder=True))
+ code, payload = self.check()
+ self.assertEqual(code, 0)
+ self.assertEqual(payload["errors"], [])
+ by_file = {w["file"]: w for w in payload["warnings"]}
+ self.assertEqual(by_file["Assets/Old.png.meta"]["code"], "orphan-meta")
+ self.assertIn("recreate the folder", by_file["Assets/OldFolder.meta"]["message"])
+
+ def test_new_file_of_other_type_is_not_paired_with_orphan(self) -> None:
+ self.write("Assets/Old.png.meta", meta_text(GUID_GONE))
+ self.write("Assets/Notes.txt", "new")
+ code, payload = self.check()
+ self.assertEqual(code, 0)
+ self.assertEqual(sorted(self.codes(payload, "warnings")), ["missing-meta", "orphan-meta"])
+
+ def test_meta_name_case_mismatch_is_a_warning(self) -> None:
+ self.write("Assets/Sprite.png", "png")
+ self.write("Assets/sprite.png.meta", meta_text(GUID_SPRITE))
+ code, payload = self.check()
+ self.assertEqual(code, 0)
+ self.assertEqual(self.codes(payload, "warnings"), ["meta-name-case"])
+
+ # meta contents
+
+ def test_duplicate_guid_is_an_error(self) -> None:
+ self.write("Assets/Scripts/PlayerCopy.cs", behaviour("PlayerCopy"))
+ self.write("Assets/Scripts/PlayerCopy.cs.meta", meta_text(GUID_PLAYER))
+ code, payload = self.check()
+ self.assertEqual(code, 1)
+ self.assertEqual(self.codes(payload, "errors"), ["duplicate-guid"])
+ self.assertEqual(payload["errors"][0]["line"], 2)
+
+ def test_meta_copied_from_a_package_is_a_duplicate(self) -> None:
+ self.write("Library/PackageCache/com.example.tools@1a2b3c/Runtime/Tool.cs", behaviour("Tool"))
+ self.write("Library/PackageCache/com.example.tools@1a2b3c/Runtime/Tool.cs.meta", meta_text(GUID_PACKAGE))
+ self.write("Assets/Tool.cs", behaviour("Tool"))
+ self.write("Assets/Tool.cs.meta", meta_text(GUID_PACKAGE))
+ code, payload = self.check()
+ self.assertEqual(code, 1)
+ self.assertEqual(self.codes(payload, "errors"), ["duplicate-guid"])
+ self.assertIn("Library/PackageCache/com.example.tools@1a2b3c", payload["errors"][0]["message"])
+
+ def test_malformed_meta_is_an_error(self) -> None:
+ self.write("Assets/Broken.txt", "x")
+ self.write("Assets/Broken.txt.meta", "fileFormatVersion: 2\nguid: not-a-guid\n")
+ self.write("Assets/NoGuid.txt", "x")
+ self.write("Assets/NoGuid.txt.meta", "fileFormatVersion: 2\n")
+ code, payload = self.check()
+ self.assertEqual(code, 1)
+ errors = {e["file"]: e for e in payload["errors"]}
+ self.assertEqual(errors["Assets/Broken.txt.meta"]["code"], "malformed-meta")
+ self.assertEqual(errors["Assets/Broken.txt.meta"]["line"], 2)
+ self.assertEqual(errors["Assets/NoGuid.txt.meta"]["code"], "malformed-meta")
+
+ def test_crlf_and_bom_metas_are_read(self) -> None:
+ self.write("Assets/Scripts/Player.cs.meta", meta_text(GUID_PLAYER), newline="\r\n", bom=True)
+ self.write("Assets/Main.unity", scene(GUID_PLAYER), newline="\r\n", bom=True)
+ code, payload = self.check()
+ self.assertEqual(code, 0, payload)
+ self.assertEqual(payload["warnings"], [])
+
+ # script references
+
+ def test_missing_script_is_an_error(self) -> None:
+ self.write("Assets/Level.unity", scene(GUID_ENEMY, object_name="Goblin"))
+ self.write("Assets/Level.unity.meta", meta_text("abcdefabcdefabcdefabcdefabcdefab"))
+ code, payload = self.check()
+ self.assertEqual(code, 1)
+ self.assertEqual(self.codes(payload, "errors"), ["missing-script"])
+ error = payload["errors"][0]
+ self.assertEqual(error["file"], "Assets/Level.unity")
+ self.assertEqual(error["line"], 12)
+ self.assertIn("'Goblin'", error["message"])
+ self.assertIn(GUID_ENEMY, error["fix"])
+
+ def test_script_in_embedded_package_or_cache_is_found(self) -> None:
+ self.write("Packages/com.me.combat/package.json", json.dumps({"name": "com.me.combat"}))
+ self.write("Packages/com.me.combat/package.json.meta", meta_text("12121212121212121212121212121212"))
+ self.write("Packages/com.me.combat/Enemy.cs", behaviour("Enemy"))
+ self.write("Packages/com.me.combat/Enemy.cs.meta", meta_text(GUID_ENEMY))
+ self.write("Library/PackageCache/com.example.ui@abc/Button.cs", behaviour("Button"))
+ self.write("Library/PackageCache/com.example.ui@abc/Button.cs.meta", meta_text(GUID_PACKAGE))
+ manifest = {"dependencies": {"com.example.ui": "1.0.0", "com.unity.modules.ui": "1.0.0"}}
+ self.write("Packages/manifest.json", json.dumps(manifest))
+ self.write("Assets/Level.prefab", scene(GUID_ENEMY) + scene(GUID_PACKAGE).split("\n", 2)[2])
+ self.write("Assets/Level.prefab.meta", meta_text("abcdefabcdefabcdefabcdefabcdefab"))
+ code, payload = self.check()
+ self.assertEqual(code, 0, payload)
+ self.assertEqual(payload["warnings"], [])
+
+ def test_tarball_and_nested_local_packages_are_read(self) -> None:
+ self.write("Library/PackageCache/com.me.tar@1a2b3c4d/Tool.cs.meta", meta_text(GUID_PACKAGE))
+ self.write("Packages/Local/com.me.local/package.json", json.dumps({"name": "com.me.local"}))
+ self.write("Packages/Local/com.me.local/Enemy.cs.meta", meta_text(GUID_ENEMY))
+ manifest = {
+ "dependencies": {
+ "com.me.tar": "file:../Tarballs/com.me.tar-1.0.0.tgz",
+ "com.me.local": "file:Local/com.me.local",
+ }
+ }
+ self.write("Packages/manifest.json", json.dumps(manifest))
+ self.write("Assets/Level.prefab", scene(GUID_ENEMY) + scene(GUID_PACKAGE).split("\n", 2)[2])
+ self.write("Assets/Level.prefab.meta", meta_text("abcdefabcdefabcdefabcdefabcdefab"))
+ code, payload = self.check()
+ self.assertEqual(code, 0, payload)
+ self.assertEqual(payload["warnings"], [])
+
+ def test_very_long_lines_are_streamed(self) -> None:
+ blob = "ab" * (8 * 1024 * 1024) # one 16 MB line, like text-serialized mesh data
+ text = scene(GUID_PLAYER).replace("--- !u!114", f" _typelessdata: {blob}\n--- !u!114")
+ text += scene(GUID_ENEMY).split("\n", 2)[2]
+ self.write("Assets/Main.unity", text)
+ del blob, text # so the child process does not start out sharing them
+ code, payload = self.check()
+ self.assertEqual(code, 1)
+ self.assertEqual(self.codes(payload, "errors"), ["missing-script"])
+ self.assertEqual(payload["errors"][0]["line"], 23)
+ self.assertIn(GUID_ENEMY, payload["errors"][0]["message"])
+ if not Path("/proc/self/status").is_file():
+ return
+ # Peak memory of the checker itself (VmHWM is reset when the process starts).
+ probe = (
+ "import runpy, sys\n"
+ f"sys.argv = [{str(CHECKER)!r}, {str(self.root)!r}]\n"
+ "try:\n runpy.run_path(sys.argv[0], run_name='__main__')\nexcept SystemExit:\n pass\n"
+ "peak = [l for l in open('/proc/self/status') if l.startswith('VmHWM:')][0].split()[1]\n"
+ "print('PEAK', peak)\n"
+ )
+ result = subprocess.run([sys.executable, "-c", probe], capture_output=True, text=True, check=False)
+ peak_kb = int(result.stdout.rsplit("PEAK", 1)[1])
+ self.assertLess(peak_kb, 40 * 1024, "the long line was read into memory at once")
+
+ def test_missing_script_is_a_warning_when_packages_are_unread(self) -> None:
+ self.write("Packages/manifest.json", json.dumps({"dependencies": {"com.unity.ugui": "2.0.0"}}))
+ self.write("Assets/Level.unity", scene(GUID_ENEMY))
+ self.write("Assets/Level.unity.meta", meta_text("abcdefabcdefabcdefabcdefabcdefab"))
+ code, payload = self.check()
+ self.assertEqual(code, 0)
+ self.assertEqual(self.codes(payload, "warnings"), ["missing-script"])
+ self.assertIn("Library/PackageCache is missing", payload["warnings"][0]["message"])
+
+ def test_builtin_and_dll_script_references_are_ignored(self) -> None:
+ text = scene("0000000000000000e000000000000000") + scene(GUID_ENEMY, file_id="-765806418").split("\n", 2)[2]
+ self.write("Assets/Level.unity", text)
+ self.write("Assets/Level.unity.meta", meta_text("abcdefabcdefabcdefabcdefabcdefab"))
+ code, payload = self.check()
+ self.assertEqual(code, 0, payload)
+ self.assertEqual(payload["warnings"], [])
+
+ def test_binary_scene_is_skipped(self) -> None:
+ path = self.root / "Assets/Binary.unity"
+ path.write_bytes(b"\x00\x00\x00\x10" + f"m_Script: {{fileID: 11500000, guid: {GUID_ENEMY}, type: 3}}".encode())
+ self.write("Assets/Binary.unity.meta", meta_text("abcdefabcdefabcdefabcdefabcdefab"))
+ code, payload = self.check()
+ self.assertEqual(code, 0, payload)
+
+ # what is and is not scanned
+
+ def test_hidden_assets_are_ignored(self) -> None:
+ self.write("Assets/.hidden.txt", "x")
+ self.write("Assets/Docs~/readme.md", "x")
+ self.write("Assets/cvs/entries", "x")
+ self.write("Assets/build.tmp", "x")
+ self.write("Assets/Docs~/Old.cs.meta", meta_text(GUID_GONE))
+ code, payload = self.check()
+ self.assertEqual(code, 0, payload)
+ self.assertEqual(payload["warnings"], [])
+
+ def test_dot_files_in_streaming_assets_are_not_hidden(self) -> None:
+ self.write("Assets/StreamingAssets.meta", meta_text(GUID_SPRITE, folder=True))
+ self.write("Assets/StreamingAssets/.config", "x")
+ code, payload = self.check()
+ self.assertEqual([w["file"] for w in payload["warnings"]], ["Assets/StreamingAssets/.config"])
+
+ def test_library_and_temp_are_not_walked(self) -> None:
+ self.write("Library/Stuff/file.txt", "x")
+ self.write("Temp/scene.unity", scene(GUID_ENEMY))
+ self.write("Logs/Editor.log", "x")
+ code, payload = self.check()
+ self.assertEqual(code, 0, payload)
+ self.assertEqual(payload["warnings"], [])
+
+ # class names
+
+ def test_class_name_mismatch_is_a_warning(self) -> None:
+ self.write("Assets/Scripts/Player.cs", behaviour("PlayerController"))
+ code, payload = self.check()
+ self.assertEqual(code, 0)
+ self.assertEqual(self.codes(payload, "warnings"), ["class-name-mismatch"])
+ warning = payload["warnings"][0]
+ self.assertEqual(warning["line"], 3)
+ self.assertIn("PlayerController.cs", warning["fix"])
+
+ def test_class_name_check_avoids_false_positives(self) -> None:
+ cases = {
+ "Matching.cs": "namespace Game { public class Matching : UnityEngine.MonoBehaviour { } }",
+ "Data.cs": "using UnityEngine;\npublic class Data : ScriptableObject { }\nclass Helper : MonoBehaviour { }",
+ "Base.cs": "using UnityEngine;\npublic abstract class EnemyBase : MonoBehaviour { }",
+ "Gen.cs": "using UnityEngine;\npublic class Pool<T> : MonoBehaviour { }",
+ "Part.Generated.cs": "using UnityEngine;\npublic partial class Part : MonoBehaviour { }",
+ "Outer.cs": "using UnityEngine;\npublic class Outer\n{\n public class Inner : MonoBehaviour { }\n}",
+ "Comment.cs": "// class Wrong : MonoBehaviour\nvar s = \"class Wrong : MonoBehaviour\";\n",
+ "Indirect.cs": "public class Boss : EnemyBase { }",
+ "Scoped.cs": "namespace Game;\nusing UnityEngine;\npublic sealed class Scoped : MonoBehaviour { }",
+ }
+ for index, (name, text) in enumerate(cases.items()):
+ self.write(f"Assets/Scripts/{name}", text)
+ self.write(f"Assets/Scripts/{name}.meta", meta_text(f"{index + 1:x}".rjust(32, "a")))
+ code, payload = self.check()
+ self.assertEqual(code, 0, payload)
+ self.assertEqual(payload["warnings"], [])
+
+
+if __name__ == "__main__":
+ unittest.main()