fix: set CACHE_DIR fallback when no package.json is reachable (#203)#208
Merged
JohannesHoppe merged 2 commits intomainfrom Apr 22, 2026
Merged
fix: set CACHE_DIR fallback when no package.json is reachable (#203)#208JohannesHoppe merged 2 commits intomainfrom
JohannesHoppe merged 2 commits intomainfrom
Conversation
gh-pages@6.3.0 calls find-cache-dir({ name: 'gh-pages' }) for both
clean() and publish(). find-cache-dir walks up from cwd looking for a
package.json and returns undefined when it finds none. gh-pages then
does path.join(undefined, ...) and throws:
The "path" argument must be of type string or an instance of Buffer
or URL. Received undefined
This hit users running `npx angular-cli-ghpages` from a repo that is
just a dist/ folder (no package.json). The reporter's workaround of
`npm install angular-cli-ghpages` first worked because it created a
package.json that find-cache-dir could find.
Fix: add ensureGhPagesCacheDir() to engine.prepare-options-helpers and
call it from engine.run() before gh-pages is required. If no
package.json is reachable from cwd, we set process.env.CACHE_DIR to
`<os.tmpdir()>/angular-cli-ghpages-cache`. find-cache-dir honors
CACHE_DIR as an explicit override — when set (and not a boolean-ish
value 'true'/'false'/'1'/'0') it short-circuits its package.json walk.
We do the package.json probe ourselves rather than delegating to
find-cache-dir: find-cache-dir captures `const {env} = process` at
module load and is then cached in require.cache, which makes it
brittle under test reassignment of process.env.
Tests:
- engine.prepare-options-helpers.spec.ts — unit tests covering: cwd
with no package.json sets fallback; cwd with package.json is a
no-op; pre-existing user-set CACHE_DIR is preserved; boolean-ish
CACHE_DIR values ('true'/'false'/etc.) are treated as unset per
find-cache-dir's own semantics; fallback path is deterministically
inside os.tmpdir().
- engine.gh-pages-no-package-json.spec.ts — real-git, real-gh-pages
end-to-end. Creates a bare repo + workdir with no package.json in
any ancestor, process.chdir()s there, runs engine.run(), inspects
the remote tree, asserts the dist actually lands. A companion
baseline test spawns a child Node process (no shared require.cache)
and calls bare gh-pages.clean() in the same workdir, asserting it
throws the original TypeError — proving the upstream bug is real.
Fixes #203
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #203 —
npx angular-cli-ghpages --dir=dist --branch=gh-pagesin a directory with nopackage.json(e.g. a repo that is just a staticdist/folder) fails with:Root cause
gh-pages@6.3.0 calls
find-cache-dir({ name: 'gh-pages' })in bothclean()andpublish().find-cache-dirwalks up from cwd looking forpackage.json— if it finds none, it returnsundefined. gh-pages then doespath.join(undefined, filenamify(repo))→TypeError.The reporter's workaround
npm install angular-cli-ghpagesworks because it creates apackage.jsonas a side effect, whichfind-cache-dircan then find.Fix
find-cache-dirhonors theCACHE_DIRenv var as an explicit override — when set (and not a boolean-ish'true'/'false'/'1'/'0'), it short-circuits thepackage.jsonwalk and returnspath.join(CACHE_DIR, name)directly.New helper
ensureGhPagesCacheDir()inengine.prepare-options-helpers.ts:CACHE_DIR(no-op).package.jsonvia our own synchronous walker.process.env.CACHE_DIR = <os.tmpdir()>/angular-cli-ghpages-cache.Called from
engine.run()beforerequire('gh-pages').We probe for
package.jsonourselves instead of delegating tofind-cache-dirbecausefind-cache-dircapturesconst {env} = processat module load and is then cached inrequire.cache— using it as our probe makes behavior brittle under testprocess.envreassignment.