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

fix: yarn modern install workspace #13197

Merged
merged 14 commits into from
Mar 27, 2024
7 changes: 7 additions & 0 deletions codebuild_specs/e2e_workflow_generated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,13 @@ batch:
TEST_SUITE: src/__tests__/auth/user-groups-s3-access.test.ts|src/__tests__/auth/user-groups.test.ts|src/__tests__/build-function.test.ts
depend-on:
- upb
- identifier: l_build_function_yarn_modern
buildspec: codebuild_specs/run_e2e_tests_linux.yml
env:
variables:
TEST_SUITE: src/__tests__/build-function-yarn-modern.test.ts
depend-on:
- upb
- identifier: l_custom_resource_with_storage_dynamodb_simulator_export_pull_c
buildspec: codebuild_specs/run_e2e_tests_linux.yml
env:
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-cli-core/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ export interface PackageManager {
// (undocumented)
readonly executable: string;
// (undocumented)
getInstallArgs: (buildType: BuildType) => string[];
getInstallArgs: (buildType: BuildType, resourceDir?: string) => string[];
// (undocumented)
getRunScriptArgs: (scriptName: string) => string[];
// (undocumented)
Expand Down
19 changes: 16 additions & 3 deletions packages/amplify-cli-core/src/utils/packageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface PackageManager {
readonly displayValue: string;
version?: SemVer;
getRunScriptArgs: (scriptName: string) => string[];
getInstallArgs: (buildType: BuildType) => string[];
getInstallArgs: (buildType: BuildType, resourceDir?: string) => string[];
}

class NpmPackageManager implements PackageManager {
Expand All @@ -43,9 +43,22 @@ class YarnPackageManager implements PackageManager {
version?: SemVer;

getRunScriptArgs = (scriptName: string) => [scriptName];
getInstallArgs = (buildType = BuildType.PROD) => {
getInstallArgs = (buildType = BuildType.PROD, resourceDir = '') => {
const useYarnModern = this.version?.major && this.version?.major > 1;
return (useYarnModern ? ['install'] : ['--no-bin-links']).concat(buildType === 'PROD' ? ['--production'] : []);
/**
* Since Yarn 2, resourceDir needs to be treated as a separate project,
* otherwise it'll be hoisted to use the lock.file from the parent directory,
* so we need to create a lock file for it.
* ref: https://github.com/yarnpkg/yarn/issues/5716#issuecomment-817330338
*/
if (useYarnModern) {
if (fs.existsSync(`${resourceDir}/${this.lockFile}`)) {
console.log(`${resourceDir}/${this.lockFile} already exists.`);
} else {
fs.writeFileSync(`${resourceDir}/${this.lockFile}`, '');
}
}
return (useYarnModern ? ['workspaces', 'focus'] : ['--no-bin-links']).concat(buildType === 'PROD' ? ['--production'] : []);
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import execa from 'execa';
import {
addFunction,
createNewProjectDir,
deleteProject,
deleteProjectDir,
functionBuild,
initJSProjectWithProfile,
} from '@aws-amplify/amplify-e2e-core';

describe('build successfully via Yarn Modern', () => {
let projRoot: string;

beforeEach(async () => {
projRoot = await createNewProjectDir('analytics');
await execa('corepack', ['enable'], { cwd: projRoot });
await execa('yarn', ['init', '-2'], { cwd: projRoot });
await execa('yarn', ['--version'], { cwd: projRoot });
});

afterEach(async () => {
await deleteProject(projRoot);
deleteProjectDir(projRoot);
});

it('add analytics and function', async () => {
await initJSProjectWithProfile(projRoot, {});
await addFunction(
projRoot,
{
functionTemplate: 'Hello World',
},
'nodejs',
);
await functionBuild(projRoot);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const runPackageManagerInstall = async (resourceDir: string, buildType: BuildTyp
return;
}

const args = packageManager.getInstallArgs(buildType);
const args = packageManager.getInstallArgs(buildType, resourceDir);
await runPackageManager(packageManager, args, resourceDir);
};

Expand Down
48 changes: 24 additions & 24 deletions shared-scripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function loadCache {
# create directory if it doesn't exist yet
mkdir -p $localPath
# check if cache exists in s3
if ! aws s3 ls $s3Path > /dev/null; then
if ! aws s3 ls $s3Path >/dev/null; then
echo "Cache not found."
exit 0
fi
Expand All @@ -57,7 +57,7 @@ function loadCacheFile {
s3Path="s3://$CACHE_BUCKET_NAME/$CODEBUILD_SOURCE_VERSION/$alias"
echo loading cache file from $s3Path
# check if cache file exists in s3
if ! aws s3 ls $s3Path > /dev/null; then
if ! aws s3 ls $s3Path >/dev/null; then
echo "Cache file not found."
exit 0
fi
Expand Down Expand Up @@ -90,6 +90,7 @@ function _installAndCacheDependencies {
storeCache $CODEBUILD_SRC_DIR repo
storeCache $HOME/.cache .cache
}

function _buildLinux {
echo Linux Build
yarn --immutable
Expand All @@ -115,8 +116,8 @@ function _validateRollbackTargetVersion {
loadCache repo $CODEBUILD_SRC_DIR
loadCache .cache $HOME/.cache
if [ -z "$ROLLBACK_TARGET_VERSION" ]; then
echo "Rollback target version is missing. Make sure CodeBuild workflow was started with ROLLBACK_TARGET_VERSION environment variable"
exit 1
echo "Rollback target version is missing. Make sure CodeBuild workflow was started with ROLLBACK_TARGET_VERSION environment variable"
exit 1
fi
yarn ts-node scripts/verify-deployment.ts -v $ROLLBACK_TARGET_VERSION
}
Expand Down Expand Up @@ -154,28 +155,28 @@ function _verifyGeneratedE2EWorkflow {
loadCache .cache $HOME/.cache

# backup current file and remove regions, they are not deterministic
cat codebuild_specs/e2e_workflow_generated.yml | grep -v "CLI_REGION:" > codebuild_specs/e2e_workflow_generated.yml.old.trimmed
cat codebuild_specs/e2e_workflow_generated.yml | grep -v "CLI_REGION:" >codebuild_specs/e2e_workflow_generated.yml.old.trimmed

# regenerate e2e workflow
yarn split-e2e-tests-codebuild

# remove regions from generated file, they are not deterministic
cat codebuild_specs/e2e_workflow_generated.yml | grep -v "CLI_REGION:" > codebuild_specs/e2e_workflow_generated.yml.trimmed
cat codebuild_specs/e2e_workflow_generated.yml | grep -v "CLI_REGION:" >codebuild_specs/e2e_workflow_generated.yml.trimmed

changed_lines_in_e2e_workflow_generated=$(diff codebuild_specs/e2e_workflow_generated.yml.old.trimmed codebuild_specs/e2e_workflow_generated.yml.trimmed | wc -l)

if [[ changed_lines_in_e2e_workflow_generated -gt 0 ]]; then
echo "Fail! An uncommitted drift in E2E workflow has been detected - e2e_workflow_generated.yml. Please run 'yarn split-e2e-tests-codebuild' and commit the result."
diff codebuild_specs/e2e_workflow_generated.yml.old.trimmed codebuild_specs/e2e_workflow_generated.yml.trimmed
exit 1;
echo "Fail! An uncommitted drift in E2E workflow has been detected - e2e_workflow_generated.yml. Please run 'yarn split-e2e-tests-codebuild' and commit the result."
diff codebuild_specs/e2e_workflow_generated.yml.old.trimmed codebuild_specs/e2e_workflow_generated.yml.trimmed
exit 1
fi

# check if wait_for_ids.json changed.
changed_wait_for_ids_manifest=$(git status | grep -F wait_for_ids.json | wc -l)

if [[ changed_wait_for_ids_manifest -gt 0 ]]; then
echo "Fail! An uncommitted drift in E2E workflow has been detected - wait_for_ids.json. Please run 'yarn split-e2e-tests-codebuild' and commit the result."
exit 1;
echo "Fail! An uncommitted drift in E2E workflow has been detected - wait_for_ids.json. Please run 'yarn split-e2e-tests-codebuild' and commit the result."
exit 1
fi

echo "Success! No drift detected in E2E workflow."
Expand Down Expand Up @@ -231,7 +232,7 @@ function _publishToLocalRegistry {
cat UNIFIED_CHANGELOG.md

echo Save new amplify Github tag
node scripts/echo-current-cli-version.js > .amplify-pkg-version
node scripts/echo-current-cli-version.js >.amplify-pkg-version

echo LS HOME
ls $CODEBUILD_SRC_DIR/..
Expand Down Expand Up @@ -306,8 +307,7 @@ function _convertCoverage {
}
# https://docs.codecov.com/docs/codecov-uploader#integrity-checking-the-uploader
function _uploadCoverageLinux {
if [ -z ${CODECOV_TOKEN+x} ]
then
if [ -z ${CODECOV_TOKEN+x} ]; then
echo "CODECOV_TOKEN not set: No coverage will be uploaded."
else
curl https://keybase.io/codecovsecurity/pgp_keys.asc | gpg --no-default-keyring --keyring trustedkeys.gpg --import # One-time step
Expand Down Expand Up @@ -564,7 +564,8 @@ function _integrationTest {

echo "running auth server in background"
export NODE_OPTIONS=--openssl-legacy-provider
nohup yarn start > server_output.txt & disown $!
nohup yarn start >server_output.txt &
disown $!
echo "Polling for server ready message"
while ! grep -Fxq "You can now view aws-amplify-cypress-auth in the browser." server_output.txt; do echo "Waiting for server to start" && sleep 1; done
echo "server started"
Expand All @@ -584,7 +585,6 @@ function _integrationTest {
expect ../amplify-cli/codebuild_specs/exp-files/delete.exp
aws s3 rb "$DEPLOYMENT_BUCKET" --force


echo "Clone API test package"
cd .. && pwd
git clone $API_CLONE_URL
Expand All @@ -605,7 +605,8 @@ function _integrationTest {

echo "running api server in background"
export NODE_OPTIONS=--openssl-legacy-provider
nohup yarn start > server_output.txt & disown $!
nohup yarn start >server_output.txt &
disown $!
echo "Polling for server ready message"
while ! grep -Fxq "You can now view aws-amplify-cypress-api in the browser." server_output.txt; do echo "Waiting for server to start" && sleep 1; done
echo "server started"
Expand Down Expand Up @@ -714,20 +715,20 @@ function _publishToNpm {

./out/amplify-pkg-linux-x64 --version
echo Authenticate with npm
echo "//registry.npmjs.org/:_authToken=$NPM_PUBLISH_TOKEN" > ~/.npmrc
echo "//registry.npmjs.org/:_authToken=$NPM_PUBLISH_TOKEN" >~/.npmrc

source ./.circleci/cb-publish-step-3-npm.sh
}
function _rollbackNpm {
loadCache repo $CODEBUILD_SRC_DIR

if [ -z "$ROLLBACK_TARGET_VERSION" ]; then
echo "Rollback target version is missing. Make sure CodeBuild workflow was started with ROLLBACK_TARGET_VERSION environment variable"
exit 1
echo "Rollback target version is missing. Make sure CodeBuild workflow was started with ROLLBACK_TARGET_VERSION environment variable"
exit 1
fi

echo Authenticate with npm
echo "//registry.npmjs.org/:_authToken=$NPM_PUBLISH_TOKEN" > ~/.npmrc
echo "//registry.npmjs.org/:_authToken=$NPM_PUBLISH_TOKEN" >~/.npmrc

npm dist-tag add @aws-amplify/cli@$ROLLBACK_TARGET_VERSION "latest"
}
Expand All @@ -750,8 +751,8 @@ function _githubRollback {
loadCache repo $CODEBUILD_SRC_DIR
echo Rollback Amplify CLI GitHub release
if [ -z "$ROLLBACK_TARGET_VERSION" ]; then
echo "Rollback target version is missing. Make sure CodeBuild workflow was started with ROLLBACK_TARGET_VERSION environment variable"
exit 1
echo "Rollback target version is missing. Make sure CodeBuild workflow was started with ROLLBACK_TARGET_VERSION environment variable"
exit 1
fi
yarn ts-node scripts/github-rollback.ts $ROLLBACK_TARGET_VERSION
}
Expand All @@ -768,7 +769,6 @@ function _amplifyGeneralConfigTests {
retry yarn general-config-e2e --no-cache --maxWorkers=3 --forceExit $TEST_SUITE
}


function _cleanUpResources {
_loadTestAccountCredentials
echo "Executing resource cleanup"
Expand Down
Loading