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

Amplify CLI not working with Yarn 4

Open jgo80 opened this issue 2 years ago • 10 comments

How did you install the Amplify CLI?

curl

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

v18.18.2

Amplify CLI Version

12.7.1

What operating system are you using?

Mac

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

No manual changes made

Describe the bug

When deploying a Lambda Function in a Project based on Yarn 4, the command amplify push crashes as follows:

% amplify push
✔ Successfully pulled backend environment dev from the cloud.

    Current Environment: dev
    
┌──────────┬───────────────┬───────────┬───────────────────┐
│ Category │ Resource name │ Operation │ Provider plugin   │
├──────────┼───────────────┼───────────┼───────────────────┤
│ Function │ ay47b3e7244   │ Create    │ awscloudformation │
└──────────┴───────────────┴───────────┴───────────────────┘
✔ Are you sure you want to continue? (Y/n) · yes
🛑 Packaging lambda function failed with the error 
Command failed with exit code 1: yarn install --production
➤ YN0050: The --production option is deprecated on 'install'; use 'yarn workspaces focus' instead

Learn more at: https://docs.amplify.aws/cli/project/troubleshooting/

Session Identifier: 7282a548-35f7-42e4-b29d-19df116b78e0

Which is caused most likely through this code in amplify-cli-core/src/utils/packageManager.ts:

class YarnPackageManager implements PackageManager {
  readonly packageManager: PackageManagerType = 'yarn';
  readonly displayValue = 'Yarn';
  readonly executable = 'yarn';
  readonly lockFile = 'yarn.lock';
  version?: SemVer;

  getRunScriptArgs = (scriptName: string) => [scriptName];
  getInstallArgs = (buildType = BuildType.PROD) => {
    const useYarnModern = this.version?.major && this.version?.major > 1;
    return (useYarnModern ? ['install'] : ['--no-bin-links']).concat(buildType === 'PROD' ? ['--production'] : []);
  };
}

It should be changed to invoke yarn workspaces focus

Expected behavior

Deploy lambdas correctly using Yarn 4

Reproduction steps

% mkdir amplify-with-yarn4
% cd amplify-with-yarn4
% corepack enable
% yarn init -2
% amplify init # just init an amplify project
% amplify add function # e.g. Hello World Lambda
% amplify push

Project Identifier

Session Identifier: 7282a548-35f7-42e4-b29d-19df116b78e0

Log output

# Put your logs below this line
2023-10-28T12:01:15.566Z|info : amplify push core  
2023-10-28T12:01:15.641Z|info : @aws-amplify/amplify-cli-core.banner-message/index.ts.fetch banner messages from https://aws-amplify.github.io/amplify-cli/banner-message.json({}
2023-10-28T12:01:16.727Z|info : amplify-provider-awscloudformation.system-config-manager.getProfileConfig(["MY_CREDENTIALS"])
2023-10-28T12:01:16.729Z|info : amplify-provider-awscloudformation.system-config-manager.getProfiledAwsConfig.profileConfig([{"region":"eu-central-1"}])
2023-10-28T12:01:16.729Z|info : amplify-provider-awscloudformation.system-config-manager.getProfileCredentials(["MY_CREDENTIALS"])
2023-10-28T12:01:16.736Z|info : amplify-provider-awscloudformation.zip-util.downloadZip.s3.getFile([{"Key":"[***]rent-[***]ud-[***]d.zip"},null])
2023-10-28T12:01:16.737Z|info : amplify-provider-awscloudformation.aws-s3.s3.getFile([{"Key":"[***]rent-[***]ud-[***]d.zip","Bucket":"[***]ify-[***]y4-[***]ev-[***]616-[***]ment"}])
2023-10-28T12:01:16.930Z|info : amplify-provider-awscloudformation.initialize-env.run.cfn.updateamplifyMetaFileWithStackOutputs([{"StackName":"[***]ify-[***]y4-[***]ev-[***]616"}])
2023-10-28T12:01:16.933Z|info : amplify-provider-awscloudformation.system-config-manager.getProfileConfig(["MY_CREDENTIALS"])
2023-10-28T12:01:16.934Z|info : amplify-provider-awscloudformation.system-config-manager.getProfiledAwsConfig.profileConfig([{"region":"eu-central-1"}])
2023-10-28T12:01:16.934Z|info : amplify-provider-awscloudformation.system-config-manager.getProfileCredentials(["MY_CREDENTIALS"])
2023-10-28T12:01:16.940Z|info : amplify-provider-awscloudformation.aws-cfn.updateamplifyMetaFileWithStackOutputs.cfn.listStackResources([{"StackName":"[***]ify-[***]y4-[***]ev-[***]616"}])
2023-10-28T12:01:17.111Z|info : amplify-provider-awscloudformation.aws-cfn.describeStack.cfn.describeStacks([{"StackName":"[***]ify-[***]y4-[***]ev-[***]616"}])
2023-10-28T12:01:17.256Z|info : amplify-provider-awscloudformation.aws-s3.s3.getFile([{"Key":"[***]ify-[***]json","Bucket":"[***]ify-[***]y4-[***]ev-[***]616-[***]ment"}])
2023-10-28T12:01:17.403Z|info : amplify-provider-awscloudformation.system-config-manager.getProfileConfig(["MY_CREDENTIALS"])
2023-10-28T12:01:17.404Z|info : amplify-provider-awscloudformation.system-config-manager.getProfiledAwsConfig.profileConfig([{"region":"eu-central-1"}])
2023-10-28T12:01:17.404Z|info : amplify-provider-awscloudformation.system-config-manager.getProfileCredentials(["MY_CREDENTIALS"])
2023-10-28T12:01:18.688Z|error : Packaging lambda function failed with the error 
Command failed with exit code 1: yarn install --production
➤ YN0050: The --production option is deprecated on 'install'; use 'yarn workspaces focus' instead
PackagingLambdaFunctionError: Packaging lambda function failed with the error 
Command failed with exit code 1: yarn install --production
➤ YN0050: The --production option is deprecated on 'install'; use 'yarn workspaces focus' instead


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.

jgo80 avatar Oct 28 '23 12:10 jgo80

Hey @jgo80 :wave: thanks for raising this! This looks similar to https://github.com/aws-amplify/amplify-cli/issues/13001, but I'd like to keep this open and tracked separately for the new yarn version

josefaidt avatar Oct 30 '23 15:10 josefaidt

I also am getting this with a yarn berry (modern) project, I haven't added a new lambda function in a while but after performing the add function, I am unable to do the following:

  • amplify build function
  • amplify push --y

I can get amplify to build if I revert to yarn classic, and then do the amplify operations, and then set yarn back to berry.

This is a major issue.

Screenshot 2024-01-19 at 1 01 06 PM

codenimble avatar Jan 19 '24 21:01 codenimble

FWIW As a workaround while we wait to get amplify updated to work with yarn modern. I created my own yarn script that works around this limitation.

#!/usr/bin/env node
let args = process.argv.slice(2)
if (args.length == 2 && args[0] === 'install' && args[1] === '--production') {
  args = [ 'workspaces', 'focus', '--all', '--production' ]
  console.log("YARN ARGS TRANSFORMED:", process.argv.slice(2), args)
}
require('./lib/corepack.cjs').runMain(['yarn', ...args]);

It simply checks to see if yarn is being invoked with the deprecated install --production arguments and if so replaces with the required workspaces focus --all --production. The function src directory I believe needs a yarn.lock so it doesn't go looking upward to the parent directories for one, just do a touch.

codenimble avatar Jan 20 '24 02:01 codenimble

I am also encountering this issue on Linux, Amplify v12.10.1 Yarn v4.1.0 node v20.11.0

Spotchi avatar Feb 16 '24 11:02 Spotchi

We've got Yarn 4 with Yarn workspaces working on Amplify CLI 12.10.1. It turns out the workaround is pretty simple: you can override the build script in the amplify.state file in the function directory. It looks like the default build script (via getInstallArgs() uses yarn --no-bin-links --production, which isn't compatible with Yarn 2+. The fix is to simply override the build script in the amplify.state as follows:

{
  "pluginId": "amplify-nodejs-function-runtime-provider",
  "functionRuntime": "nodejs",
  "useLegacyBuild": true,
  "defaultEditorFile": "src/index.js",
  "scripts": {
    "build": "yarn"
  }
}

The build script provided here overrides what getInstallArgs() would otherwise default to, as you can see in legacyBuild.ts here:

https://github.com/aws-amplify/amplify-cli/blob/dev/packages/amplify-nodejs-function-runtime-provider/src/utils/legacyBuild.ts#L16

With this small tweak, our Yarn 4 functions can be successfully deployed with amplify push.

I couldn't find anything about this in the documentation, but I happened to stumble upon the setting when adding a new function this week. It looks like the default amplify.state file now includes the build script by default (with a value of yarn --no-bin-links --production as the default), which tipped me off to the easy solution.

brianlenz avatar Feb 16 '24 13:02 brianlenz

The trick to change the script to "build": "yarn" did not work for me unfortunately. In particular, my TypeScript Lambdas would show an error upon running amplify push.

chrisl777 avatar Apr 21 '24 01:04 chrisl777

When I use yarn 4, the CI/CD fails for my TypeScript Lambda:

✖ There was an error initializing your environment.

2024-06-21T01:05:19.595Z [INFO]: 🛑 Packaging lambda function failed with the error

Command failed with exit code 1

chrisl777 avatar Jun 21 '24 01:06 chrisl777

Try this, this worked for me

amplify.yml

version: 1
applications:
  - frontend:
      phases:
        preBuild:
          commands:
            - corepack enable
            - yarn set version stable
            - yarn install --frozen-lockfile
        build:
          commands:
            - yarn run build
      artifacts:
        # IMPORTANT - Please verify your build output directory
        baseDirectory: build
        files:
          - "**/*"
      cache:
        paths:
          - node_modules/**/*

abura1han avatar Jul 08 '24 02:07 abura1han