Skip to content

fix(utils): replace runtime require with static import for initStorage#1600

Merged
piorpua merged 1 commit intomainfrom
fix/utils-lazy-require-bundling
Mar 20, 2026
Merged

fix(utils): replace runtime require with static import for initStorage#1600
piorpua merged 1 commit intomainfrom
fix/utils-lazy-require-bundling

Conversation

@kaizhou-lab
Copy link
Copy Markdown
Collaborator

Summary

Changes

  • src/process/utils/utils.ts: Remove lazyGetSystemDir() wrapper using require('./initStorage'), replace with static import { getSystemDir } from './initStorage'

Root Cause

PR #1583 changed a static import to a lazy require() to break circular dependency. In bundled output, require('./initStorage') resolves relative to out/main/chunks/, where no such module exists. The bug only triggers when files is [] (empty array, not undefined), which is why the first message from guid page works but subsequent messages fail.

Related Issue

Closes #1599

Test Plan

  • Create an ACP conversation from guid page, send first message → should work
  • Send a second message in the same conversation → should work (previously crashed)
  • Create a second ACP conversation → should work
  • Send message with file attachments → should work

The refactoring in PR #1583 changed `import { getSystemDir } from
'./initStorage'` to a lazy `require('./initStorage')` to break a
circular dependency. However, Vite/Rollup does not transform dynamic
`require()` relative paths in bundled output, causing
`copyFilesToDirectory` to throw "Cannot find module './initStorage'"
at runtime when `files` is a non-empty or empty array (not undefined).

This broke the second message in ACP conversations because the first
message passes `files: undefined` (skipping the require), while
subsequent messages pass `files: []` (empty array), which reaches the
broken require call.

Replace `require('./initStorage')` with a static import. The circular
dependency between utils.ts and initStorage.ts is safe because
`getSystemDir` is only called at runtime inside function bodies, not
during module initialization. This matches the pre-refactor behavior
that worked correctly.
@piorpua piorpua merged commit 2da3ee9 into main Mar 20, 2026
13 of 17 checks passed
@piorpua piorpua deleted the fix/utils-lazy-require-bundling branch March 20, 2026 09:52
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.

fix(utils): runtime require('./initStorage') fails in bundled output

2 participants