Skip to content

fix(create-electron-app): make import safe on ESM projects and existing Forge configs - #4467

Merged
erickzhao merged 6 commits into
mainfrom
claude/project-thread-oz9ycq
Nov 4, 2026
Merged

erickzhao merged 6 commits into
mainfrom
claude/project-thread-oz9ycq

Conversation

@erickzhao

@erickzhao erickzhao commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Requested by Erick · project thread

  • I have read the contribution documentation for this project.
  • I agree to follow the code of conduct that this project follows, as appropriate.
  • The changes are appropriately documented (if applicable).
  • The changes have sufficient test coverage (if applicable).
  • The testsuite passes successfully on my local machine (if applicable).

Summarize your changes:

create-electron-app import is still essentially the Forge 6 script and breaks on common project shapes. This is the first, correctness-only slice of a larger modernization; it changes no defaults for projects that worked before.

Bugs fixed

  • ESM projects got an unloadable config. A project with "type": "module" received the base template's CommonJS forge.config.js, and since forge-config.ts loads .js with native import(), every Forge command then failed with require is not defined in ES module scope. The template config is now rewritten as an ES module (import … from / export default) for those projects. The test loads the generated file through Node to prove it parses as ESM.
  • Existing forge.config.* files were overwritten. Only config.forge.makers in package.json counted as "existing config", so re-running import on a Forge project replaced forge.config.js, and a .ts/.mjs config got shadowed by a new .js one. All six extensions Forge loads are now detected and the existing config is kept. A string config.forge counts only when the file it points to exists, mirroring core's forgeConfigIsValidFilePath.
  • The config.forge merge discarded its result. It import()ed the template, lodash.merged the user's config into the module namespace object, then re-read and re-wrote the unchanged package.json. The existing config is now simply kept untouched (and the lodash-es dependency goes away).
  • Package manager resolved from the wrong directory, and npx always won. resolvePackageManager() searched for a lockfile from process.cwd(), and the npm_config_user_agent set by npx took precedence over any lockfile, so npx create-electron-app import installed into a pnpm or Yarn project with npm. resolvePackageManager gains { cwd, preferLockfile } options; import passes the project directory and prefers its lockfile, and the <pm> --version probe runs in that directory so a Corepack shim sees the project's packageManager field. Import also gets a --package-manager <name> flag like init.
  • .yarnrc.yml with nodeLinker: node-modules was written for Yarn Classic too; it is now only written for Yarn Berry. pnpm projects are left alone, as before.
  • The release script is added when the project has none, matching the base template. An existing release script is kept.

Docs

docs/cli.md and docs/import-existing-project.md still said npx electron-forge import, which was removed from the CLI in #4148. They now use npx create-electron-app@latest import and document the two flags.

Refactor

isYarnBerry moves from template-base to core-utils so import can share it (and template-base drops its direct semver dependency). lodash-es and @types/lodash-es are removed from the lockfile since nothing uses them anymore.

Tests

New fast spec packages/external/create-electron-app/spec/fast/import.spec.ts (install and package manager spawns mocked, real temp dirs) covering the CJS and ESM config output, each existing-config location, config.forge preservation, lockfile-in-dir detection and precedence over the user agent, the kept release script, and the Yarn Classic/Berry .yarnrc.yml behaviour. resolvePackageManager's new options get cases in the core-utils spec.

Verified locally: tsc -b packages, yarn test:fast (731 passed), lint:js, lint:markdown, lint:markdown-links, knip.

Not in this PR (planned follow-ups): wiring the confirmation prompts, which have never been connected in either Forge 7 or 8; pnpm setup for imported projects (allowBuilds/onlyBuiltDependencies, version-gated); a --dry-run; and an electron-builder config migrator.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG

…ng Forge configs

`create-electron-app import` was still the Forge 6 script and broke on
common 2026 project shapes:

- A project with `"type": "module"` got the base template's CommonJS
  `forge.config.js`, which Node then refuses to load (`require is not
  defined in ES module scope`). The template config is now rewritten as
  an ES module for those projects.
- An existing `forge.config.{js,mjs,cjs,ts,mts,cts}` file was not
  detected (only `config.forge.makers` in package.json was), so import
  overwrote it. All config locations Forge loads are now detected and
  kept as-is.
- When `config.forge` was an object in package.json, import merged the
  template config into the imported module namespace and then wrote the
  unchanged package.json back, discarding the result. The existing
  config is now kept untouched instead.
- The package manager was resolved from `process.cwd()`, not from the
  project directory, so `import ../other-app` picked the wrong one.
  `resolvePackageManager` gains a `cwd` option, import passes the
  project directory, and a `--package-manager` flag matches `init`.
- `.yarnrc.yml` was written for Yarn Classic too; it is now only written
  for Yarn Berry, and pnpm projects get the template's
  `pnpm-workspace.yaml` so Electron's build script is allowed.
- The `release` script was not added, unlike the base template.

The docs still pointed at `npx electron-forge import`, which was removed
from the CLI in #4148; they now use `npx create-electron-app@latest
import`.

`isYarnBerry` moves from template-base to core-utils so import can share
it, and `lodash-es` is no longer needed by create-electron-app.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG
@erickzhao
erickzhao requested a review from a team as a code owner November 3, 2026 05:22
@erickzhao erickzhao self-assigned this Nov 3, 2026
…project directory

The Windows fast-tests job failed because the new import spec resolved a
pnpm lockfile in a temp project and `resolvePackageManager` then ran
`pnpm --version` in the current directory, where the Corepack shim
refused with "This project is configured to use yarn".

Run the version probe with the `cwd` option so a Corepack shim sees the
imported project's `packageManager` field, and mock `spawn` in the
import spec so the test never runs a package manager for real.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings marked 🟡 are optional suggestions and need no follow-up push.

- Keep an existing `release` script; only add Forge's when none is set.
- Treat a string `config.forge` as an existing config only when the file
  it points to exists, mirroring core's `forgeConfigIsValidFilePath`.
- Prefer the lockfile in the project directory over the user agent of
  the package manager running the command (`npx` always reports npm),
  via a new `preferLockfile` option on `resolvePackageManager`.
- Require a value for `--package-manager` so a bare flag is rejected.
- Drop the `pnpm-workspace.yaml` copy: it would nest a workspace file
  inside a pnpm monorepo and its `allowBuilds` key is not understood by
  older pnpm 10 releases. pnpm setup for imports is deferred.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 optional suggestions (nits or notes on pre-existing code) were found and not posted.

…gent

With `preferLockfile`, `findUp` could return a stray lockfile from any
ancestor directory (e.g. `~/yarn.lock`) and switch the install to a
package manager the project never used. Only count a lockfile that sits
in `cwd` or beside a `package.json` in an ancestor, such as a workspace
root.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG
On Windows `path.resolve('/some/project')` gains a drive letter and
backslashes, so a mocked POSIX lockfile path never matched `cwd`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

@georgexu99 georgexu99 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stamped- the import path is in much better shape. A few nits inline, none blocking.

🤖 - a message from George's lil' guy

- Match core's config precedence in `findExistingForgeConfig`: object,
  then `forge.config.*` files, then a `config.forge` path string.
- Throw if CommonJS syntax survives the ESM conversion of the template
  config, so template drift fails loudly.
- Probe the project's Yarn version when `--package-manager yarn` is
  given without a version, so Yarn Classic projects do not get a
  `.yarnrc.yml`.
- Explain the failure when a lockfile's package manager cannot be run,
  instead of a bare spawn ENOENT.
- Restore an unset `npm_config_user_agent` properly in the import spec.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

@georgexu99 georgexu99 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-stamped on f69c7db- all five addressed, the loud throw on template drift is exactly what i wanted.

🤖 - a message from George's lil' guy

@erickzhao
erickzhao merged commit 0369cc2 into main Nov 4, 2026
24 checks passed
@erickzhao
erickzhao deleted the claude/project-thread-oz9ycq branch November 4, 2026 03:15
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.

3 participants