Skip to content

fix(cli): tell a missing import file from an unreadable one - #815

Merged
Ishaan Gangwani (ishaan1124) merged 2 commits into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/import-malformed-json
Sep 29, 2026
Merged

Ishaan Gangwani (ishaan1124) merged 2 commits into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/import-malformed-json

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

What

Eleven lines of src/cli/cmd/import.ts had three separate problems:

const file = Bun.file(args.file)
exportData = await file.json().catch(() => {})
if (!exportData) {
  process.stdout.write(`File not found: ${args.file}`)
  process.stdout.write(EOL)
  return
}

if (!exportData) {                    // unreachable — the block above returned
  process.stdout.write(`Failed to read session data`)
  process.stdout.write(EOL)
  return
}
  1. .catch(() => {}) collapses every failure, so a file that exists but is not valid JSON is reported as File not found.
  2. The second if (!exportData) block is dead code — Failed to read session data can never be printed.
  3. Neither path sets a non-zero exit code, so a failed import exits 0.

Why it matters

File not found: /path/you/just/typed sends the user hunting for a path that is right there. The real cause — a truncated export, a merge-conflict marker, a JSONC file — is invisible.

The exit code matters more. import is the natural way to move a session between machines and to script a restore, so openscience import backup.json && echo done prints done after a completely failed import.

There's also a quieter third failure: valid JSON that is not a session export. {"hello": "world"} parses fine, so !exportData is false, and the next line is exportData.info.id — a TypeError on undefined.

Verification

The read logic is extracted to an exported readExport helper so it can be driven from a test without bootstrapping a project; the new test covers five inputs.

Confirmed against the original code on this machine:

$ echo '{ this is not json' > bad.json
$ openscience import bad.json
File not found: C:\...\bad.json        <- the file exists
$ echo $?
0                                      <- reported as success

With the fix:

(pass) reads a well-formed export
(pass) a malformed file is reported as unreadable, not as missing
(pass) a missing file still says so
(pass) valid JSON that is not a session export is rejected with a reason
(pass) an empty file is rejected rather than imported as nothing
 5 pass
 0 fail

The malformed case asserts not.toContain("File not found") as well as the new message, so the two failure modes cannot collapse back into one.

The change

/** Read an exported session file, saying which of the two failures it was.
 * A single `json().catch(() => {})` reported a malformed file as missing, and
 * the second copy of the same check below was unreachable. */
export async function readExport(
  file: string,
): Promise<{ ok: true; data: SessionExport } | { ok: false; message: string }> {
  if (!(await Bun.file(file).exists())) return { ok: false, message: `File not found: ${file}` }
  let data: unknown
  try {
    data = await Bun.file(file).json()
  } catch (error) {
    return { ok: false, message: `Failed to read session data from ${file}: ${error instanceof Error ? error.message : String(error)}` }
  }
  if (typeof data !== "object" || data === null || !("info" in data) || !("messages" in data)) {
    return { ok: false, message: `Failed to read session data from ${file}: expected an object with info and messages` }
  }
  return { ok: true, data: data as SessionExport }
}

and in the handler:

-      const file = Bun.file(args.file)
-      exportData = await file.json().catch(() => {})
-      if (!exportData) { process.stdout.write(`File not found: ${args.file}`); ... return }
-      if (!exportData) { process.stdout.write(`Failed to read session data`); ... return }
+      const read = await readExport(args.file)
+      if (!read.ok) {
+        // A failure here is the whole result of the command, so it must not
+        // exit 0 and read as a success in a script.
+        process.stderr.write(read.message)
+        process.stderr.write(EOL)
+        process.exitCode = 1
+        return
+      }
+      const exportData = read.data

The success path and the write loop are unchanged. Failures go to stderr where a script can capture them, and the dead duplicate branch is gone.

bun run typecheck clean; touched files are Prettier-clean (checked on LF-normalized copies — this checkout has core.autocrlf=true, which makes Prettier flag every file in the repo).

Fixes #814

@vercel

vercel Bot commented Sep 28, 2026

Copy link
Copy Markdown

ANIRUDDHA ADAK (@aniruddhaadak80) is attempting to deploy a commit to the InkVell Team on Vercel.

A member of the Team first needs to authorize it.

The handler collapsed both failures into one message and left the
exit code at 0, so a script importing sessions could not tell a typo
from a corrupt export. Extract the read into a helper that separates
the two, checks the shape, and fails the command.
@ishaan1124
Ishaan Gangwani (ishaan1124) merged commit f389bfa into synthetic-sciences:main Sep 29, 2026
8 of 9 checks passed
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.

openscience import reports a malformed session file as not found and exits 0

2 participants