Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions lib/ensure/__tests__/symlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,53 @@ describe('fse-ensure-symlink', () => {
if (newBehavior === 'dir-error') dirErrorSync(args, fn)
})
})

// https://github.com/jprichardson/node-fs-extra/issues/1038
describe('ensureSymlink() with relative path called twice (issue #1038)', () => {
it('should succeed when calling ensureSymlink twice with a relative path', async () => {
const targetDir = path.join(TEST_DIR, 'target-1038')
const linkDir = path.join(TEST_DIR, 'link-1038')
const linkPath = path.join(linkDir, 'link')
const relativeTarget = path.relative(linkDir, targetDir)

// Create target directory with a file
await fse.ensureDir(targetDir)
fs.writeFileSync(path.join(targetDir, 'file.txt'), 'content')

// First ensureSymlink call with relative path - should succeed
await ensureSymlink(relativeTarget, linkPath, 'dir')
assert.strictEqual(fs.lstatSync(linkPath).isSymbolicLink(), true)

// Second ensureSymlink call with same relative path - should also succeed
await ensureSymlink(relativeTarget, linkPath, 'dir')
assert.strictEqual(fs.lstatSync(linkPath).isSymbolicLink(), true)

// Verify the symlink still works
const content = fs.readFileSync(path.join(linkPath, 'file.txt'), 'utf8')
assert.strictEqual(content, 'content')
})

it('should succeed when calling ensureSymlinkSync twice with a relative path', () => {
const targetDir = path.join(TEST_DIR, 'target-1038-sync')
const linkDir = path.join(TEST_DIR, 'link-1038-sync')
const linkPath = path.join(linkDir, 'link')
const relativeTarget = path.relative(linkDir, targetDir)

// Create target directory with a file
fse.ensureDirSync(targetDir)
fs.writeFileSync(path.join(targetDir, 'file.txt'), 'content')

// First ensureSymlinkSync call with relative path - should succeed
ensureSymlinkSync(relativeTarget, linkPath, 'dir')
assert.strictEqual(fs.lstatSync(linkPath).isSymbolicLink(), true)

// Second ensureSymlinkSync call with same relative path - should also succeed
ensureSymlinkSync(relativeTarget, linkPath, 'dir')
assert.strictEqual(fs.lstatSync(linkPath).isSymbolicLink(), true)

// Verify the symlink still works
const content = fs.readFileSync(path.join(linkPath, 'file.txt'), 'utf8')
assert.strictEqual(content, 'content')
})
})
})
35 changes: 30 additions & 5 deletions lib/ensure/symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ async function createSymlink (srcpath, dstpath, type) {
} catch { }

if (stats && stats.isSymbolicLink()) {
const [srcStat, dstStat] = await Promise.all([
fs.stat(srcpath),
fs.stat(dstpath)
])
// When srcpath is relative, resolve it relative to dstpath's directory
// (standard symlink behavior) or fall back to cwd if that doesn't exist
let srcStat
if (path.isAbsolute(srcpath)) {
srcStat = await fs.stat(srcpath)
} else {
const dstdir = path.dirname(dstpath)
const relativeToDst = path.join(dstdir, srcpath)
try {
srcStat = await fs.stat(relativeToDst)
} catch {
srcStat = await fs.stat(srcpath)
}
}

const dstStat = await fs.stat(dstpath)
if (areIdentical(srcStat, dstStat)) return
}

Expand All @@ -46,7 +57,21 @@ function createSymlinkSync (srcpath, dstpath, type) {
stats = fs.lstatSync(dstpath)
} catch { }
if (stats && stats.isSymbolicLink()) {
const srcStat = fs.statSync(srcpath)
// When srcpath is relative, resolve it relative to dstpath's directory
// (standard symlink behavior) or fall back to cwd if that doesn't exist
let srcStat
if (path.isAbsolute(srcpath)) {
srcStat = fs.statSync(srcpath)
} else {
const dstdir = path.dirname(dstpath)
const relativeToDst = path.join(dstdir, srcpath)
try {
srcStat = fs.statSync(relativeToDst)
} catch {
srcStat = fs.statSync(srcpath)
}
}

const dstStat = fs.statSync(dstpath)
if (areIdentical(srcStat, dstStat)) return
}
Expand Down