|
| 1 | +id: runner-environment-434 |
| 2 | +title: 'actions/checkout sparse-checkout Cone Mode Does Not Include Root Files by Default' |
| 3 | +category: runner-environment |
| 4 | +severity: silent-failure |
| 5 | +tags: |
| 6 | + - checkout |
| 7 | + - sparse-checkout |
| 8 | + - cone-mode |
| 9 | + - monorepo |
| 10 | + - root-files |
| 11 | + - missing-files |
| 12 | +patterns: |
| 13 | + - regex: 'sparse-checkout-cone-mode:\s*true' |
| 14 | + flags: 'i' |
| 15 | + - regex: 'sparse-checkout:\s*true' |
| 16 | + flags: 'i' |
| 17 | + - regex: 'sparse.checkout.*cone' |
| 18 | + flags: 'i' |
| 19 | +error_messages: |
| 20 | + - "sparse-checkout cone mode: true — root files not found" |
| 21 | + - "Repository root files missing after checkout with sparse-checkout-cone-mode: true" |
| 22 | + - "File not found: package.json (or README.md, .gitignore, etc.) after sparse checkout" |
| 23 | +root_cause: | |
| 24 | + When using `actions/checkout` with `sparse-checkout: true` and |
| 25 | + `sparse-checkout-cone-mode: true`, root-level files (files located directly in the |
| 26 | + repository root, NOT inside any subdirectory) may not be included in the checkout |
| 27 | + unless they are explicitly specified in `sparse-checkout-patterns`. |
| 28 | +
|
| 29 | + Git's cone mode documentation states: "files in the root directory are always included." |
| 30 | + However, the `actions/checkout` action generates cone-mode patterns programmatically |
| 31 | + from `sparse-checkout-patterns`, and the generated patterns may be overly restrictive — |
| 32 | + effectively "locking out" root-level files when the pattern list only names subdirectories |
| 33 | + (`src/`, `libs/`, etc.) without also including a root wildcard. |
| 34 | +
|
| 35 | + **Example:** If `sparse-checkout-patterns` lists only `src/` and `packages/`, the |
| 36 | + generated cone-mode patterns may exclude root files such as: |
| 37 | + - `package.json`, `yarn.lock`, `pnpm-lock.yaml` — build tool manifests |
| 38 | + - `.gitignore`, `.gitattributes`, `.npmrc` — repository configuration |
| 39 | + - `README.md`, `LICENSE` — documentation |
| 40 | + - `Makefile`, `Dockerfile`, `docker-compose.yml` — build/deploy configs |
| 41 | +
|
| 42 | + This causes silent build failures downstream when tools (npm install, yarn, make, etc.) |
| 43 | + can't find their configuration files in the expected root location. |
| 44 | +
|
| 45 | + **Why it's a silent failure:** The checkout step succeeds without error. Files are simply |
| 46 | + absent from the working directory. Build tools then fail with "file not found" errors |
| 47 | + that appear unrelated to the sparse checkout configuration. |
| 48 | +
|
| 49 | + Source: actions/checkout#2366 (Jan 2026, 2 reactions) — self-hosted machine, cone mode |
| 50 | + actively contradicts documented Git behavior for root files. |
| 51 | +fix: | |
| 52 | + Add `/*` to your `sparse-checkout-patterns` list to explicitly include all root-level files. |
| 53 | + The `/*` pattern matches all files in the root directory (depth 1, non-recursive) while |
| 54 | + leaving subdirectory traversal controlled by your other patterns. |
| 55 | +
|
| 56 | + ```yaml |
| 57 | + - uses: actions/checkout@v4 |
| 58 | + with: |
| 59 | + sparse-checkout: | |
| 60 | + /* |
| 61 | + src/ |
| 62 | + packages/ |
| 63 | + sparse-checkout-cone-mode: true |
| 64 | + ``` |
| 65 | +
|
| 66 | + This ensures root files are always checked out, while still limiting deep checkout to |
| 67 | + only the subdirectories you specify. |
| 68 | +
|
| 69 | + **Alternative: use non-cone mode** if you need precise file selection without cone |
| 70 | + mode's subdirectory semantics: |
| 71 | + ```yaml |
| 72 | + - uses: actions/checkout@v4 |
| 73 | + with: |
| 74 | + sparse-checkout: | |
| 75 | + package.json |
| 76 | + yarn.lock |
| 77 | + src/ |
| 78 | + sparse-checkout-cone-mode: false |
| 79 | + ``` |
| 80 | + Non-cone mode uses standard gitignore-style patterns rather than cone-mode's |
| 81 | + directory-boundary semantics. |
| 82 | +fix_code: |
| 83 | + - language: yaml |
| 84 | + label: 'Broken: cone mode silently excludes root files when only subdirs are listed' |
| 85 | + code: | |
| 86 | + - uses: actions/checkout@v4 |
| 87 | + with: |
| 88 | + sparse-checkout: | |
| 89 | + src/ |
| 90 | + packages/ |
| 91 | + sparse-checkout-cone-mode: true |
| 92 | + # ❌ Root files (package.json, .gitignore, Makefile, etc.) |
| 93 | + # may be missing — cone mode generates patterns that exclude root |
| 94 | + # unless root files are explicitly requested |
| 95 | +
|
| 96 | + # Downstream failure: |
| 97 | + - run: npm install # Error: ENOENT: no such file or directory, open 'package.json' |
| 98 | + - language: yaml |
| 99 | + label: 'Fixed: add /* to explicitly include all root-level files' |
| 100 | + code: | |
| 101 | + - uses: actions/checkout@v4 |
| 102 | + with: |
| 103 | + sparse-checkout: | |
| 104 | + /* |
| 105 | + src/ |
| 106 | + packages/ |
| 107 | + sparse-checkout-cone-mode: true |
| 108 | + # ✅ /* matches all files in the root directory (non-recursive) |
| 109 | + # src/ and packages/ are still the only subdirectories checked out |
| 110 | +
|
| 111 | + - run: npm install # Now works — package.json found at root |
| 112 | + - language: yaml |
| 113 | + label: 'Alternative: non-cone mode with explicit file list' |
| 114 | + code: | |
| 115 | + - uses: actions/checkout@v4 |
| 116 | + with: |
| 117 | + sparse-checkout: | |
| 118 | + package.json |
| 119 | + yarn.lock |
| 120 | + .gitignore |
| 121 | + .npmrc |
| 122 | + src/ |
| 123 | + packages/ |
| 124 | + sparse-checkout-cone-mode: false |
| 125 | + # Non-cone mode uses gitignore-style patterns — more explicit |
| 126 | + # but each root file must be listed individually |
| 127 | +prevention: |
| 128 | + - 'Always add `/*` to `sparse-checkout-patterns` when using `sparse-checkout-cone-mode: true` to ensure root files are included.' |
| 129 | + - 'After setting up sparse checkout, verify that expected root files (package.json, Makefile, etc.) exist before running build steps.' |
| 130 | + - 'If root file inclusion is uncertain, add a validation step: `ls package.json yarn.lock || echo "WARNING: root files missing"` before builds.' |
| 131 | + - 'When in doubt, prefer `sparse-checkout-cone-mode: false` (non-cone mode) with explicit file patterns — behavior is more predictable.' |
| 132 | + - 'Test sparse-checkout configurations on a local clone before deploying to CI: `git sparse-checkout init --cone && git sparse-checkout set src/`' |
| 133 | +docs: |
| 134 | + - url: 'https://github.com/actions/checkout/issues/2366' |
| 135 | + label: 'actions/checkout#2366 — Sparse checkout cone mode does not include root files by default (Jan 2026)' |
| 136 | + - url: 'https://github.com/actions/checkout#sparse-checkout' |
| 137 | + label: 'actions/checkout README — sparse-checkout and sparse-checkout-cone-mode inputs' |
| 138 | + - url: 'https://git-scm.com/docs/git-sparse-checkout#_cone_mode_handling' |
| 139 | + label: 'Git docs — sparse-checkout cone mode: root directory handling' |
0 commit comments