Prevent duplicate entries in ctx.include_tops

The include_tops hints are global for the whole parse
context (which is probably also something to fix), which
means that if an include_top hint is duplicated there will
be duplicated keys generated in the _entry starlark dictionary.

Bug: 193566316
Test: go test
Change-Id: I01a0546ac9be91ef46c5248e87e1a40e0f211193
This commit is contained in:
Cole Faust
2021-12-21 14:15:12 -08:00
parent 68542bfcb5
commit f7ed5343de
2 changed files with 41 additions and 0 deletions

View File

@@ -1629,6 +1629,13 @@ func (ctx *parseContext) maybeHandleAnnotation(cnode *mkparser.Comment) {
return
}
if p, ok := maybeTrim(annotation, "include_top"); ok {
// Don't allow duplicate include tops, because then we will generate
// invalid starlark code. (duplicate keys in the _entry dictionary)
for _, top := range ctx.includeTops {
if top == p {
return
}
}
ctx.includeTops = append(ctx.includeTops, p)
return
}

View File

@@ -1075,6 +1075,40 @@ def init(g, handle):
if not _varmod_init:
rblf.mkerror("cannot")
rblf.inherit(handle, _varmod, _varmod_init)
`,
},
{
desc: "Dynamic inherit with duplicated hint",
mkname: "product.mk",
in: `
MY_PATH:=foo
#RBC# include_top vendor/foo1
$(call inherit-product,$(MY_PATH)/cfg.mk)
#RBC# include_top vendor/foo1
$(call inherit-product,$(MY_PATH)/cfg.mk)
`,
expected: `load("//build/make/core:product_config.rbc", "rblf")
load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
def init(g, handle):
cfg = rblf.cfg(handle)
g["MY_PATH"] = "foo"
#RBC# include_top vendor/foo1
_entry = {
"vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
}.get("%s/cfg.mk" % g["MY_PATH"])
(_varmod, _varmod_init) = _entry if _entry else (None, None)
if not _varmod_init:
rblf.mkerror("cannot")
rblf.inherit(handle, _varmod, _varmod_init)
#RBC# include_top vendor/foo1
_entry = {
"vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
}.get("%s/cfg.mk" % g["MY_PATH"])
(_varmod, _varmod_init) = _entry if _entry else (None, None)
if not _varmod_init:
rblf.mkerror("cannot")
rblf.inherit(handle, _varmod, _varmod_init)
`,
},
{