Module: Aidp::SafeDirectory

Overview

Provides safe directory creation with automatic fallback for permission errors Used across AIDP components to handle CI environments and restricted filesystems

Instance Method Summary collapse

Instance Method Details

#safe_mkdir_p(path, component_name: "AIDP", skip_creation: false) ⇒ String

Safely create a directory with fallback to temp/home on permission errors

Parameters:

  • path (String)

    The directory path to create

  • component_name (String) (defaults to: "AIDP")

    Name of the component for logging (default: “AIDP”)

  • skip_creation (Boolean) (defaults to: false)

    If true, skip directory creation entirely (default: false)

Returns:

  • (String)

    The actual directory path (may differ from input if fallback occurred)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/aidp/safe_directory.rb', line 16

def safe_mkdir_p(path, component_name: "AIDP", skip_creation: false)
  return path if skip_creation
  return path if Dir.exist?(path)

  begin
    FileUtils.mkdir_p(path)
    path
  rescue SystemCallError => e
    fallback = determine_fallback_path(path)

    # Suppress permission warnings during tests to reduce noise
    unless ENV["RSPEC_RUNNING"] == "true"
      Kernel.warn "[#{component_name}] Cannot create directory #{path}: #{e.class}: #{e.message}"
      Kernel.warn "[#{component_name}] Using fallback directory: #{fallback}"
    end

    # Try to create fallback directory
    begin
      FileUtils.mkdir_p(fallback) unless Dir.exist?(fallback)
    rescue SystemCallError => e2
      # Suppress fallback errors during tests too
      unless ENV["RSPEC_RUNNING"] == "true"
        Kernel.warn "[#{component_name}] Fallback directory creation also failed: #{e2.class}: #{e2.message}"
      end
    end

    fallback
  end
end