Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(cli): preserve Windows files when replacement fails (#43974)

huyengiang101086-spec667e2f8e2ffe
diff --git a/cli/src/sys/index.test.ts b/cli/src/sys/index.test.ts
index 5622a8dd87d..95208d63fe7 100644
--- a/cli/src/sys/index.test.ts
+++ b/cli/src/sys/index.test.ts
@@ -1,6 +1,7 @@
-import { homedir } from 'node:os'
+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { homedir, tmpdir } from 'node:os'
import { join } from 'node:path'
-import { describe, expect, it } from 'vite-plus/test'
+import { afterEach, beforeEach, describe, expect, it } from 'vite-plus/test'
import { resolvePlatform, SUBDIR } from './index'
describe('resolvePlatform', () => {
@@ -32,3 +33,47 @@ describe('resolvePlatform', () => {
expect(resolvePlatform().atomicReplace).toBeTypeOf('function')
})
})
+
+describe('atomicReplace', () => {
+ let dir: string
+
+ beforeEach(() => {
+ dir = mkdtempSync(join(tmpdir(), 'difyctl-replace-'))
+ })
+
+ afterEach(() => {
+ rmSync(dir, { recursive: true, force: true })
+ })
+
+ it('replaces an existing file with the new contents', () => {
+ const source = join(dir, 'settings.tmp')
+ const target = join(dir, 'settings.yaml')
+ writeFileSync(source, 'new settings')
+ writeFileSync(target, 'old settings')
+
+ resolvePlatform().atomicReplace(source, target)
+
+ expect(readFileSync(target, 'utf8')).toBe('new settings')
+ expect(existsSync(source)).toBe(false)
+ })
+
+ it('creates the destination when no previous file exists', () => {
+ const source = join(dir, 'settings.tmp')
+ const target = join(dir, 'settings.yaml')
+ writeFileSync(source, 'new settings')
+
+ resolvePlatform().atomicReplace(source, target)
+
+ expect(readFileSync(target, 'utf8')).toBe('new settings')
+ expect(existsSync(source)).toBe(false)
+ })
+
+ it('preserves the existing file when the replacement source is missing', () => {
+ const source = join(dir, 'missing.tmp')
+ const target = join(dir, 'settings.yaml')
+ writeFileSync(target, 'old settings')
+
+ expect(() => resolvePlatform().atomicReplace(source, target)).toThrow()
+ expect(readFileSync(target, 'utf8')).toBe('old settings')
+ })
+})
diff --git a/cli/src/sys/index.ts b/cli/src/sys/index.ts
index a49e252f0a7..513a86fd4bf 100644
--- a/cli/src/sys/index.ts
+++ b/cli/src/sys/index.ts
@@ -70,9 +70,6 @@ function posixAtomicReplace(src: string, dst: string): void {
}
function win32AtomicReplace(src: string, dst: string): void {
- try {
- fs.unlinkSync(dst)
- } catch {}
fs.renameSync(src, dst)
}