diff --git a/packages/js-sdk/src/template/utils.ts b/packages/js-sdk/src/template/utils.ts index 34670767c4..be47e53b3e 100644 --- a/packages/js-sdk/src/template/utils.ts +++ b/packages/js-sdk/src/template/utils.ts @@ -86,37 +86,23 @@ export async function getAllFilesInPath( includeDirectories: boolean = true ) { const { glob } = await dynamicImport('glob') - const files = new Map() - const globFiles = await glob(src, { + // Match both the pattern and its recursive contents in one call + // This handles directories (src -> src + src/**/*) and file patterns (*.txt -> just files) + const normalizedSrc = normalizePath(src) + const patterns = [normalizedSrc, `${normalizedSrc}/**/*`] + + const globFiles = await glob(patterns, { ignore: ignorePatterns, withFileTypes: true, - // this is required so that the ignore pattern is relative to the file path + nodir: !includeDirectories, cwd: contextPath, }) + // Deduplicate by full path + const files = new Map() for (const file of globFiles) { - if (file.isDirectory()) { - // For directories, add the directory itself and all files inside it - if (includeDirectories) { - files.set(file.fullpath(), file) - } - const dirPattern = normalizePath( - // When the matched directory is '.', `file.relative()` can be an empty string. - // In that case, we want to match all files under the current directory instead of - // creating an absolute glob like '/**/*' which would traverse the entire filesystem. - path.join(file.relative() || '.', '**/*') - ) - const dirFiles = await glob(dirPattern, { - ignore: ignorePatterns, - withFileTypes: true, - cwd: contextPath, - }) - dirFiles.forEach((f) => files.set(f.fullpath(), f)) - } else { - // For files, just add the file - files.set(file.fullpath(), file) - } + files.set(file.fullpath(), file) } return Array.from(files.values()).sort() diff --git a/packages/python-sdk/e2b/template/utils.py b/packages/python-sdk/e2b/template/utils.py index 09f87ac095..e86a99ba85 100644 --- a/packages/python-sdk/e2b/template/utils.py +++ b/packages/python-sdk/e2b/template/utils.py @@ -81,36 +81,27 @@ def get_all_files_in_path( :param include_directories: Whether to include directories :return: Array of files """ - files = set() - - # Use glob to find all files/directories matching the pattern under context_path abs_context_path = os.path.abspath(context_path) + normalized_src = normalize_path(src) + + # Match both the pattern and its recursive contents in one call + # This handles directories (src -> src + src/**/*) and file patterns (*.txt -> just files) + patterns = [normalized_src, f"{normalized_src}/**/*"] + files_glob = glob.glob( - src, + patterns, flags=glob.GLOBSTAR, root_dir=abs_context_path, exclude=ignore_patterns, ) + # Deduplicate and convert to absolute paths + files = set() for file in files_glob: - # Join it with abs_context_path to get the absolute path file_path = os.path.join(abs_context_path, file) - - if os.path.isdir(file_path): - # If it's a directory, add the directory and all entries recursively - if include_directories: - files.add(file_path) - dir_files = glob.glob( - normalize_path(file) + "/**/*", - flags=glob.GLOBSTAR, - root_dir=abs_context_path, - exclude=ignore_patterns, - ) - for dir_file in dir_files: - dir_file_path = os.path.join(abs_context_path, dir_file) - files.add(dir_file_path) - else: - files.add(file_path) + if not include_directories and os.path.isdir(file_path): + continue + files.add(file_path) return sorted(list(files))