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

Support cleaning the cache directory #696

Open
ryanccn opened this issue Mar 12, 2022 · 16 comments · May be fixed by #865
Open

Support cleaning the cache directory #696

ryanccn opened this issue Mar 12, 2022 · 16 comments · May be fixed by #865
Labels
enhancement New feature or request

Comments

@ryanccn
Copy link
Contributor

ryanccn commented Mar 12, 2022

It would be great if fnm supported cleaning up the cache directory. Currently, I have 2398 directories in the cache directory, and I think that having a command (which maybe runs periodically when you run any fnm command?)that removes everything in that directory is better than having to do so manually while the folders build up.

@ryanccn
Copy link
Contributor Author

ryanccn commented Aug 12, 2022

@Schniz

@Schniz
Copy link
Owner

Schniz commented Aug 16, 2022

hey there. There is no good solution unfortunately.

  1. these are not directories but symlinks. They don't really harm your computer but feel free to remove them: rm $(dirname $FNM_MULTISHELL_PATH)/*
  2. periodic cleanings aren't gonna cut it 100% because we need to know whether the shell is still in use. however this is not really possible because we can't know for sure which shell have evaluated this env. it was possible if we would say to init fnm with fnm env --pid=$$ but we don't.
  3. as I mentioned time does not tell us anything. We might be able to clean on restarts--but I don't know how to hook into that. If you know how to make this magic happen I'd be happy to pair or get your insights so we can bake this into fnm and clean up the symlinks directory.
  4. I have 937 symlinks (shocking, I expected way more). du $(dirname $FNM_MULTISHELL_PATH) return 0 bytes, so I am not concerned about it. should I?

@Schniz Schniz added the enhancement New feature or request label Aug 16, 2022
@aesadsklfjsl
Copy link

@Schniz
Copy link
Owner

Schniz commented Oct 28, 2022

Thanks for this!!!

@Schniz Schniz linked a pull request Nov 19, 2022 that will close this issue
@fearnliu
Copy link

Hello~ I don't know why fnm needs to create multishells symlinks. Even if you enter the same path again, it still creates a new and different symlinks. I don't understand why it was designed this way.

@ryanccn
Copy link
Contributor Author

ryanccn commented Jul 20, 2023

Hello~ I don't know why fnm needs to create multishells symlinks. Even if you enter the same path again, it still creates a new and different symlinks. I don't understand why it was designed this way.

@fengyinxu It's because each shell instance can be using its own version of Node.js, so each multishell symlink is specific to one shell instance.

@fearnliu
Copy link

Hello~ I don't know why fnm needs to create multishells symlinks. Even if you enter the same path again, it still creates a new and different symlinks. I don't understand why it was designed this way.

@fengyinxu It's because each shell instance can be using its own version of Node.js, so each multishell symlink is specific to one shell instance.

Typically, what we need is a different version of Nodejs for each project, not each shell instance. I'm not familiar with rust. I'm curious as to why fnm designed it this way (cache, multishells) What is the need for this design. I see there is a PR #865 associated with the issue, but it's a pity that the PR has not been merged now.

@ryanccn
Copy link
Contributor Author

ryanccn commented Jul 20, 2023

@fengyinxu Commands like fnm use change the node version the shell is using; even .node-version and engines.node support set the node version in the shell where you cded into the project.

@MunifTanjim
Copy link

Is there a reason for not creating these symlinks in $TMPDIR? 🤔

@Aalivexy
Copy link

Aalivexy commented Aug 7, 2024

For fish shell user:

function fnm_clean_up --on-event fish_exit
    rm -r $FNM_MULTISHELL_PATH
end

@Schniz
Copy link
Owner

Schniz commented Oct 6, 2024

the problem with using hooks is that almost no one exits their shell cleanly, so that hook will not be called. Maybe fish works better than what I tested?

@Schniz
Copy link
Owner

Schniz commented Oct 6, 2024

Is there a reason for not creating these symlinks in $TMPDIR? 🤔 @MunifTanjim

sorry for replying after more than 1 year. fnm multishells used to be in TMPDIR. Unfortunately, a MacOS change in behavior of tmpdirs made them clean up on hibernation instead of reboot. we had bug reports in this regard, which the new multishell directory fixed. I didn't find any literature about this change but changing the directory fixed it for good, so we left it like that.

I think it's a good tradeoff to have: symlinks are mostly 0-sized and fast, and working software is better than broken software

@MunifTanjim
Copy link

Is there a way to add a cleanup command? Maybe add the pid of the shell as a prefix for FNM_MULTISHELL_PATH file. And the cleanup command can remove the file if the process for that pid is not running anymore?

Or maybe just add the pid prefix part, and no need to add the command. People who wants to cleanup can write their own script?

@Schniz
Copy link
Owner

Schniz commented Oct 6, 2024

the problem with pid is that it isn't a shell command but an executable, so I need to use ppid for the parent id, but that doesn't cut it either, because what if you have a "setup script" that calls fnm for your shell?

# my .zshrc
eval "$(~/.config/setup-fnm)"

# ~/setup
fnm env --use-on-cd

then we need to traverse the process tree to understand what's the closest shell

however we can do fnm env --pid $PID as an optional argument which will tell "This is the current shell". again it will be problematic when it is composed in another script, but might help cleaning up stuff if necessary

I don't think there's a good and easy solution here. do you have something in mind?

@Schniz
Copy link
Owner

Schniz commented Oct 6, 2024

tldr the main concern is that using PID might hurt composition

@secondetik
Copy link

I don't realize this issue already raised as I created my suggestion on #1374

I see that the problem would be on ensuring the clean up command to be executed on any shell exit?
Using PID sounds complicated so I'm not sure about that.

Also, it makes me wonder why do we need symlink in the first place? Why not assign the path directly to avoid creation of symlink?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants