link-module-alias icon indicating copy to clipboard operation
link-module-alias copied to clipboard

This deleted all my source files?

Open OmgImAlexis opened this issue 6 years ago β€’ 70 comments

So I used yarn as usual to add a new dependancy and from the looks of it all my source that was symlinked via node_modules was removed.

Lucky I had just committed and could use git to retrieve the files but this looks like a huge issue.

PING: @Rush

OmgImAlexis avatar Jan 11 '19 05:01 OmgImAlexis

I'm testing but I believe the pre-install script is what would have prevented this.

~Maybe there should be more than a small message in the readme about it? Personally I'd suggest having the package check for the preinstall hook and add it unless there's a flag passed.~

~Something like --no-install?~

OmgImAlexis avatar Jan 11 '19 05:01 OmgImAlexis

So I used yarn as usual to add a new dependancy and from the looks of it all my source that was symlinked via node_modules was removed.

Wow, this sounds bad. Can you show me the structure of your project? Btw. just yesterday I discovered that it's possible to link subdirectories via a file: version specifier, is it the way of your project?

Rush avatar Jan 11 '19 09:01 Rush

Maybe there should be more than a small message in the readme about it? Personally I'd suggest having the package check for the preinstall hook and add it unless there's a flag passed.

Can you elaborate? I am not sure I understand.

link-module-alias shouldn't at any circumstances remove actual source code - this needs to be fixed.

Rush avatar Jan 11 '19 09:01 Rush

Clone this repo and try the steps below. Not sure if this also happens with npm, just wanted to get this to you ASAP.

➜  delete-bug git:(master) rm -rf node_modules
➜  delete-bug git:(master) yarn
yarn install v1.12.3
warning package.json: No license field
warning No license field
[1/4] πŸ”  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] πŸ”—  Linking dependencies...
[4/4] πŸ“ƒ  Building fresh packages...

$ link-module-alias
[####################] 20/20link-module-alias: @components -> ./components
✨  Done in 0.80s.
➜  delete-bug git:(master) node main.js
TEST_COMPONENT
➜  delete-bug git:(master) yarn add fuse.js
yarn add v1.12.3
warning package.json: No license field
warning No license field
[1/4] πŸ”  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] πŸ”—  Linking dependencies...
[4/4] πŸ“ƒ  Building fresh packages...

warning No license field
success Saved 1 new dependency.
info Direct dependencies
└─ [email protected]
info All dependencies
└─ [email protected]
$ link-module-alias
link-module-alias: @components -> ./components
✨  Done in 0.82s.
➜  delete-bug git:(master) βœ— node main.js
file:///Users/xo/code/delete-bug/index.js:1
Error: Cannot find module '@components'
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:734:10)

OmgImAlexis avatar Jan 11 '19 10:01 OmgImAlexis

PING @Rush

OmgImAlexis avatar Jan 14 '19 01:01 OmgImAlexis

@OmgImAlexis I ran your steps - very helpful! .. and it looks like it's a bug in yarn ... I'll investigate it further

Rush avatar Jan 14 '19 11:01 Rush

@OmgImAlexis it's not a permanent solution but this would help avoid this particular issue:

   "scripts": {
     "preinstall": "which link-module-alias && link-module-alias clean || echo",
     "postinstall": "link-module-alias"
   }

Rush avatar Jan 14 '19 11:01 Rush

@OmgImAlexis the upstream bug is #5709

I added a comment to that issue thread: https://github.com/yarnpkg/yarn/issues/5709#issuecomment-453984390

Rush avatar Jan 14 '19 12:01 Rush

@OmgImAlexis the thread seems to indicate that another workaround is not using @ in front of the aliased package name.

Rush avatar Jan 14 '19 12:01 Rush

This happened to me a few times in the last week and it took me a while to understand that using yarn with link-module-alias was the cause of the issue. I haven't been able to reproduce it consistently, it will just happen once in a while. @Rush Is this issue only fixable on yarn's side?

josecarneiro avatar Jan 20 '19 01:01 josecarneiro

@josecarneiro this patch does fix the issue.

https://github.com/Rush/link-module-alias/issues/3#issuecomment-453976562

OmgImAlexis avatar Jan 20 '19 01:01 OmgImAlexis

@josecarneiro this patch does fix the issue.

#3 (comment)

I added it to my package.json and haven't been able to reproduce the issue ever since, so it seems like a plausible solution until yarn solves the underlying problem.

Thank you @OmgImAlexis and @Rush!

josecarneiro avatar Jan 21 '19 18:01 josecarneiro

Well, it did happen again after running yarn upgrade, even with the fix. It appears that the upgrade command does not run the preinstall hook. Just a heads up for anyone who may be encountering the same issue.

josecarneiro avatar Jan 26 '19 03:01 josecarneiro

This looks related. Possibly a bug?

https://github.com/yarnpkg/yarn/issues/2878

OmgImAlexis avatar Jan 26 '19 04:01 OmgImAlexis

Is not using @ in the package name an option for you guys?

Rush avatar Jan 26 '19 10:01 Rush

@Rush I'd prefer if we could keep using it. Any chance @/ or # would work?

OmgImAlexis avatar Jan 27 '19 01:01 OmgImAlexis

I find that using Β΄@Β΄ is one of the great appeals of this module. Wanting to keep consistency between my backend codebase and my webpack-built client side app is the reason I researched this topic and consequentially found Β΄link-module-aliasΒ΄. Changing the alias is a possibility.

josecarneiro avatar Jan 28 '19 19:01 josecarneiro

@Rush Having the preinstall hook do a link-module-alias clean fixes it for me when doing npm install. HOWEVER, npm update doesn't run the pre or post install hooks and will happily delete all of my files. I would usually dig in and help debug this but frankly after having this delete in progress work about 5 times now I'm a bit too pissed to be helpful. At the moment this project is completely useless if you can't use standard npm install and npm update commands without it deleting work.

shawncplus avatar Feb 02 '19 19:02 shawncplus

I want to add to @shawncplus 's comment that this does not seem to be yarn-related at all, since it deletes files in plain npm as well.

seanohue avatar Feb 06 '19 02:02 seanohue

Uh, I wasn't lucky enough to be aware of this issue before it deleted my day of work. I always forget to commit more frequently :smile:

jsardev avatar Mar 23 '19 00:03 jsardev

At the moment this project is completely useless if you can't use standard npm install and npm update commands without it deleting work.

Have you tried naming your modules without the @ prefix?

jsardev avatar Mar 23 '19 00:03 jsardev

"module-alias" was not working with the ESLint, so installed this one. This module worked perfectly.

And the moment I ran "yarn remove module-alias", everything in my project directory got deleted including .git

I just have just package.json and yarn.lock in directory (and the aliased directory @utils - empty).

My local commits got lost, several hours of work. Cool!

If this module has an issue, please add a big RED BOX on the npm page that this can potentially wipe the whole directory.

mfaheemakhtar avatar Apr 03 '19 14:04 mfaheemakhtar

If this module has an issue, please add a big RED BOX on the npm page that this can potentially wipe the whole directory.

I am very sorry you've lost your work. :-( Can you share the setup that caused everything to disappear? We can then prevent doing this particular set up or at least warning against it.

Rush avatar Apr 03 '19 17:04 Rush

@Rush, I don't have anything because everything got wiped. So, here is the explanation - no code.

  1. Directories - constants, config, utils, db, api.
  2. ESLint + Prettier config files. (.eslintrc, .prettierrc).
  3. jsconfig.json to make aliases work for ESLint. Had added for "module-alias" but did not work.
  4. And the configuration for the module as provided in the docs.

"_moduleAliases": { "@api": "./api", "@constants": "./constants", "@config": "./config", "@db": "./db", "@root": ".", "@utils": "./utils" }

ESLint was not working with "module-alias" package, so I installed this one (yours) and ESLint worked. Then the moment I ran yarn remove module-alias, everything got deleted (not even in the Trash).

I came to know that there is an issue with this module after it, if you could please add some warning with a link to this issue, it will be really helpful.

I'm ashamed that I had not pushed my work on GitHub.

BTW, I'm on Linux 18.04.2.

mfaheemakhtar avatar Apr 03 '19 19:04 mfaheemakhtar

There's no need to be ashamed @mfaheemakhtar, you're in good company based on this thread. I second the idea of a huge warning on the NPM page for this module, or even unpublishing it, until the issue is resolved.

seanohue avatar Apr 03 '19 19:04 seanohue

@seanohue @mfaheemakhtar added the warning. Let me know if you guys think it's sufficient ...

Rush avatar Apr 03 '19 19:04 Rush

I think I will add a warning at runtime that discourages the use of modules starting with @ .

Rush avatar Apr 03 '19 20:04 Rush

I think I will add a warning at runtime that discourages the use of modules starting with @ .

I think that's a good idea! Perhaps even throw an error if they use such an alias. I'm willing to make a PR that does this.

Thanks for being so responsive with this issue.

seanohue avatar Apr 03 '19 20:04 seanohue

I'm willing to make a PR that does this.

You're very welcome to give it a first shot. Thanks!

Rush avatar Apr 03 '19 20:04 Rush

Thanks for the quick response @Rush. Really appreciate it.

Please also consider adding the warning on the npm page and change the usage example.

I read the comments above and the issues with yarn. One way to fix it could to not store the symlinks in the node_modules folder at all.

The module can create a directory for symlinks (.aliases, or anything), and add that path to the module.paths. This way (I think), the node_modules folder will be untouched and both yarn and npm should be working fine.

I'll try this module again without the "@" now, and will not add any alias for root. I guess my .git folder was removed because I had a root entry. So, even if this happens again, at least I'll have my .git folder to recover my work.

mfaheemakhtar avatar Apr 04 '19 05:04 mfaheemakhtar