Skip to content

cli: Allow opening new files in non-existent parent directories#42328

Closed
SadiqOnGithub wants to merge 5 commits intozed-industries:mainfrom
SadiqOnGithub:sadiq/fix-non-existent-dir-opening
Closed

cli: Allow opening new files in non-existent parent directories#42328
SadiqOnGithub wants to merge 5 commits intozed-industries:mainfrom
SadiqOnGithub:sadiq/fix-non-existent-dir-opening

Conversation

@SadiqOnGithub
Copy link
Contributor

Discussion: #42327

Previously, the zed CLI would fail when attempting to open a new file if any of its parent directories did not yet exist on filesystem (e.g., zed path/to/new_dir/new_file.txt where path/to/new_dir does not exist).

This failure occurred because the parse_path_with_position function unconditionally called canonicalize() on the provided argument. canonicalize() fails if any component of the path, including intermediate directories, does not exist, causing the CLI to with an error like "No such file or directory".

This change modifies the path parsing logic. If canonicalize() fails, it now falls back to resolving the path to an absolute relative to the current working directory. This allows a PathWithPosition to be constructed with a valid PathBuf that can represent file whose parent directories may not yet exist.

This correctly shifts the responsibility for creating intermediate directories (and the file itself) from the CLI to the main application, aligning with user expectations and the behavior of other modern editors.

Closes #ISSUE

Release Notes:

  • N/A or Added/Fixed/Improved ...

Discussion: zed-industries#42327

Previously, the `zed` CLI would fail when attempting to open a new file
if any of its parent directories did not yet exist on filesystem
(e.g., `zed path/to/new_dir/new_file.txt` where `path/to/new_dir` does not exist).

This failure occurred because the `parse_path_with_position` function
unconditionally called `canonicalize()` on the provided argument.
`canonicalize()` fails if any component of the path, including
intermediate directories, does not exist, causing the CLI to with
an error like "No such file or directory".

This change modifies the path parsing logic. If `canonicalize()` fails,
it now falls back to resolving the path to an absolute relative to the
current working directory. This allows a `PathWithPosition` to be
constructed with a valid `PathBuf` that can represent file whose
parent directories may not yet exist.

This correctly shifts the responsibility for creating intermediate
directories (and the file itself) from the CLI to the main
application, aligning with user expectations and the behavior
of other modern editors.
@cla-bot
Copy link

cla-bot bot commented Nov 10, 2025

We require contributors to sign our Contributor License Agreement, and we don't have @SadiqOnGithub on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@maxdeviant maxdeviant changed the title fix(cli): Allow opening new files in non-existent parent directories cli: Allow opening new files in non-existent parent directories Nov 10, 2025
@SadiqOnGithub
Copy link
Contributor Author

@cla-bot check

@cla-bot
Copy link

cla-bot bot commented Nov 10, 2025

We require contributors to sign our Contributor License Agreement, and we don't have @SadiqOnGithub on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cla-bot
Copy link

cla-bot bot commented Nov 10, 2025

The cla-bot has been summoned, and re-checked this pull request!

@SadiqOnGithub
Copy link
Contributor Author

@cla-bot check

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Nov 10, 2025
@cla-bot
Copy link

cla-bot bot commented Nov 10, 2025

The cla-bot has been summoned, and re-checked this pull request!

Copy link
Contributor Author

@SadiqOnGithub SadiqOnGithub left a comment

Choose a reason for hiding this comment

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

@maxdeviant

May I know why you think those comments were not needed?

@SomeoneToIgnore SomeoneToIgnore self-assigned this Nov 10, 2025
@maxdeviant
Copy link
Member

@maxdeviant

May I know why you think those comments were not needed?

Because they are just restating what the code is doing.

@maxdeviant maxdeviant force-pushed the sadiq/fix-non-existent-dir-opening branch from a6c519e to b460efa Compare November 10, 2025 15:57
@SomeoneToIgnore
Copy link
Contributor

Thank you for this PR and collaborating on another, I have looked at both and decided that #43250 is indeed a better approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants