← Writing

models/ in .gitignore Matches at Any Depth

3 min read#Git#CI#Autarkia

CI reported No module named core.models and it was put down to environment differences; the next day Android CI failed with over a thousand unresolved references, and the cause was one models/ line in the root .gitignore matching every directory named models. Two more rules have the same shape, and one still swallows source files.

On September 9 Core CI failed during test collection with No module named 'core.models'. The verdict was that CI is a slim environment without that optional module and my local setup simply had more installed; the fix rewrote the test to check function signatures with AST instead of importing. CI went green, and the explanation held together.

The next day Android CI failed at the Kotlin compile step with over a thousand Unresolved reference errors: models, EmotionType, DeviceContext… Android Studio compiled it fine locally. Following import ...domain.models.* to GitHub, the directory wasn’t there. git check-ignore -v on a file inside it pointed at one line, models/, in the root .gitignore. core.models was the same: it had never been in the repository.

The root .gitignore had these lines since June:

cache/
models/
models/**/*
*_token*

They were meant for the root models directory with weights, the root cache directory, and credential files with token in their names. But in gitignore, a pattern with no slash (a trailing one doesn’t count) matches by name at any depth. models/ means every directory called models: Android’s domain/models/, Python’s core/models/, iOS’s Models/, all kept out of the repository. Matching only the root one takes /models/.

An empty repository shows the difference:

bash
$ mkdir -p models app/domain/models core/cache src
$ touch models/w.bin app/domain/models/User.kt core/cache/m.py src/bpe_tokenizer.cpp
$ printf 'models/\ncache/\n*_token*\n' > .gitignore
$ git ls-files -i -o --exclude-standard
app/domain/models/User.kt
core/cache/m.py
models/w.bin
src/bpe_tokenizer.cpp

$ printf '/models/\n/cache/\n' > .gitignore
$ git ls-files -i -o --exclude-standard
models/w.bin

Ignored files stay in the local working tree, so the editor, local runs and local tests all work, and git status doesn’t even list them as untracked. Android CI was only added on August 17; before that nothing compiled Android from a clean checkout, and the Python failure had been explained away as an environment difference.

01The other two rules

The September 10 fix changed only models/ to /models/. On October 22, *_token* struck: it had swallowed the files for token usage statistics. That time the code was renamed to avoid _token and two test files got ! exceptions; the rule itself stayed.

Listing every ignored source file on HEAD recently turned up two more. Two .py files under core/cache/ are swallowed by cache/ on line 1, and the importer says except ImportError: manager.search_cache = None, so in any environment checked out from git the search cache is silently off. cpp_fast_tokenizer/src/bpe_tokenizer.cpp and its .h are swallowed by *_token*, while the extension’s CMakeLists.txt references src/bpe_tokenizer.cpp directly, so it can’t build from a clean checkout.

They all call for the same change: a leading slash for anything meant only for the root, and credential rules narrowed to the actual file shapes, such as *.token or *_token.json. Plus a CI step that fails when a source file is ignored:

bash
git ls-files -i -o --exclude-standard \
  | grep -E '\.(py|kt|swift|cpp|h|ts)$' \
  | grep -vE '(^|/)(venv[^/]*|node_modules|build[^/]*|_deps)/' \
  && { echo "源码文件被 .gitignore 忽略"; exit 1; } || true