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

Stale transition value #13997

Closed
nikhilkalige opened this issue Sep 15, 2021 · 6 comments
Closed

Stale transition value #13997

nikhilkalige opened this issue Sep 15, 2021 · 6 comments
Assignees
Labels
P1 I'll work on this now. (Assignee required) team-Configurability platforms, toolchains, cquery, select(), config transitions type: bug

Comments

@nikhilkalige
Copy link
Contributor

I have following code that I am using to create a transition. It transitions on //hal/mach:mach build setting as defined below

string_flag(
    name = "mach",
    build_setting_default = "",
    values = ["x86", "arm"],
    visibility = ["//visibility:public"],
)

def _mach_transition_impl(settings, attr):
    print(attr.name, attr.mach)
    return {"//hal/mach:mach": attr.mach}

_mach_transition = transition(
    implementation = _mach_transition_impl,
    inputs = [],
    outputs = ["//hal/mach:mach"],
)

def _create_image_impl(ctx):
    inputs = []
    content_map = {}
    for app in ctx.attr.applications:
        if DefaultInfo in app:
            inputs.extend(app[DefaultInfo].files.to_list())
        for f in app.files.to_list():
            inputs.append(f)

    manifest_file = ctx.actions.declare_file(ctx.label.name + ".manifest")
    inputs.append(manifest_file)
    output = [file.path for file in inputs]
    ctx.actions.write(
        manifest_file,
        "\n".join(output),
    )
    return DefaultInfo(files = depset(inputs))

_create_image = rule(
    implementation = _create_image_impl,
    cfg = _mach_transition,
    attrs = {
        "applications": attr.label_list(mandatory = True), # , cfg = _mach_transition),
        "mach": attr.string(mandatory = True),
        "_allowlist_function_transition": attr.label(
            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
        ),
    },
)

I create the following using the above

image.create_image(
    name = "pc",
    mach = "x86",
    applications = [ ... ]
)

image.create_image(
    name = "arm",
    mach = "arm",
    applications = [ ... ]
)

When I change the mach to a invalid value, say armX, I get an error, but if I change it again to armY, I still get an error saying the armX is invalid.

With armX
Error in fail: Error setting //hal/mach:mach: invalid value 'armX'. Allowed values are ["x86", "arm"]

With armY
Error in fail: Error setting //hal/mach:mach: invalid value 'armX'. Allowed values are ["x86", "arm"]

I don't understand why I am still getting stale values.

I have a minimal example here https://github.com/nikhilkalige/bazel-select-impl-test , but this does not seem to suffer the same problem, where as my larger codebase fails to pick up the changes to files.

I am only seeing this error with newer version of bazel, I tried it out 5 post release (1a7d524) and one more slightly older version too.

@philwo philwo added team-Configurability platforms, toolchains, cquery, select(), config transitions untriaged labels Sep 21, 2021
@gregestren
Copy link
Contributor

Interesting report. I'll give it a look as soon as I can...

@gregestren gregestren self-assigned this Oct 1, 2021
@gregestren
Copy link
Contributor

Thanks. I confirmed this.

It's cache key imprecision when trying to cache transition execution:

CacheKey(StarlarkDefinedConfigTransition starlarkDefinedConfigTransition, Rule rule) {
this.starlarkDefinedConfigTransition = starlarkDefinedConfigTransition;
this.ruleLabel = rule.getLabel();
this.hashCode = Objects.hash(starlarkDefinedConfigTransition, ruleLabel);
}

I think this is as simple as storing the full rule instead of rule.getLabel() - key point being that also takes into account the target's attributes, so when you change mach = "armX" to mach = "armY" that change invalidates the cache.

The point of the cache was to avoid redundant execution of expensive transitions. I'd want to do a few quick performance tests to check that this fix doesn't slow anything down (I don't think it will).

Thanks for catching this!

@gregestren gregestren added P1 I'll work on this now. (Assignee required) type: bug and removed untriaged labels Oct 5, 2021
@gregestren
Copy link
Contributor

FYI I have a pending fix for this at https://bazel-review.googlesource.com/c/bazel/+/178498. I just want to check it doesn't introduce performance regressions before submitting.

@illicitonion
Copy link
Contributor

@bazel-io fork 5.2

@brentleyjones
Copy link
Contributor

@bazel-io flag

@bazel-io bazel-io added the potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone label Apr 9, 2022
@ckolli5
Copy link

ckolli5 commented Apr 13, 2022

@bazel-io fork 5.2.0

@bazel-io bazel-io removed the potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone label Apr 13, 2022
brentleyjones pushed a commit to brentleyjones/bazel that referenced this issue May 5, 2022
…s change.

Fixes bazelbuild#13997

PiperOrigin-RevId: 406886706
(cherry picked from commit fe644be)
ckolli5 pushed a commit that referenced this issue May 9, 2022
…s change. (#15404)

Fixes #13997

PiperOrigin-RevId: 406886706
(cherry picked from commit fe644be)

Co-authored-by: gregce <gregce@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1 I'll work on this now. (Assignee required) team-Configurability platforms, toolchains, cquery, select(), config transitions type: bug
Projects
None yet
Development

No branches or pull requests

7 participants