fix(import-audit): preserve JSON reports when writing fails #4425

Merged
Lorkydey merged 1 commit from upstream-pr/4425 into main 2026-10-14 01:06:17 +00:00
Lorkydey commented 2026-10-12 10:48:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Lorkydey on 2026-10-12T10:48:42Z.


What

import_audit.py --json audit.json truncates the destination before writing the new report. A write error leaves an incomplete JSON file and destroys the previous audit. For a new destination it leaves a partial file that looks like an available report.

Write the complete report inside a temporary directory beside the resolved destination, then atomically replace the destination. New reports retain the normal 0666 permissions filtered by the process umask, without changing that process-wide setting. Preserve existing report permissions and symlink targets. Keep the caller-supplied output path in the directory-error diagnostic, including Windows short paths. Failed writes or replacements keep the old report, return the existing failure exit code and remove staging files, including Windows read-only temporaries. Input-overwrite protection and import classification stay unchanged.

Eight new test methods exercise the CLI entry point and real temporary files. POSIX permission cases run with isolated subprocess umasks. The disk-full injection writes an actual partial prefix before raising ENOSPC; with main dacd78525, an existing report is truncated and a new report is left behind. A replacement denial is also covered. This is controlled fault injection, not a claim of exhausting the host disk.

Tested

Rebased onto main dacd78525e537a779682032c0ace7f7af120af05, including #4545 and #4606, at exact head d45fee9b69c8f59e11818d3e25fbdfc0b6e296fd:

  • Windows 11, Python 3.12.4: python -m unittest discover -s tools/tests -p 'test_*.py' -q runs 287 tests, 285 pass and the two POSIX-only permission tests skip.
  • WinLibs GCC 15.2.0 r7, Ninja, Release: the relinker-only build succeeds and all 150 CTests pass. The focused import_audit CTest also passes.
  • Exact-source Windows/Linux/macOS validation passes with Python 3.12. Each runs all 287 tools tests, including JSON stops, module imports, atomic replacement, partial writes, symlinks, permissions and input-overwrite protection. Windows skips the two POSIX-only permission tests; Linux/macOS skip the Windows-only read-only test.
  • On all three hosted platforms and local Windows, the partial-write regression fails in both subcases against main's import_audit.py: the old report is truncated, or a partial new report is left behind. Restoring the exact PR source passes. This is controlled fault injection, not actual disk exhaustion.
  • The official Build run also passes all three relinker builds and test suites (Windows, Linux, macOS) for this PR head.
  • Conventions and git diff --check pass. No native runtime change or title run.

Overlap coordination

#4545 and #4606 are now merged. This rebase preserves main's stops construction and JSON field, then passes the complete result to write_json(args.json, result). It also retains the merged hard-link test changes, with one os import and the stat import required for permission tests. Their module-import, stops and hard-link regressions pass alongside the atomic-write tests. #4415 is closed without merging. No outstanding dependency on those PRs remains.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no runtime exports or silent stubs changed
  • No notes, investigation files or images added to the repository
  • New third-party code is a submodule built from source; none added
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes (Codex)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4828317207 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4425). Originally posted by [Lorkydey](https://github.com/Lorkydey) on 2026-10-12T10:48:42Z. --- ### What `import_audit.py --json audit.json` truncates the destination before writing the new report. A write error leaves an incomplete JSON file and destroys the previous audit. For a new destination it leaves a partial file that looks like an available report. Write the complete report inside a temporary directory beside the resolved destination, then atomically replace the destination. New reports retain the normal 0666 permissions filtered by the process umask, without changing that process-wide setting. Preserve existing report permissions and symlink targets. Keep the caller-supplied output path in the directory-error diagnostic, including Windows short paths. Failed writes or replacements keep the old report, return the existing failure exit code and remove staging files, including Windows read-only temporaries. Input-overwrite protection and import classification stay unchanged. Eight new test methods exercise the CLI entry point and real temporary files. POSIX permission cases run with isolated subprocess umasks. The disk-full injection writes an actual partial prefix before raising ENOSPC; with main `dacd78525`, an existing report is truncated and a new report is left behind. A replacement denial is also covered. This is controlled fault injection, not a claim of exhausting the host disk. ### Tested Rebased onto main `dacd78525e537a779682032c0ace7f7af120af05`, including #4545 and #4606, at exact head `d45fee9b69c8f59e11818d3e25fbdfc0b6e296fd`: - Windows 11, Python 3.12.4: `python -m unittest discover -s tools/tests -p 'test_*.py' -q` runs 287 tests, 285 pass and the two POSIX-only permission tests skip. - WinLibs GCC 15.2.0 r7, Ninja, Release: the relinker-only build succeeds and all 150 CTests pass. The focused `import_audit` CTest also passes. - [Exact-source Windows/Linux/macOS validation](https://github.com/Lorkydey/AnyPS5/actions/runs/38455106401) passes with Python 3.12. Each runs all 287 tools tests, including JSON `stops`, module imports, atomic replacement, partial writes, symlinks, permissions and input-overwrite protection. Windows skips the two POSIX-only permission tests; Linux/macOS skip the Windows-only read-only test. - On all three hosted platforms and local Windows, the partial-write regression fails in both subcases against main's `import_audit.py`: the old report is truncated, or a partial new report is left behind. Restoring the exact PR source passes. This is controlled fault injection, not actual disk exhaustion. - The official [Build run](https://github.com/boykopovar/AnyPS5/actions/runs/38457570551) also passes all three relinker builds and test suites (Windows, Linux, macOS) for this PR head. - Conventions and `git diff --check` pass. No native runtime change or title run. ### Overlap coordination #4545 and #4606 are now merged. This rebase preserves main's `stops` construction and JSON field, then passes the complete result to `write_json(args.json, result)`. It also retains the merged hard-link test changes, with one `os` import and the `stat` import required for permission tests. Their module-import, `stops` and hard-link regressions pass alongside the atomic-write tests. #4415 is closed without merging. No outstanding dependency on those PRs remains. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; no runtime exports or silent stubs changed - [x] No notes, investigation files or images added to the repository - [x] New third-party code is a submodule built from source; none added - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes (Codex)
MarcZX10 commented 2026-10-12 10:50:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T10:50:13Z.


One filesystem behavior question: when the destination does not exist, NamedTemporaryFile creates the staging file with mode 0600 on POSIX, and that mode is carried through the replace. The old Path.write_text created a new report with the usual 0666 filtered by the process umask; existing destinations are correctly handled separately here. Is the new-report permission tightening intentional? If reports are meant to remain group-readable under a typical umask, the new-destination case may need an explicit mode and regression check.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6122777824 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4425#issuecomment-6122777824). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T10:50:13Z. --- One filesystem behavior question: when the destination does not exist, `NamedTemporaryFile` creates the staging file with mode 0600 on POSIX, and that mode is carried through the replace. The old `Path.write_text` created a new report with the usual 0666 filtered by the process umask; existing destinations are correctly handled separately here. Is the new-report permission tightening intentional? If reports are meant to remain group-readable under a typical umask, the new-destination case may need an explicit mode and regression check.
MarcZX10 commented 2026-10-12 10:54:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T10:54:04Z.


The Windows relinker check has one reproducible failure: import_audit fails test_unreadable_library_and_unwritable_json_exit_two (1/124). The test passes str(work.modules) as --json and expects that path in the diagnostic. write_json() first calls path.resolve(), so on the hosted Windows runner the error reports C:\Users\runneradmin\...\modules while the supplied temp path is C:\Users\RUNNER~1\...\modules. Keeping the caller-supplied path for diagnostics (while using a separately resolved path for the atomic replace if needed) should preserve the existing CLI error contract and make this test pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6122835376 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4425#issuecomment-6122835376). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T10:54:04Z. --- The Windows relinker check has one reproducible failure: `import_audit` fails `test_unreadable_library_and_unwritable_json_exit_two` (1/124). The test passes `str(work.modules)` as `--json` and expects that path in the diagnostic. `write_json()` first calls `path.resolve()`, so on the hosted Windows runner the error reports `C:\Users\runneradmin\...\modules` while the supplied temp path is `C:\Users\RUNNER~1\...\modules`. Keeping the caller-supplied path for diagnostics (while using a separately resolved path for the atomic replace if needed) should preserve the existing CLI error contract and make this test pass.
Lorkydey commented 2026-10-12 12:49:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Lorkydey on 2026-10-12T12:49:40Z.


@MarcZX100 Thx for catching both issues! Fixed the diagnostic path and the permissions for new reports, with regression tests added. The relinker CI now passes on Windows, Linux and macOS :)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6124595937 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4425#issuecomment-6124595937). Originally posted by [Lorkydey](https://github.com/Lorkydey) on 2026-10-12T12:49:40Z. --- @MarcZX100 Thx for catching both issues! Fixed the diagnostic path and the permissions for new reports, with regression tests added. The relinker CI now passes on Windows, Linux and macOS :)
Lorkydey commented 2026-10-12 22:04:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Lorkydey on 2026-10-12T22:04:34Z.


@MrMha @thuanquang @Nixitov

I checked the overlap using #4425 at 05d52e396, #4415 at 79b039269, #4545 at 4feeef698 and #4606 at 7eae85946, combined on main c952c2b0e. Those PR heads are still unchanged. The changes cover different behavior, but there are textual merge conflicts:

  • #4415 / #4545: keep all required test imports (os, stat, shutil, etc.) and #4545's os.link change.
  • #4606: keep its stops data in the JSON result, then pass that result to #4425's write_json(args.json, result). When combined with #4415, retain its module-aware reference count and both sets of tests as well.

With those resolutions, the combined Windows checkout ran 99 focused Python tests: 96 passed and 3 platform-specific cases were skipped. This was a local integration check on the pinned base above, not a full suite or Linux/macOS run. No dependency commits have been added to #4425.

@Nixitov has offered to rebase #4606, and #4545's description also offers a rebase if #4425 lands first. I propose #4425 first, followed by rebases preserving the changes above. Would that order work for everyone? If a different order is preferred, I can rebase this PR instead.

AI-assisted: yes (Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6133458548 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4425#issuecomment-6133458548). Originally posted by [Lorkydey](https://github.com/Lorkydey) on 2026-10-12T22:04:34Z. --- @MrMha @thuanquang @Nixitov I checked the overlap using #4425 at `05d52e396`, #4415 at `79b039269`, #4545 at `4feeef698` and #4606 at `7eae85946`, combined on main `c952c2b0e`. Those PR heads are still unchanged. The changes cover different behavior, but there are textual merge conflicts: - #4415 / #4545: keep all required test imports (`os`, `stat`, `shutil`, etc.) and #4545's `os.link` change. - #4606: keep its `stops` data in the JSON result, then pass that result to #4425's `write_json(args.json, result)`. When combined with #4415, retain its module-aware reference count and both sets of tests as well. With those resolutions, the combined Windows checkout ran 99 focused Python tests: 96 passed and 3 platform-specific cases were skipped. This was a local integration check on the pinned base above, not a full suite or Linux/macOS run. No dependency commits have been added to #4425. @Nixitov has [offered to rebase #4606](https://github.com/boykopovar/AnyPS5/pull/4606#issuecomment-6130604805), and #4545's description also offers a rebase if #4425 lands first. I propose #4425 first, followed by rebases preserving the changes above. Would that order work for everyone? If a different order is preferred, I can rebase this PR instead. AI-assisted: yes (Codex).
Nixitov commented 2026-10-12 22:18:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nixitov on 2026-10-12T22:18:38Z.


Thanks @Lorkydey — that order works for me.

#4425 first, then I'll rebase #4606 after it lands: keep the stops data in the JSON result and pass it to your write_json(args.json, result), and if #4415 lands before that I'll retain its module-aware reference count and both sets of tests as well.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6133651222 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4425#issuecomment-6133651222). Originally posted by [Nixitov](https://github.com/Nixitov) on 2026-10-12T22:18:38Z. --- Thanks @Lorkydey — that order works for me. **#4425 first**, then I'll rebase **#4606** after it lands: keep the `stops` data in the JSON result and pass it to your `write_json(args.json, result)`, and if **#4415** lands before that I'll retain its module-aware reference count and both sets of tests as well. AI-assisted: yes
thuanquang commented 2026-10-13 06:51:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-13T06:51:04Z.


Thanks @Lorkydey, that order works for me: #4425 first. Once it lands I'll rebase #4545 onto main, keeping a single import os and the two os.link changes, and rerun import_audit and stubgen. If #4415 lands before that, I'll keep its imports too.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140010168 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4425#issuecomment-6140010168). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-13T06:51:04Z. --- Thanks @Lorkydey, that order works for me: #4425 first. Once it lands I'll rebase #4545 onto `main`, keeping a single `import os` and the two `os.link` changes, and rerun `import_audit` and `stubgen`. If #4415 lands before that, I'll keep its imports too. AI-assisted: yes (Claude Code)
MrMha commented 2026-10-13 09:23:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MrMha on 2026-10-13T09:23:38Z.


Confirm their proposed merge order works, and explain that your #4415 was closed, so they can proceed without waiting for it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6142448125 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4425#issuecomment-6142448125). Originally posted by [MrMha](https://github.com/MrMha) on 2026-10-13T09:23:38Z. --- Confirm their proposed merge order works, and explain that your #4415 was closed, so they can proceed without waiting for it.
thuanquang commented 2026-10-13 17:49:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-13T17:49:25Z.


#4545 was merged first, so the overlap is now only the import os line that main already has. Dropping yours from the import block should resolve the conflict. Sorry for the reversed order, and thanks for coordinating.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6151687202 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4425#issuecomment-6151687202). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-13T17:49:25Z. --- #4545 was merged first, so the overlap is now only the `import os` line that `main` already has. Dropping yours from the import block should resolve the conflict. Sorry for the reversed order, and thanks for coordinating. AI-assisted: yes (Claude Code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4425
No description provided.