Commit e907192
authored
fix: variable file default discovery for nested tiles (#538)
There's a bug where Kiln `re-bake` wasn't accounting for the relative
tile / Kilnfile path when automatically discovering variable files.
Since `re-bake` relies heavily on convention in typical use, we'd like
to fix this.
### Reproducing the bug
I forked `hello-tile` and modified it to have a nested tile directory
with a single variable `$( variable "hellothere" )` to be interpolated
in its `base.yml`. I created a variables file under
`nest/variables/hellothere.yml` defining its namesake.
It's expected at this point that we need to pass the `--variables-file`
explicitly because `kiln` doesn't know about the nesting yet:
**Creating the record**
```sh
kiln bake --final --version 6 \
--vr artifactory_host=<redacted> \
--vr artifactory_repo=<redacted> \
--vr artifactory_username=<redacted> \
--vr artifactory_password=<redacted> \
--metadata nest/base.yml \
--icon=nest/icon.png \
--kilnfile nest/Kilnfile \
--variables-file nest/variables/hellothere.yml
Setting default credentials from ~/.kiln/credentials.yml. (hint: --variable-file overrides this default. --variable overrides both.)
Warning: The "allow-only-publishable-releases" flag was not set. Some fetched releases may be intended for development/testing only. EXERCISE CAUTION WHEN PUBLISHING A TILE WITH THESE RELEASES!
Gathering releases...
All releases already downloaded
Reading release manifests...
Reading stemcell criteria from Kilnfile.lock
Encoding icon...
Building tile-6.pivotal...
Adding metadata/metadata.yml to tile-6.pivotal...
Creating empty migrations folder in tile-6.pivotal...
Adding releases/hello-release-1.0.5-ubuntu-jammy-1.737.tgz to tile-6.pivotal…
```
This is expected behavior.
Then we find the bug:
**Re-baking the record**
```sh
mv nest/bake_records/6.json /tmp/6.json
mv tile-6.pivotal ~/workspace/tilediff/tile-6-new.zip
kiln re-bake --output-file /tmp/tile-6-rebake.pivotal /tmp/6.json
Setting default credentials from ~/.kiln/credentials.yml. (hint: --variable-file overrides this default. --variable overrides both.)
Warning: The "allow-only-publishable-releases" flag was not set. Some fetched releases may be intended for development/testing only. EXERCISE CAUTION WHEN PUBLISHING A TILE WITH THESE RELEASES!
Gathering releases...
All releases already downloaded
Reading release manifests...
Reading stemcell criteria from Kilnfile.lock
Encoding icon...
2025/03/13 13:45:30 could not execute "re-bake": failed when rendering a template: nest/base.yml:3:10: executing "nest/base.yml" at <variable "hellothere">: error calling variable: could not find variable with key 'hellothere'
```
### The fix
Essentially we're including the nested directory when available during
our variable files search. We define
```go
func getVariablesDir(fs flags.FileSystem, kilnfilePath string) string
```
and then use it during the search:
```go
func getVariablesFilePaths(fs flags.FileSystem, kilnfilePath string) ([]string, error) {
variablesDirPath := getVariablesDir(fs, kilnfilePath)
files, err := fs.ReadDir(variablesDirPath)
...
```
and
```go
func variablesDirPresent(fs flags.FileSystem, kilnfilePath string) bool {
variablesDirPath := getVariablesDir(fs, kilnfilePath)
file, err := fs.Stat(variablesDirPath)
...
```
### Testing the fix
Let's build this branch as `kiln-dev` and then re-run the above commands
in an equivalent way:
**Creating the record**
```sh
kiln-dev bake --final --version 6 \
--vr artifactory_host=<redacted> \
--vr artifactory_repo=<redacted> \
--vr artifactory_username=<redacted> \
--vr artifactory_password=<redacted> \
--metadata nest/base.yml \
--icon=nest/icon.png \
--kilnfile nest/Kilnfile
Setting default credentials from ~/.kiln/credentials.yml. (hint: --variable-file overrides this default. --variable overrides both.)
Warning: The "allow-only-publishable-releases" flag was not set. Some fetched releases may be intended for development/testing only. EXERCISE CAUTION WHEN PUBLISHING A TILE WITH THESE RELEASES!
Gathering releases...
All releases already downloaded
Reading release manifests...
Reading stemcell criteria from Kilnfile.lock
Encoding icon...
Building tile-6.pivotal...
Adding metadata/metadata.yml to tile-6.pivotal...
Creating empty migrations folder in tile-6.pivotal...
Adding releases/hello-release-1.0.5-ubuntu-jammy-1.737.tgz to tile-6.pivotal...
```
**Re-baking the record**
```sh
kiln-dev re-bake --output-file /tmp/tile-6-rebake.pivotal /tmp/6.json
Setting default credentials from ~/.kiln/credentials.yml. (hint: --variable-file overrides this default. --variable overrides both.)
Warning: The "allow-only-publishable-releases" flag was not set. Some fetched releases may be intended for development/testing only. EXERCISE CAUTION WHEN PUBLISHING A TILE WITH THESE RELEASES!
Gathering releases...
All releases already downloaded
Reading release manifests...
Reading stemcell criteria from Kilnfile.lock
Encoding icon...
Building /tmp/tile-6-rebake.pivotal...
Adding metadata/metadata.yml to /tmp/tile-6-rebake.pivotal...
Creating empty migrations folder in /tmp/tile-6-rebake.pivotal...
Adding releases/hello-release-1.0.5-ubuntu-jammy-1.737.tgz to /tmp/tile-6-rebake.pivotal…
```
fixing the issue.2 files changed
Lines changed: 76 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
299 | 299 | | |
300 | 300 | | |
301 | 301 | | |
302 | | - | |
303 | | - | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
304 | 305 | | |
305 | 306 | | |
306 | 307 | | |
307 | | - | |
308 | | - | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
309 | 311 | | |
310 | 312 | | |
311 | 313 | | |
312 | 314 | | |
313 | 315 | | |
314 | 316 | | |
315 | | - | |
| 317 | + | |
316 | 318 | | |
317 | 319 | | |
318 | 320 | | |
319 | 321 | | |
320 | 322 | | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
321 | 335 | | |
322 | 336 | | |
323 | 337 | | |
| |||
338 | 352 | | |
339 | 353 | | |
340 | 354 | | |
341 | | - | |
342 | | - | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
343 | 358 | | |
344 | 359 | | |
345 | 360 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
583 | 583 | | |
584 | 584 | | |
585 | 585 | | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
586 | 639 | | |
587 | 640 | | |
588 | 641 | | |
| |||
606 | 659 | | |
607 | 660 | | |
608 | 661 | | |
609 | | - | |
610 | | - | |
| 662 | + | |
611 | 663 | | |
612 | 664 | | |
613 | 665 | | |
| |||
0 commit comments