Skip to content

'cd ..' command does not handle root directory correctly" #35

@Harushii16

Description

@Harushii16

When running cd .. multiple times from the root directory in MyCMD, the shell continues to move “above” the root (resulting in invalid paths like null or empty strings).

Example:

C:> cd ..
C:> cd ..
C:> cd ..

Expected behavior: Once you reach the filesystem root (e.g., C:\ or /), further cd .. should keep you at the same directory and not break the context.

Steps to Reproduce

Launch MyCMD.

Type cd .. repeatedly while at the root directory.

Observe the shell context path.

Expected Result

The shell should stay at the root once reached, and not attempt to go higher.

Actual Result

Path becomes invalid, and commands like dir or type may fail afterward.

Proposed Fix

Add a check in the CdCommand class to prevent navigating above root:

If the new path after resolving .. is invalid or empty, keep current directory unchanged.

Print a user-friendly message like:
"Already at the root directory."

Impact

This fix prevents path errors, ensures stable navigation, and improves command reliability.

I’d like to submit a PR implementing this fix under Hacktoberfest.

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions