Skip to content

fix: set CACHE_DIR fallback when no package.json is reachable (#203)#208

Merged
JohannesHoppe merged 2 commits intomainfrom
fix/issue-203-npx-path-undefined
Apr 22, 2026
Merged

fix: set CACHE_DIR fallback when no package.json is reachable (#203)#208
JohannesHoppe merged 2 commits intomainfrom
fix/issue-203-npx-path-undefined

Conversation

@JohannesHoppe
Copy link
Copy Markdown
Member

@JohannesHoppe JohannesHoppe commented Apr 22, 2026

Summary

Fixes #203npx angular-cli-ghpages --dir=dist --branch=gh-pages in a directory with no package.json (e.g. a repo that is just a static dist/ folder) fails with:

❌ An error occurred when trying to deploy:
The "path" argument must be of type string or an instance of Buffer or URL. Received undefined

Root cause

gh-pages@6.3.0 calls find-cache-dir({ name: 'gh-pages' }) in both clean() and publish(). find-cache-dir walks up from cwd looking for package.json — if it finds none, it returns undefined. gh-pages then does path.join(undefined, filenamify(repo))TypeError.

The reporter's workaround npm install angular-cli-ghpages works because it creates a package.json as a side effect, which find-cache-dir can then find.

Fix

find-cache-dir honors the CACHE_DIR env var as an explicit override — when set (and not a boolean-ish 'true'/'false'/'1'/'0'), it short-circuits the package.json walk and returns path.join(CACHE_DIR, name) directly.

New helper ensureGhPagesCacheDir() in engine.prepare-options-helpers.ts:

  1. Respects a user-set CACHE_DIR (no-op).
  2. Probes for a reachable package.json via our own synchronous walker.
  3. If none is found, sets process.env.CACHE_DIR = <os.tmpdir()>/angular-cli-ghpages-cache.

Called from engine.run() before require('gh-pages').

We probe for package.json ourselves instead of delegating to find-cache-dir because find-cache-dir captures const {env} = process at module load and is then cached in require.cache — using it as our probe makes behavior brittle under test process.env reassignment.

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
We don't ship a new npm version per fix; all pending fixes (#204, #205,
#203, and follow-ups) will be released in a single bump at release
time. This also rolls back the earlier per-PR bump that landed on main.
@JohannesHoppe JohannesHoppe merged commit 1694071 into main Apr 22, 2026
@JohannesHoppe JohannesHoppe deleted the fix/issue-203-npx-path-undefined branch April 22, 2026 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v3.0.2: "path argument undefined" error when running standalone with npx

1 participant