Skip to content

Commit 25dbb07

Browse files
MK8S-25: Add detailed error reporting for directory cleanup failures
Enhance Mkdir cleanup to show tree view of directory contents when removal fails: - Custom _clean_with_details method for better error diagnosis - Uses 'tree -a' command for detailed directory tree view - Falls back to 'ls -la' if tree command is not available - Shows exactly what files are preventing directory removal - Helps diagnose "_build_root - cannot remove (it is not empty)" errors This will make debugging cleanup failures much easier by showing the exact files that are blocking directory removal.
1 parent eef60b9 commit 25dbb07

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

buildchain/buildchain/targets/directory.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
"""Provides operations on directories."""
55

66

7+
import subprocess
78
from pathlib import Path
89
from typing import Any
910

11+
from buildchain import coreutils
1012
from buildchain import types
1113
from buildchain import utils
1214

@@ -35,6 +37,7 @@ def task(self) -> types.TaskDict:
3537
{
3638
"title": utils.title_with_target1("MKDIR"),
3739
"actions": [(self._run, [task["targets"][0]])],
40+
"clean": [self._clean_with_details],
3841
"uptodate": [True],
3942
}
4043
)
@@ -43,3 +46,43 @@ def task(self) -> types.TaskDict:
4346
@staticmethod
4447
def _run(directory: Path) -> None:
4548
directory.mkdir(exist_ok=True)
49+
50+
def _clean_with_details(self) -> None:
51+
"""Clean directory with detailed error reporting if removal fails."""
52+
directory = self.targets[0]
53+
if not directory.exists():
54+
return
55+
56+
try:
57+
# Try to remove the directory
58+
coreutils.rm_rf(directory)
59+
except Exception as exc:
60+
# If removal fails, show detailed tree view of what's still there
61+
print(f"ERROR: Cannot remove directory '{directory}' - {exc}")
62+
print("Directory contents:")
63+
try:
64+
# Use the 'tree' command if available, otherwise use 'ls -la'
65+
try:
66+
result = subprocess.run(
67+
["tree", "-a", str(directory)],
68+
capture_output=True,
69+
text=True,
70+
timeout=30,
71+
)
72+
if result.returncode == 0:
73+
print(result.stdout)
74+
else:
75+
raise subprocess.CalledProcessError(result.returncode, "tree")
76+
except (subprocess.CalledProcessError, FileNotFoundError):
77+
# Fall back to ls -la if tree is not available
78+
result = subprocess.run(
79+
["ls", "-la", str(directory)],
80+
capture_output=True,
81+
text=True,
82+
timeout=30,
83+
)
84+
print(result.stdout)
85+
except Exception as list_exc:
86+
print(f"Could not list directory contents: {list_exc}")
87+
# Re-raise the original exception
88+
raise

0 commit comments

Comments
 (0)