Skip to content

feat(builtin): support for substitutions - #1664

Merged
alexeagle merged 3 commits into
bazel-contrib:stablefrom
thesayyn:master
Aug 31, 2020
Merged

alexeagle merged 3 commits into
bazel-contrib:stablefrom
thesayyn:master

Conversation

@thesayyn

Copy link
Copy Markdown
Collaborator

PR Checklist

Please check if your PR fulfills the following requirements:

  • [ X] Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • [ X ] Feature (please, look at the "Scope of the project" section in the README.md file)
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #1621

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • [ X ] No

Other information

@thesayyn

Copy link
Copy Markdown
Collaborator Author

//internal/pkg_web/test3:test is failing. I'm gonna fix it asap.

Comment thread internal/pkg_web/pkg_web.bzl Outdated

@alexeagle alexeagle left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for contributing!

Comment thread internal/pkg_web/test3/BUILD.bazel Outdated
@@ -0,0 +1,26 @@
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_web")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

"test3" isn't a descriptive name, can you make it "substitutions" or similar?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done ✅

Comment thread internal/pkg_web/test3/spec.js Outdated
describe('pkg_web', () => {
it('should match the golden file', () => {
const output = 'build_bazel_rules_nodejs/internal/pkg_web/test/pkg/index.html';
const golden = 'build_bazel_rules_nodejs/internal/pkg_web/test/index_golden.html_';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

use the golden_file_test rule instead

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done ✅

Comment thread internal/pkg_web/pkg_web.bzl Outdated
www_dir = action_factory.declare_directory(output_name)
args = action_factory.args()
args.add(www_dir.path)
args.add(version_file.path)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this needs some conditional logic because the version_file isn't always present?

I think we need to detect use of --stamp like pkg_npm does stamp = ctx.attr.node_context_data[NodeContextInfo].stamp

@thesayyn thesayyn Feb 28, 2020 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

as far as I know, the version file is always present but the content changes regarding to the --workspace_status_command. Since we have a logic in bundler which checks for the presence of the keys, we'll be okay I guess.

@thesayyn thesayyn Mar 22, 2020 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As far as I understand the rule has to obey the --stamp parameter which is stored in the node_context helper rule. I should make this work that way.

Comment thread internal/pkg_web/assembler_spec.js Outdated

it('should replace contents with dynamic text from volatile file', () => {
assembler.main(
[outdir, volatilePath, '{"content":"{TEST_KEY}"}', '--assets', 'path/to/thing1.txt']);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we should make the API match with pkg_npm. Over there we don't let you grab arbitrary keys from the version file, instead we know the key that indicates the version and the replace_with_version attribute which says what placeholder is used.

I suspect you prefer this more general implementation? but I think it's more important to be consistent.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes. I think we should permit the users to replace the contents with the arbitrary keys from the version file. It would be more useful this way. Since we have a key check in bundler, the user would get an error if the key doesn't exist in the version file.

@thesayyn thesayyn Mar 20, 2020 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

In addition, this approach is more general and gives you the ability to replace "placeholder texts" with arbitrary keys from the version file.

For instance, In one of our projects, we are replacing "BUILD_SCM_HASH" with the real hash along with the "BUILD_SCM_VERSION" key and it is not possible to accomplish the same thing with the pkg_npm rule.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Perhaps provide both APIs? The replace_with_version attr on pkg_web would provide a consistent API surface for both rules, and pkg_npm would change to support substitutions from workspace_status?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

update, I'm deprecating replace_with_version so let's just do it your way :)

Comment thread internal/pkg_web/assembler.js Outdated
if (substituteWith.match(/^{.*?}$/)) {
substituteWith = substituteWith.replace(/^{(.*?)}$/, '$1');
if (!stampMap[substituteWith]) {
throw new Error(`Could not find ${substituteWith} key in volatile-status file.`);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not sure about throwing this error here - if the user is replacing 0.0.0-PLACEHOLDER as the version string for regular builds with a key from --workspace_status_command which is tied to --stamp, this will throw when not running a stamped build

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

When I tie this with the version file from node context, which only appears when --stamp option is present, this would be long gone. So I thought it would be better if we explicitly check for the keys when the user trying to stamp builds with --stamp option present.

Comment thread internal/node/_node_bin/node Outdated
@@ -0,0 +1,9 @@
#!/bin/bash

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Revert this file?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This happened automatically when I was trying to commit the changes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sure, I'll try to revert it.

Comment thread internal/pkg_web/assembler_spec.js Outdated

it('should replace contents with dynamic text from volatile file', () => {
assembler.main(
[outdir, volatilePath, '{"content":"{TEST_KEY}"}', '--assets', 'path/to/thing1.txt']);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Perhaps provide both APIs? The replace_with_version attr on pkg_web would provide a consistent API surface for both rules, and pkg_npm would change to support substitutions from workspace_status?

"index.js",
],
substitutions = {
"THIS_SHOULD_BE_REPLACED": "THIS_SHOULD_BE_REPLACED_WITH_THIS",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: I know this is a substitutions, but at first I didn't see the different between the strings after the build as only the suffix changed 😄
Perhaps provide a real world example of a replacement here, as this is mostly centered around version replacement.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I agree, a bit confusing. we can add an example that replaces with an arbitrary key from the volatile file therefore we would have tests and examples for both use cases.

@alexeagle

Copy link
Copy Markdown
Collaborator

Hey I'm finally picking up stamping again, I'll get this merged shortly...

@thesayyn

Copy link
Copy Markdown
Collaborator Author

I had no time to work on this and there is still some work to do. Maybe I could get them done by Sunday.

@alexeagle

Copy link
Copy Markdown
Collaborator

no worries @thesayyn I have time today and want to make this consistent with other stamping changes I just landed. I'll push a fixup commit

@alexeagle
alexeagle changed the base branch from master to stable August 31, 2020 20:53
@googlebot

Copy link
Copy Markdown

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@alexeagle

Copy link
Copy Markdown
Collaborator

@googlebot I consent.

@googlebot

Copy link
Copy Markdown

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@alexeagle
alexeagle merged commit 421280a into bazel-contrib:stable Aug 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants