Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't mkdir directories that already exist #7899

Open
wants to merge 4 commits into
base: latest
Choose a base branch
from

Conversation

TheCSDev
Copy link

@TheCSDev TheCSDev commented Nov 9, 2024

This pull request aims to resolve #6412, by making checks for whether a project's working directory already exists, before attempting to mkdir it.
This effectively prevents reify.js from accidentally executing mkdir on root drive letters on Windows, like mkdir "D:/" for example, as the Windows operating system prohibits such operations.

The resolution of #6412 will inevitably introduce the npm error Tracker "idealTree" already exists issue, however I believe that is an issue separate from #6412, and that separate issue was already documented before, #7596 being a good example of one of those documentations (in my opinion).

As can be seen in #6412, this pull request makes changes to the following line of code in reify.js.

Of course, should one attempt to execute npm i in a drive letter that does not exist, then this error in my opinion should take place, informing the user that said drive letter and the path within cannot be created, and that error will take place. However this error in my opinion should not take place on existing drives, hence this pull request.

Additional info

Resolution attempt for npm#6412.

Unfortunately, resolution of that issue introduces another error; `Tracker "idealTree" already exists`.
@TheCSDev TheCSDev requested a review from a team as a code owner November 9, 2024 09:39
@wraithgar wraithgar changed the title fix: Resolve #6412; where npm i attempts to mkdir root directories on Windows fix: don't mkdir directories that already exist Nov 20, 2024
Addressed style and syntax issues as per GitHub Workflows feedback.
Now using `access` from `node:fs/promises` instead of `existsSync` from `node:fs`.
@wraithgar
Copy link
Member

npm run lintfix should fix linting errors.

Now using single-quotes instead of double-quotes.
@TheCSDev
Copy link
Author

Ah, it appears I accidentally used double-quotes instead of single-quotes for the string.
Also for some reason npm run lintfix did not spot any errors or make any "fixing" changes for me, so it took a bit of figuring out. I eventually found the npm run lint -w @npmcli/arborist command from a GitHub Workflows task, and that one was able to spot the syntax error I made.
I will now make one more commit to correct that error, and hopefully that should be it. Thank you for helping me.

@wraithgar
Copy link
Member

Oh shoot, sorry I forgot this was the cli repo. Yeah you gotta either be in the arborist directory or specify the workspace. I'll have to remember that when suggesting that in the future.

await access(resolvedPath)
} catch (accessError) {
if (accessError.code === 'ENOENT') {
await mkdir(resolvedPath, { recursive: true })
Copy link
Member

Choose a reason for hiding this comment

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

Just a note for future devs looking at this change. Any error other than ENOENT that access gets is going to also be thrown if attempting mkdir. I think only ignoring that error is the correct one.

if (accessError.code === 'ENOENT') {
await mkdir(resolvedPath, { recursive: true })
} else {
throw accessError
Copy link
Member

Choose a reason for hiding this comment

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

The reason tests are failing is because there aren't any that test for this scenario, i.e. an error other than ENOENT happening.

Copy link
Author

Choose a reason for hiding this comment

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

Will any changes have to be made to this code to resolve the failing tests in this case?

Copy link
Member

Choose a reason for hiding this comment

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

A test will have to be added to cover that code path.

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.

[BUG] npm i tries to recreate root directory (drive letter) in Windows
2 participants