zth / rescript-relay

Use Relay with ReScript.
https://rescript-relay-documentation.vercel.app/docs/getting-started
338 stars 53 forks source link

fix: Protect with try ~ catch on postinstall. #368

Closed jeong-sik closed 2 years ago

jeong-sik commented 2 years ago

Postinstall error when using yarn workspace. A patch that protects this case.

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

➤ YN0000: │ Some peer dependencies are incorrectly met; run yarn explain peer-requirements <hash> for details, where <hash> is the six-letter p-prefixed code
➤ YN0000: └ Completed in 0s 252ms
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed in 0s 613ms
➤ YN0000: ┌ Link step
➤ YN0007: │ rescript-relay@npm:1.0.0-beta.20 [99da6] must be built because it never has been before or the last one failed
➤ YN0009: │ rescript-relay@npm:1.0.0-beta.20 [99da6] couldn't be built successfully (exit code 1, logs can be found here: /private/var/folders/_9/4r7lk75947nd53l3751p8ltw0000gn/T/xfs-0f7f7f6d/build.log)

build.log

# This file contains the result of Yarn building a package (rescript-relay@virtual:99da68c324550f8710c415b829bbd9a394bff34ad8514f31346cdabf0527966a11332d6b07f65324969b2168a9318e26017d916aa168344f5c553310763e3dc8#npm:1.0.0-beta.20)
# Script name: postinstall

Error: ENOENT: no such file or directory, copyfile '/some/node_modules/rescript-relay/ppx-darwin' -> '/some/node_modules/rescript-relay/ppx'
    at Object.copyFileSync (node:fs:2800:3)
    at copyPlatformBinaries (/some/node_modules/rescript-relay/postinstall.js:101:7)
    at Object.<anonymous> (/some/node_modules/rescript-relay/postinstall.js:158:5)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47 {
  errno: -2,
  syscall: 'copyfile',
  code: 'ENOENT',
  path: '/some/node_modules/rescript-relay/ppx-darwin',
  dest: '/some/node_modules/rescript-relay/ppx'
}
node:internal/fs/utils:344
    throw err;
    ^

Error: ENOENT: no such file or directory, unlink '/some/node_modules/rescript-relay/ppx-darwin'
    at Object.unlinkSync (node:fs:1718:3)
    at removeInitialBinaries (/some/node_modules/rescript-relay/postinstall.js:127:6)
    at Object.<anonymous> (/some/node_modules/rescript-relay/postinstall.js:165:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47 {
  errno: -2,
  syscall: 'unlink',
  code: 'ENOENT',
  path: '/some/node_modules/rescript-relay/ppx-darwin'
}
zth commented 2 years ago

Hmm, copying the right binaries in postinstall is essential for rescript-relay to work. How does that work in workspaces?

jeong-sik commented 2 years ago

Hmm, copying the right binaries in postinstall is essential for rescript-relay to work. How does that work in workspaces?

/packages/workspace-a /packages/workspace-b

using yarn berry Workspaces a and b both use rescript-relay .

{
  "name": "workspace-a",
  "version": "0.0.1",
  "dependencies": {
    "rescript-relay": "1.0.0-beta.20"
  }
}

{
  "name": "workspace-b",
  "version": "0.0.1",
  "dependencies": {
    "rescript-relay": "1.0.0-beta.20"
  }
}

I'm not sure why exactly, but it looks like postinstall.js is executed twice in the yarn link step.

So, after the first copyPlatformBinaries and removeInitialBinaries functions are executed, an error seems to occur when the next copyPlatformBinaries function is executed.

$ yarn
➤ YN0000: ┌ Project validation
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed in 0s 696ms
➤ YN0000: ┌ Link step
➤ YN0007: │ rescript-relay@npm:1.0.0-beta.20 [2f1dd] must be built because it never has been before or the last one failed
➤ YN0009: │ rescript-relay@npm:1.0.0-beta.20 [2f1dd] couldn't be built successfully (exit code 1, logs can be found here: /private/var/folders/_9/4r7lk75947nd53l3751p8ltw0000gn/T/ xfs-50b4fda1/build.log)
➤ YN0000: └ Completed in 1s 74ms
Running `afterInstall` hook...
➤ YN0000: Done in 0s
➤ YN0000: Failed with errors in 3s 691ms
jeong-sik commented 2 years ago

Come to think of it, this seems to be a yarn issue, not this project issue. 😅🙏