amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

unable to `amplify build function` when using yarn 3

Open ginobean opened this issue 2 years ago • 6 comments

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

16.19.1

Amplify CLI Version

12.2.0

What operating system are you using?

Ubuntu

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No

Describe the bug

I used 'amplify add function' to add a lambda, with the default 'Hello World' js option.

Then, I tried to run 'amplify function build', and got this error:

Packaging lambda function failed with the error 
Command failed with exit code 1: yarn install
Usage Error: The nearest package directory (/home/gino/local/amplify-app/amplify/backend/function/foobar/src) doesn't seem to be part of the project declared in /home/gino/local/amplify-app.

BTW, as a side note, in trying to resolve this issue, I noticed that the package.json, for the lambda function, was in the src subdirectory for the function. Out of curiosity, I tried moving package.json to the parent of src directory, e.g. 'mv package.json ..' . And 'amplify add function' no longer errored out. Not sure if this is the correct resolution..

I believe 'amplify push' also yields the exact same error message as 'amplify add function'. And moving the package.json, for the lambda function, from the src subdir to the parent of the src directory, 'mv package.json ..' also seemed to enable 'amplify push' to work correctly.

Expected behavior

Expection is that the function would be built correctly.

Reproduction steps

See the bug description for the reproducible steps.

Project Identifier

No response

Log output

# Put your logs below this line


Additional information

No response

Before submitting, please confirm:

  • [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • [X] I have removed any sensitive information from my code snippets and submission.

ginobean avatar Jul 25 '23 09:07 ginobean

Hey @ginobean :wave: thanks for raising this! Out of curiosity what version of yarn are you using? Is there a workspaces setting that is picking up this directory?

josefaidt avatar Jul 25 '23 15:07 josefaidt

yarn 3.5.0 Could not find any workspaces setting that is picking up the lambda related directory.

ginobean avatar Jul 25 '23 17:07 ginobean

Hey @ginobean thanks for clarifying! I'm not able to reproduce with the latest yarn berry release and amplify add function. Would you mind sharing the full command execution output? I'm curious if there is some setting or prompt that is triggering this

josefaidt avatar Jul 25 '23 17:07 josefaidt

Hey @ginobean I was able to reproduce this by running amplify build

➜  amplify build function
✔ Are you sure you want to continue building the resources? (y/N) · yes
🛑 Packaging lambda function failed with the error 
Command failed with exit code 1: yarn install
➤ YN0070: Migrating from Yarn 1; automatically enabling the compatibility node-modules linker 👍

Usage Error: The nearest package directory (/Users/josef/Documents/projects/aws-amplify/reproductions/12881/amplify/backend/function/1288141ced489/src) doesn't seem to be part of the project declared in /Users/josef/Documents/projects/aws-amplify/reproductions/12881/amplify/backend.

However the error is also printing suggestions that seem like a reasonable solution:

...
- Finally, if /Users/josef/Documents/projects/aws-amplify/reproductions/12881/amplify/backend is fine and you intend function/1288141ced489/src to be treated as a completely separate project (not even a workspace), create an empty yarn.lock file in it.

Although after creating empty yarn.lock files I am presented with a different error:

➜  amplify build function
✔ Are you sure you want to continue building the resources? (y/N) · yes
🛑 Received error [Error: Command failed with exit code 1: yarn --no-bin-links --production
Unknown Syntax Error: Unsupported option name ("--no-bin-links").

This appears to be failing to resolve the correct yarn version to change the install command https://github.com/aws-amplify/amplify-cli/blob/dev/packages/amplify-cli-core/src/utils/packageManager.ts#L148-L152

Marking as a bug

josefaidt avatar Jul 25 '23 18:07 josefaidt

+1 See also https://github.com/aws-amplify/amplify-cli/issues/13377

codenimble avatar Jan 19 '24 21:01 codenimble

The error happens because Modern Yarn (2,3) doesn't create yarn.lock when running yarn install if it can find a yarn.lock in it's parent directory.

Before the PR https://github.com/aws-amplify/amplify-cli/pull/13197 gets merged, we can manually do a workaround to create a yarn.lock in the amplify directory. It should solve the problem.

0618 avatar Jan 23 '24 21:01 0618

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.

github-actions[bot] avatar May 01 '24 19:05 github-actions[bot]