From b9336dd3e241bf5c888c50b13342eb65e414f1e7 Mon Sep 17 00:00:00 2001 From: raj pandey Date: Fri, 24 Jul 2026 13:50:06 +0530 Subject: [PATCH] fix: resolve package root by directory depth in clone command The clone command computed its content directory path by substring- splitting __dirname on '/src/' or '/lib/'. In a published/installed layout this breaks because the install path itself can contain '/lib/' (e.g. Node's own lib/node_modules), so the split matched the wrong occurrence and resolved to an ancestor directory instead of the package root. As a result, the pre-clone check, the post-import cleanup, and the interrupt-handler cleanup were all deleting an unrelated, effectively empty directory, while the actual export/import data (written to the correct path via clone-handler.ts's own resolution) was never cleaned up. Content from a previous clone run persisted on disk and bled into subsequent clones of a different stack. Resolve packageRoot by fixed relative directory depth instead, matching the approach already used in clone-handler.ts. This is independent of the surrounding install path and has been verified against nested, flat/hoisted, and pnpm virtual-store install layouts. --- packages/contentstack-clone/src/commands/cm/stacks/clone.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/contentstack-clone/src/commands/cm/stacks/clone.ts b/packages/contentstack-clone/src/commands/cm/stacks/clone.ts index 06b44a255..2e826f738 100644 --- a/packages/contentstack-clone/src/commands/cm/stacks/clone.ts +++ b/packages/contentstack-clone/src/commands/cm/stacks/clone.ts @@ -15,8 +15,10 @@ import { readFileSync, promises as fsPromises } from 'fs'; import { CloneConfig } from '../../../types/clone-config'; import { CloneContext } from '../../../types/clone-context'; -// Resolve path to package root (works in both src and lib contexts) -const packageRoot = __dirname.includes('/src/') ? __dirname.split('/src/')[0] : __dirname.split('/lib/')[0]; +// Resolve path to package root by directory depth (commands/cm/stacks is 3 levels below src|lib), +// matching the pattern used in clone-handler.ts. Avoids breaking when the install path itself +// contains '/lib/' (e.g. Node's own lib/node_modules), which substring-splitting did not handle. +const packageRoot = path.resolve(__dirname, '../../../..'); const pathdir = path.join(packageRoot, 'contents'); let config: CloneConfig = {};