yarnpkg / yarn

The 1.x line is frozen - features and bugfixes now happen on https://github.com/yarnpkg/berry
https://classic.yarnpkg.com
Other
41.4k stars 2.72k forks source link

Error when using 'postinstall' in a yarn workspace's package #7694

Open kripod opened 4 years ago

kripod commented 4 years ago

Do you want to request a feature or report a bug?

I'm reporting a bug.

If the current behavior is a bug, please provide the steps to reproduce.

When using Yarn workspaces with the configuration below:

// root package.json
{
  "private": true,
  "workspaces": [
    "client",
    "server"
  ],
  // ...
}

The postinstall script inside server/package.json cannot run properly. It tries to invoke a binary of its own dependency, as seen below:

server/package.json
{
  "private": true,
  "name": "server",
  "version": "0.0.0",
  "scripts": {
    "postinstall": "prisma2 generate"
  },
  "dependencies": {
    "prisma2": "^2.0.0-preview016.2"
  }
}

And the following error gets thrown when executing yarn from the root directory:

[4/4] Building fresh packages...
error D:\Development\Projects\app\node_modules\server: Command failed.
Exit code: 1
Command: prisma2 generate
Arguments:
Directory: D:\Development\Projects\app\node_modules\server
Output:
internal/modules/cjs/loader.js:783
    throw err;
    ^

Error: Cannot find module 'D:\Development\Projects\app\node_modules\node_modules\prisma2\build\index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:780:15)
    at Function.Module._load (internal/modules/cjs/loader.js:685:27)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1014:10)
    at internal/main/run_main_module.js:17:11 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

As it can be seen, it tries to invoke D:\Development\Projects\app\node_modules\node_modules\prisma2\build\index.js, which does not exist. When running yarn from the 'server' directory, everything works as expected.

I think that before executing postinstall scripts for each package, the current directory should be modified to simulate execution straight from the package instead of the workspace root.

Please mention your node.js, yarn and operating system version.

Node.js: 12.11.1 Yarn: 1.19.1 Windows 10 x64

AiTranXuan commented 4 years ago

Same Error and I using: Node:12.14.0 Yarn:1.21.1 Macos 10.15.2 (19C57)

alejandroclaro commented 4 years ago

Same Error when try to execute electron-rebuild in the postinstall of a package. This only happens in windows. In Linux is working properly.

Ravnurin commented 4 years ago

Experiencing same problem on Windows 10, with postinstall trying to find the module under \node_modules\node_modules\....

Kadeluxe commented 4 years ago

What is the state of this? I can't execute tsc from postinstall script, same error.

cdegalitt commented 3 years ago

Hi,

I'm facing the same issue here.

Environment:

Description: I stumbled upon this after changing my package names to scoped package names (in the form @company/package).

Trace:

[4/4] Building fresh packages...
error C:\Projects\galitt-l3-tse\node_modules\@galitt\exception: Command failed.
Exit code: 1
Command: run-s build
Arguments:
Directory: C:\Projects\galitt-l3-tse\node_modules\@galitt\exception
Output:
yarn run v1.22.5
$ echo $$NPM_EXEC_PATH && run-s build:code build:types
$$NPM_EXEC_PATH
$ echo $$NPM_EXEC_PATH && tsc
$$NPM_EXEC_PATH
internal/modules/cjs/loader.js:1033
  throw err;
  ^

Error: Cannot find module 'C:\Projects\galitt-l3-tse\node_modules\node_modules\typescript\bin\tsc'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1030:15)
    at Function.Module._load (internal/modules/cjs/loader.js:899:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
error Command failed with exit code 1.

Some more details: I noticed wrappers to tsc seems to have been created inside the sub-package's node_modules by yarn install.

Contents of /c/Projects/galitt-l3-tse/node_modules/@galitt/exception/node_modules/.bin/tsc below:

#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")

case `uname` in
    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac

if [ -x "$basedir/node" ]; then
  "$basedir/node"  "$basedir/../../../../typescript/bin/tsc" "$@"
  ret=$?
else 
  node  "$basedir/../../../../typescript/bin/tsc" "$@"
  ret=$?
fi
exit $ret

It seems that the path %~dp0\..\..\..\..\ resolves to the root node_modules folder, and the wrapper script is appending node_modules to it.

Hence we lookup for the binary inside ...\node_modules\node_modules\... instead of ...\node_modules\...

One can notice there is also a windows shell script named tsc.cmd that follows the same (flawed ?) logic.

Fun fact: When I cd into /c/Projects/galitt-l3-tse/node_modules/@galitt/exception and execute yarn postinstall manually, it works just fine.

Question: Does somebody have any idea how those wrappers are generated ? And if it is possible how to fix them so they can properly implement the node module resolution algorithm by recursively looking upwards ?

starikcetin commented 3 years ago

Same issue here, it is trying to resolve to root\node_modules\node_modules\package instead of root\node_modules\package.

Windows 10 Node 12.0.0 npm 6.9.0 yarn 1.22.10

Putting the postinstall scripts at the root package.json solved the issue for me, but ideally, I would want them localized.

miguelcast commented 1 month ago

I have the same problem, but trying to postinstall in my monorepo Husky o Lefthook in windows with yarn 1.22.22