Skip to content

Commit 25f2246

Browse files
committed
Probe target existence before rename
Use fs.access as an explicit existence probe and distinguish an absent target from real I/O failures. The change introduces a targetExists flag and a try/catch around fs.access so that expected "not found" control flow proceeds to fs.rename, while genuine errors from fs.unlink (e.g. permission issues or locks) will propagate instead of being silently swallowed. A clarifying comment was also added to explain the reasoning.
1 parent 00420c8 commit 25f2246

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/plugins/llms-html-injector/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,26 @@ async function normalizeMarkdownLayout(outDir) {
9191
if (!relTo || relTo === relFrom) continue
9292

9393
const absTo = path.join(outDir, relTo)
94+
95+
// `fs.access` is used purely as an existence probe: a throw means "target
96+
// is absent, proceed to rename", which is expected control flow. Errors
97+
// from the subsequent `fs.unlink` are real I/O failures (file locked,
98+
// permission denied, etc.) and must propagate rather than silently fall
99+
// through to `fs.rename`, which would overwrite the existing target and
100+
// contradict the "target exists -> drop the duplicate source" intent.
101+
let targetExists = true
94102
try {
95103
await fs.access(absTo)
104+
} catch {
105+
targetExists = false
106+
}
107+
108+
if (targetExists) {
96109
await fs.unlink(absFrom)
97110
renames.set(relFrom, relTo)
98111
dirsToPrune.add(path.dirname(absFrom))
99112
continue
100-
} catch {}
113+
}
101114

102115
await fs.mkdir(path.dirname(absTo), { recursive: true })
103116
await fs.rename(absFrom, absTo)

0 commit comments

Comments
 (0)