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

Hooks provided in opts are lost in processing by install command #5306

Closed
AGrzes opened this issue Sep 4, 2022 · 8 comments · Fixed by #5403
Closed

Hooks provided in opts are lost in processing by install command #5306

AGrzes opened this issue Sep 4, 2022 · 8 comments · Fixed by #5403
Milestone

Comments

@AGrzes
Copy link
Contributor

AGrzes commented Sep 4, 2022

I was trying to get pnpm deploy (beta) feature to work and correctly "inject" my workspace modules and I noticed that they are not injected due to readPackage hook not being applied.

Here

if (!globalHooks && !hooks) return {}

empty object is returned if neither globalHooks nor hooks are set.
Simple change to

if (!globalHooks && !hooks) return opts.hooks || {}

Would prevent hooks set in opts from being lost.

f the change would be acceptable I will provide PR with tests.

pnpm version:

7.9.5

Code to reproduce the issue:

I can provide it if the description is not enough.

Expected behavior:

When hooks are provided in opts they are returned from requireHooks function

Actual behavior:

Hooks provided in opts are lost.

Additional information:

  • node -v prints: v14.20.0
  • Windows, macOS, or Linux?: Linux
AGrzes added a commit to AGrzes/pnpm that referenced this issue Sep 5, 2022
Pnpm deploy command is providing readPackage in opts and it is being discarded by requireHooks.
This change ensures, that hooks provided in opts are merged with those from local and global
pnpmfiles.

Closes pnpm#5306
AGrzes added a commit to AGrzes/pnpm that referenced this issue Sep 5, 2022
Pnpm deploy command is providing readPackage in opts and it is being discarded by requireHooks.
This change ensures, that hooks provided in opts are merged with those from local and global
pnpmfiles.

Closes pnpm#5306
AGrzes added a commit to AGrzes/pnpm that referenced this issue Sep 5, 2022
Pnpm deploy command is providing readPackage in opts and it is being discarded by requireHooks.
This change ensures, that hooks provided in opts are merged with those from local and global
pnpmfiles.

Closes pnpm#5306
@AGrzes
Copy link
Contributor Author

AGrzes commented Sep 7, 2022

Actually after working on PR I found out that the hooks provided in opts are not only ignored if there ar no local/ global hooks - they are always ignored. My PR addresses this issue and I would be grateful for some feedback - as I confirmed that changed version resolves my issue with workspace package not being "injected" when using deploy command.

@AGrzes AGrzes changed the title requireHooks in pnpmfile module ignores hooks provided in options if neither globalHooks nor hooks are set requireHooks in pnpmfile module ignores hooks provided in options Sep 8, 2022
@zkochan
Copy link
Member

zkochan commented Sep 9, 2022

I think we should do some refactoring. Probably @pnpm/pnpmfile should not merge the hooks and just return an array of hooks. Then the deploy command would just append a new hooks object to the array. Then later before usage (maybe in @pnpm/core) the hooks can be piped together. This part is already partially implemented:

const readPackageAndExtend = hooks.length === 1
? hooks[0]
: pipeWith(async (f, res) => f(await res), hooks as any) as ReadPackageHook // eslint-disable-line @typescript-eslint/no-explicit-any
if (readPackageHook != null) {
return (async (manifest: ProjectManifest, dir?: string) => readPackageAndExtend(await readPackageHook(manifest, dir), dir)) as ReadPackageHook
}
return readPackageAndExtend
}

@AGrzes
Copy link
Contributor Author

AGrzes commented Sep 10, 2022

I will look into this - if I understand You correctly you suggest that

  • @pnpm/pnpmfile would return array of hooks for hooks that can be "merged" later
  • the array is passed correctly to createReadPackageHook
  • hook from options is also passed in and handled by createReadPackageHook
    Am I right?

@AGrzes AGrzes changed the title requireHooks in pnpmfile module ignores hooks provided in options Hooks provided in opts are lost in processing by install command Sep 10, 2022
@zkochan
Copy link
Member

zkochan commented Sep 10, 2022

Sounds correct.

@AGrzes
Copy link
Contributor Author

AGrzes commented Sep 10, 2022

What I found out the issue happens only with option shared-workspace-lockfile = false - with shared workspace lock the hook is handled correctly.

@AGrzes
Copy link
Contributor Author

AGrzes commented Sep 10, 2022

The line

const hooks = opts.ignorePnpmfile ? {} : requireHooks(rootDir, opts)

coupled with

Causes hooks from opts to be discarded in favor of pnpmfile generated hooks before they can reach createReadPackageHook.

@AGrzes
Copy link
Contributor Author

AGrzes commented Sep 10, 2022

@zkochan one question - I found out that config module also uses requireHooks

pnpmConfig.hooks = requireHooks(pnpmConfig.lockfileDir ?? pnpmConfig.dir, pnpmConfig)

And it is very hard for me to trace places where change in type (to array) could have ripple effects - so I'm worried about changing type of readPackage hook to array in between requireHooks and createReadPackageHook because I'm not sure the flow will always lead to the later.

@zkochan
Copy link
Member

zkochan commented Sep 10, 2022

You will get typing errors, just run pnpm -w compile and you'll be able to find all places that need to be changed.

AGrzes added a commit to AGrzes/pnpm that referenced this issue Sep 24, 2022
Prevent hook provided in opts from being discarded

Closes pnpm#5306
AGrzes added a commit to AGrzes/pnpm that referenced this issue Sep 24, 2022
Prevent hook provided in opts from being discarded

Closes pnpm#5306
zkochan pushed a commit to AGrzes/pnpm that referenced this issue Sep 25, 2022
Prevent hook provided in opts from being discarded

Closes pnpm#5306
zkochan added a commit that referenced this issue Sep 25, 2022
Close #5306

Co-authored-by: Zoltan Kochan <z@kochan.io>
@zkochan zkochan added this to the v7.13 milestone Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants