Skip to content

Conversation

@harp-intel
Copy link
Contributor

Summary

Fixes #556 by automatically creating the output directory when using the --output flag, rather than requiring it to exist beforehand.

Problem

Previously, when using --output /path/to/dir, PerfSpect would exit with an error if the directory didn't exist:

Error: requested output dir, /path/to/dir, does not exist

This was inconsistent with the default behavior where output directories are created automatically.

Solution

Replace the existence check with a call to common.CreateOutputDir(), which:

  • Creates the directory if it doesn't exist using os.MkdirAll
  • Creates parent directories as needed (e.g., --output /path/to/nested/dir)
  • Sets appropriate permissions (0755)
  • Safely handles the case where the directory already exists
  • Provides clear error messages if creation fails (e.g., permission denied)

Changes

File: cmd/root.go

Replaced lines 160-168:

exists, err := util.DirectoryExists(outputDir)
if err != nil {
    fmt.Printf("Error: failed to determine if output dir exists: %v\n", err)
    os.Exit(1)
}
if !exists {
    fmt.Printf("Error: requested output dir, %s, does not exist\n", outputDir)
    os.Exit(1)
}

With:

// Create the directory if it doesn't exist
if err := common.CreateOutputDir(outputDir); err != nil {
    fmt.Printf("Error: failed to create output directory: %v\n", err)
    os.Exit(1)
}

Benefits

  1. Improved usability - Users no longer need to manually create directories
  2. Consistent behavior - --output now works like default output directories
  3. Safe implementation - Uses existing, tested infrastructure
  4. Better user experience - Creates nested directories automatically

Testing

✅ All existing unit tests pass
✅ Code quality checks pass (make format, make test)

Manual testing scenarios to verify:

  • --output /tmp/newdir creates the directory
  • --output /existing/dir works without error
  • --output /deeply/nested/new/dir creates all parents
  • Invalid paths (e.g., no permissions) fail with clear error message

@harp-intel harp-intel force-pushed the fix-556-create-output-directory branch from 6e338df to 89263ec Compare November 22, 2025 20:00
When using the --output flag, PerfSpect now accepts and creates
directories that don't exist yet, rather than requiring them to exist
beforehand. The directory is created early during initialization to
validate write permissions before data collection begins, preventing
wasted effort if the directory cannot be created.

Changes:
- Removed existence check that prevented specifying new directories
- Added early directory creation after logging setup
- Validates write access before any data collection starts
- Provides clear error messages if creation fails

Benefits:
- Improved usability - no manual directory creation needed
- Fail-fast validation - catches permission/disk issues immediately
- No wasted data collection - validates before work begins
- Creates nested directories automatically (e.g., /path/to/new/dir)
- Consistent with default output directory behavior

The directory is created at an optimal point in initialization:
- After basic setup (logging) is complete, so errors can be logged
- Before setting app context and starting any data collection
- Early enough to fail fast and save user time

Common failure modes handled gracefully:
- Permission denied - clear error before any work
- Disk full - detected immediately
- Read-only filesystem - fails at start
- Path conflicts (file exists) - caught early

Fixes #556

Signed-off-by: Jason Harper <jason.m.harper@intel.com>
Signed-off-by: Harper, Jason M <jason.m.harper@intel.com>
@harp-intel harp-intel force-pushed the fix-556-create-output-directory branch from 89263ec to 1b5d660 Compare November 22, 2025 21:23
When using the --output flag, PerfSpect now accepts and creates
directories that don't exist yet, rather than requiring them to exist
beforehand. The directory is created once, early during initialization,
to validate write permissions before data collection begins.

Changes:
- Removed existence check in initializeApplication()
- Added createOutputDir() function in root.go (single use, kept local)
- Create output directory early (after logging, before data collection)
- Only log creation when directory is actually created
- Removed redundant directory creation calls from commands
- Detects if output path exists as a file and fails with clear error

Benefits:
- Improved usability - no manual directory creation needed
- Fail-fast validation - catches permission/disk issues immediately
- No wasted data collection - validates write access before work begins
- Single point of directory creation - simpler, more maintainable
- Cleaner logs - only logs when directory is actually created
- Creates nested directories automatically (e.g., /path/to/new/dir)

Directory creation timing:
- After logging is configured (errors can be logged)
- Before app context is set and data collection starts
- Early enough to fail fast and save time

Error handling:
- Permission denied - clear error before any work
- Disk full - detected immediately
- Read-only filesystem - fails at start
- Path exists as file - caught with clear error
- Path already exists as directory - succeeds silently

Fixes #556

Signed-off-by: Jason Harper <jason.m.harper@intel.com>
Signed-off-by: Harper, Jason M <jason.m.harper@intel.com>
@harp-intel harp-intel force-pushed the fix-556-create-output-directory branch from 663e86e to 2cf99e0 Compare November 22, 2025 21:43
@harp-intel harp-intel merged commit fe58b1f into main Nov 23, 2025
5 checks passed
@harp-intel harp-intel deleted the fix-556-create-output-directory branch November 23, 2025 03:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create override output directory

2 participants