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
18 changes: 18 additions & 0 deletions src/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const {
getLockFilePath,
_installLocalPlugin,
_isLocalPluginSource,
_moveDir,
_resolveStoredPluginSource,
_toLocalPluginSource,
} = pluginModule;
Expand Down Expand Up @@ -735,3 +736,20 @@ describe('plugin source helpers', () => {
expect(source).toBe('local:/tmp/plugin');
});
});

describe('moveDir', () => {
it('cleans up destination when EXDEV fallback copy fails', () => {
const src = path.join(os.tmpdir(), 'opencli-move-src');
const dest = path.join(os.tmpdir(), 'opencli-move-dest');
const renameErr = Object.assign(new Error('cross-device link not permitted'), { code: 'EXDEV' });
const copyErr = new Error('copy failed');
const renameSync = vi.fn(() => { throw renameErr; });
const cpSync = vi.fn(() => { throw copyErr; });
const rmSync = vi.fn(() => undefined);

expect(() => _moveDir(src, dest, { renameSync, cpSync, rmSync })).toThrow(copyErr);
expect(renameSync).toHaveBeenCalledWith(src, dest);
expect(cpSync).toHaveBeenCalledWith(src, dest, { recursive: true });
expect(rmSync).toHaveBeenCalledWith(dest, { recursive: true, force: true });
});
});
32 changes: 30 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,33 @@ function resolveStoredPluginSource(lockEntry: LockEntry | undefined, pluginDir:
return lockEntry?.source ?? getPluginSource(pluginDir);
}

// ── Filesystem helpers ──────────────────────────────────────────────────────

/**
* Move a directory, with EXDEV fallback.
* fs.renameSync fails when source and destination are on different
* filesystems (e.g. /tmp → ~/.opencli). In that case we copy then remove.
*/
type MoveDirFsOps = Pick<typeof fs, 'renameSync' | 'cpSync' | 'rmSync'>;

function moveDir(src: string, dest: string, fsOps: MoveDirFsOps = fs): void {
try {
fsOps.renameSync(src, dest);
} catch (err: unknown) {
if ((err as NodeJS.ErrnoException).code === 'EXDEV') {
try {
fsOps.cpSync(src, dest, { recursive: true });
} catch (copyErr) {
try { fsOps.rmSync(dest, { recursive: true, force: true }); } catch {}
throw copyErr;
}
fsOps.rmSync(src, { recursive: true, force: true });
} else {
throw err;
}
}
}

// ── Validation helpers ──────────────────────────────────────────────────────

export interface ValidationResult {
Expand Down Expand Up @@ -292,7 +319,7 @@ function installSinglePlugin(
}

fs.mkdirSync(PLUGINS_DIR, { recursive: true });
fs.renameSync(cloneDir, targetDir);
moveDir(cloneDir, targetDir);

postInstallLifecycle(targetDir);

Expand Down Expand Up @@ -404,7 +431,7 @@ function installMonorepo(
// Move clone to permanent monorepos location (if not already there)
if (!fs.existsSync(repoDir)) {
fs.mkdirSync(monoreposDir, { recursive: true });
fs.renameSync(cloneDir, repoDir);
moveDir(cloneDir, repoDir);
}

let pluginsToInstall = getEnabledPlugins(manifest);
Expand Down Expand Up @@ -957,6 +984,7 @@ export {
getMonoreposDir as _getMonoreposDir,
installLocalPlugin as _installLocalPlugin,
isLocalPluginSource as _isLocalPluginSource,
moveDir as _moveDir,
resolveStoredPluginSource as _resolveStoredPluginSource,
toLocalPluginSource as _toLocalPluginSource,
};