[BUG] amend_golem_config() drops `!expr` tags after the first expression of a section

Open Beginner friendly
#1,256 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
r
Domain
tooling

Research direction

The tagging happens in add_expr_tag() in R/golem-yaml-utils.R, which sets the !expr attribute only on tag[[1]]; find_and_tag_exprs() in the same file decides which values get tagged. Run the reproduction from the issue and check inst/golem-config.yml afterwards, then confirm that every expression in a section keeps !expr after amend_golem_config(). Done means a test with two or more expressions in one section passes and the written file still has the tags.

Written by the indexing model from the issue text.

Description

bug

Describe the bug

amend_golem_config() (and therefore set_golem_name(), set_golem_version(), set_golem_wd() and fill_desc()) silently turns !expr values into plain strings when a config section contains more than one expression. This is a partial regression of #709.

add_expr_tag() (R/golem-yaml-utils.R#L4-L12) only sets the tag attribute on tag[[1]], so every expression after the first one in a section loses its !expr tag. YAML comments are also dropped by yaml::write_yaml().

To Reproduce

library(golem)
p <- file.path(
  tempdir(),
  "yamlapp"
)
create_golem(
  p,
  open = FALSE,
  check_name = FALSE
)
cfg <- file.path(
  p,
  "inst",
  "golem-config.yml"
)
write(
  "  db_host: !expr Sys.getenv('DB_HOST', 'localhost')",
  cfg,
  append = TRUE
)
amend_golem_config(
  key = "my_key",
  value = "my_value",
  golem_wd = p
)
cat(
  readLines(cfg),
  sep = "\n"
)

Before:

dev:
  golem_wd: !expr golem::pkg_path()

  db_host: !expr Sys.getenv('DB_HOST', 'localhost')

After:

dev:
  golem_wd: !expr golem::pkg_path()
  db_host: Sys.getenv('DB_HOST', 'localhost')

db_host is now the literal string "Sys.getenv('DB_HOST', 'localhost')".

Expected behavior

All !expr values are preserved.

Additional context

Tagging every element fixes the reproducible example above (tested by patching the namespace):

add_expr_tag <- function(
  tag
) {
  for (i in seq_along(tag)) {
    attr(
      tag[[i]],
      "tag"
    ) <- "!expr"
  }
  tag
}

Remaining limitations worth a test: expressions nested deeper than one level are not detected by find_and_tag_exprs() (one-level %in% comparison), and comments are lost on every write.

session info

R 4.3.3, Ubuntu 24.04, {golem} master (9938a41, version 1.0.1).


Generated by Claude Code

Dominant language
R
Stars
947
Forks
135
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ThinkR-open/golem

All issues in ThinkR-open/golem

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.