fix(electron): number a download whose name is taken, and place downloads of an early session (ENG-892)
Merged
KD
20h ago+61+61+01234567890123456789-6-6-0123456789
What
- A download whose name is already taken in the attempt's download directory is saved as
note (1).txt,note (2).txt, and so on, as a browser numbers it. Before, the second download replaced the first. - A download from a session the app made before any window used it (
session.fromPartition('persist:later')in the app's ready handler) is saved inside the attempt. Before, it went to the app's own downloads path, which can be the user's Downloads folder.
Why
The third PR of the stack for the review of #2237 (ENG-892), which added @e2e-dev/electron.
placeDownloadssaved each download as<directory>/<filename>. A second download of the same name overwrote the first, andelectron.waitForDownloadthen pointed two downloads at one file.placeDownloadsplaced the default session, the sessions of open windows, and sessions created later. A session created before it ran, with no window on it yet, was in none of these, so its downloads were not placed.
How it works
- Before the main process is told where to save,
placeDownloadslists the files already in the directory (packages/electron/src/app-process.ts:548). A relaunch in the same attempt keeps the files of the launch before it. - In the main process,
unique(app-process.ts:554) gives each download a name not in that list and not taken by an earlier download of this launch, compared without case, since two names that differ only in case are one file on macOS and Windows. Files the app writes into the directory itself are not counted. placeDownloadsalso places the session of each newwebContents(app-process.ts:584), so a session made earlier is placed when a window first uses it.docs/electron.mdx(Downloads) says both.
A download that starts before the app's first window loads still goes where the app saves it, because placeDownloads runs after the first window. The docs say so.
Issue
Part of ENG-892. Stack: #2419 → #2420 → this PR → #2422 → #2423.
Verified
Ran it locally: yes
pnpm --filter @e2e-dev/electron testandtest:integration, and the electron testbed, on this layer:
@e2e-dev/electron unit + integration: Tests 148 passed (148)
apps/electron-testbed: Tests 75 passed (75)
- The new tests of this PR on the code of the PR below it (the bug) and on this PR (the fix):
| Test | PR below | this PR |
|---|---|---|
launch.test.ts: saves inside the attempt a download from a session the app made before any window used it | failed | passed |
testbed controls.e2e.ts: saves a download in the attempt's own directory, never the user's (second note (1).txt) | failed | passed |
- The integration test also checks that nothing was written to the app's own downloads path.
- The testbed's download test also covers the numbering across a relaunch and without case: after
note.txtandnote (1).txt,NOTE.txtis saved asNOTE (2).txt, and afterapp.restart()the nextnote.txtisnote (3).txt. With the list of the files a launch found forced empty, the test fails at the relaunch step. - Behavior and timing against
main: see thepnpm bench:abtable in #2423. - No changeset:
@e2e-dev/electronis not released yet (0.0.0, not on npm).
🤖 Generated with Claude Code
Summary by CodeRabbit
- New Features
- Repeated downloads with the same filename are saved with numbered names, even when capitalization differs. Files written by the app itself don’t affect numbering.
- Downloads started before the first app window loads are saved to the app’s normal download destination.
- The Electron testbed now includes a “Download shouting note” link for
NOTE.txt.
- Documentation
- Clarified how filename conflicts are handled and where early downloads are saved.