Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 26, 2025

Fixes the issue where deleting a try directory that you're currently inside causes shell errors like:

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

Problem

When a user deletes a try directory they are currently working in using Ctrl-D, the shell's current working directory becomes invalid after the deletion. This causes subsequent shell operations to fail with getcwd errors because the shell can no longer determine its location.

Solution

The fix adds graceful error handling to the handle_delete method in try.rb:

  1. Detection: Before deletion, check if the current working directory is within the path being deleted
  2. Safe Navigation: After successful deletion, if the current directory was affected, change to a safe location using this fallback hierarchy:
    • Try the try base path (tries root directory)
    • If that fails, try the parent directory of the deleted path
    • As last resort, go to the home directory
  3. Error Handling: All directory changes are wrapped in rescue blocks to handle any potential failures gracefully

Testing

  • Added comprehensive tests in tests/test_delete_current_directory.rb that reproduce the issue and validate the fix
  • All existing tests continue to pass, ensuring no regressions
  • Manually verified the fix eliminates shell errors while preserving normal functionality

The implementation is minimal and surgical, only affecting the specific error case while maintaining all existing behavior for normal directory operations.

Original prompt

This section details on the original issue you should resolve

<issue_title>Error when deleting try when in same current directory</issue_title>
<issue_description>When deleting a try and you're in the same current working directory as that try, the app will error

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

Graceful error handling will need to be required in the event the deleted try is the same as the current working directory and/or default cd .. or go back to the default try apex directory.</issue_description>

Comments on the Issue (you are @copilot in this section)

@tobi Yes but I have a busy full time job 😆
Fixes #34

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] Error when deleting try when in same current directory Fix: Handle current directory deletion gracefully to prevent shell errors Sep 26, 2025
@Copilot Copilot AI requested a review from tobi September 26, 2025 15:33
Copilot finished work on behalf of tobi September 26, 2025 15:33
@all_tries = nil # Clear cache to reload tries

# If current directory was deleted, change to a safe location
if is_current_dir_affected
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this entire thing could be done vastly simpler by just appending an cd .. to the end of the script if the current directory is the deleted one.

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.

Error when deleting try when in same current directory

2 participants