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

Amplify CLI not working with Yarn 4 #13377

Open
2 tasks done
jgo80 opened this issue Oct 28, 2023 · 8 comments
Open
2 tasks done

Amplify CLI not working with Yarn 4 #13377

jgo80 opened this issue Oct 28, 2023 · 8 comments
Labels
bug Something isn't working functions Issues tied to the functions category p3

Comments

@jgo80
Copy link

jgo80 commented Oct 28, 2023

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:

  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.
@jgo80 jgo80 added the pending-triage Issue is pending triage label Oct 28, 2023
@josefaidt
Copy link
Contributor

Hey @jgo80 👋 thanks for raising this! This looks similar to #13001, but I'd like to keep this open and tracked separately for the new yarn version

@josefaidt josefaidt added bug Something isn't working functions Issues tied to the functions category and removed pending-triage Issue is pending triage labels Oct 30, 2023
@ykethan ykethan added the p3 label Nov 2, 2023
@codenimble
Copy link

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
Copy link

codenimble commented Jan 20, 2024

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.

@Spotchi
Copy link

Spotchi commented Feb 16, 2024

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

@brianlenz
Copy link
Contributor

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.

@chrisl777
Copy link

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
Copy link

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

@abura1han
Copy link

abura1han commented Jul 8, 2024

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/**/*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working functions Issues tied to the functions category p3
Projects
None yet
Development

No branches or pull requests

8 participants