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

feat(cli): added build field to cdk.json #17176

Merged
merged 19 commits into from Nov 4, 2021
Merged

feat(cli): added build field to cdk.json #17176

merged 19 commits into from Nov 4, 2021

Conversation

comcalvi
Copy link
Contributor

@comcalvi comcalvi commented Oct 27, 2021

Adds a build field to cdk.json. The command specified in the build will be executed before synthesis. This can be used to build any code that needs to be built before synthesis (for example, CDK App code or Lambda Function code).

This is part of the changes needed for the cdk watch command
(aws/aws-cdk-rfcs#383).


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link

gitpod-io bot commented Oct 27, 2021

@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Oct 27, 2021
@comcalvi comcalvi changed the title feat (cli): added build field to cdk.json feat(cli): added build field to cdk.json Oct 27, 2021
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Oct 27, 2021
Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Some minor details.

Can you also add a PR description stating what is this change accomplishing, and why is it being added?

packages/aws-cdk/README.md Outdated Show resolved Hide resolved
packages/aws-cdk/lib/api/cxapp/exec.ts Outdated Show resolved Hide resolved
packages/aws-cdk/test/api/exec.test.ts Outdated Show resolved Hide resolved
packages/aws-cdk/lib/api/cxapp/exec.ts Outdated Show resolved Hide resolved
packages/aws-cdk/lib/api/cxapp/exec.ts Outdated Show resolved Hide resolved
@skinny85
Copy link
Contributor

@eladb can you also take a look? Thanks!

@skinny85 skinny85 assigned comcalvi and unassigned rix0rrr Oct 27, 2021
@skinny85 skinny85 requested a review from eladb October 27, 2021 00:22
@skinny85 skinny85 added the pr/do-not-merge This PR should not be merged at this time. label Oct 28, 2021
packages/aws-cdk/README.md Outdated Show resolved Hide resolved
packages/aws-cdk/test/api/exec.test.ts Show resolved Hide resolved
@@ -46,6 +46,11 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
debug('context:', context);
env[cxapi.CONTEXT_ENV] = JSON.stringify(context);

const build = config.settings.get(['build']);
if (build) {
await exec([build]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if "build" is set to something like "mvn package"? How does this split into program and args?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exec() now splits its argument into command and args before spawning the process.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like exec() treats the first item in the array as the command but this is not true in the case I described above.

Can you add a test to verify ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the split doesn't happen in the case of the build key. It winds up working because the whole command just get put into command and spawn() happily accepts it.

@@ -103,7 +108,9 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
// anyway, and if the subprocess is printing to it for debugging purposes the
// user gets to see it sooner. Plus, capturing doesn't interact nicely with some
// processes like Maven.
const proc = childProcess.spawn(commandLine[0], commandLine.slice(1), {
const command = commandAndArgs[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if commandAndArgs[0] is mvn package

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we just need to accept a single string and pass it down to spawn(). Since you use shell:true this should just work without splitting to arguments imho

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to split the arguments because of the other usage of exec(), namely this line:

await exec(commandLine);

commandLine is defined by

const commandLine = await guessExecutable(appToArray(app));

guessExecutable() needs a string[], not a string. Do you want me to rework this so that we don't need to have any string[]s passed to exec(), and instead make exec() take just a string? exec() previously operated on a string[].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eladb if commandAndArgs[0] is mvn package then spawn() will still correctly start the process.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit messy but if this works as is i am okay with that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eladb just made this cleaner, now mvn package will be split into ["mvn", "package"] instead of the previous ["mvn package"]. Would appreciate another pass on the PR!

@comcalvi comcalvi requested a review from eladb November 3, 2021 18:47
@@ -57,7 +62,7 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
return createAssembly(app);
}

const commandLine = await guessExecutable(appToArray(app));
const commandLine = await guessExecutable(commandToArray(app));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is better TBH...

What happens if the command has quotes in it?

For example "mvn package"?

Bottom line, I think we should simply spawn this command without splitting it into arguments and with shell:true. There's a variant of spawn I believe that just accepts a single string and passes it to the shell.

There are also intricacies related to Windows/POSIX here that can blow up in 5,000 ways, so I rather we avoid any parsing of the command line if possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eladb just to be clear - you also want to change how we handle the "app" key, right? And no longer do any splitting there?

Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small comment.

packages/aws-cdk/lib/api/cxapp/exec.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@skinny85 skinny85 removed the pr/do-not-merge This PR should not be merged at this time. label Nov 4, 2021
@mergify
Copy link
Contributor

mergify bot commented Nov 4, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: f64725f
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 57ad1e0 into aws:master Nov 4, 2021
@mergify
Copy link
Contributor

mergify bot commented Nov 4, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
Adds a `build` field to `cdk.json`. The command specified in the `build` will be executed before synthesis. This can be used to build any code that needs to be built before synthesis (for example, CDK App code or Lambda Function code).

This is part of the changes needed for the `cdk watch` command
(aws/aws-cdk-rfcs#383).

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS. package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants