golem icon indicating copy to clipboard operation
golem copied to clipboard

golem::document_and_reload is not using get_golem_config to figure out where the golem configuration is

Open harell opened this issue 2 years ago • 6 comments

Hi

Thanks for the great work.

Given I have moved the default "./inst/golem-config.yml" to "./inst/config/golem.yml" AND declare the configuration file path in "/R/get_golem_config.R" When I run "./dev/run_dev.R" Then script fails to find the config file in the updated address

Failure Message

image

get_golem_config

get_golem_config <- function(
  value,
  config = Sys.getenv("GOLEM_CONFIG_ACTIVE", Sys.getenv("R_CONFIG_ACTIVE","default")),
  use_parent = TRUE,
  # Modify this if your config file is somewhere else
  file = app_sys("config/golem.yml")
) {
  config::get(
    value = value,
    config = config,
    file = file,
    use_parent = use_parent
  )
}

Note I updated file = app_sys("config/golem.yml") from its original file = app_sys("golem-config.yml")

Failure Traceback

image

  • None of the functions above uses get_golem_config to retrieve information from the configuration file.

Please advise how to change the location of the yml file and run /run_dev.R successfully.

harell avatar Jul 22 '22 02:07 harell

Hi @harell,

Thanks for reporting this.

TLDR: Currently you cannot change the location of inst/golem-config.yml despite what the comment in get_golem_config() says. Apologies for that. We will try to make it happen or at least less confusing in the future versions of golem.

Indeed, despite the comment in the code of get_golem_config() (R/app_config.R)

# Modify this if your config file is somewhere else
  file = app_sys("config/golem-config.yml")

It is not currently possible to change the location of the golem-config.yml.

Doing so will break a number of golem functions that rely on golem-config.yml to get information about the current golem project. For example in your case the problem arises when document_and_reload() tries to determine the current working directory.

document_and_reload()
  |-> get_golem_wd()
    |-> get_golem_things()
      |-> get_current_config()

In current get_current_config() the path to inst/golem-config.yml is hardcoded.

Here is a reprex of that issue:

# Using golem last dev version
packageVersion("golem")
#> [1] '0.3.2.9004'

path_dummy_golem <- tempfile(pattern = "dummygolem")

golem::create_golem(
  path = path_dummy_golem,
  open = FALSE
)
#> ── Checking package name ───────────────────────────────────────────────────────
#> ✔ Valid package name
#> ── Creating dir ────────────────────────────────────────────────────────────────
#> ✔ Creating '/tmp/RtmpFJejPj/dummygolem74271f138c71/'
#> ✔ Setting active project to '/tmp/RtmpFJejPj/dummygolem74271f138c71'
#> ✔ Creating 'R/'
#> ✔ Writing a sentinel file '.here'
#> • Build robust paths within your project via `here::here()`
#> • Learn more at <https://here.r-lib.org/>
#> ✔ Setting active project to '<no active project>'
#> ✔ Created package directory
#> ── Copying package skeleton ────────────────────────────────────────────────────
#> ✔ Copied app skeleton
#> ── Setting the default config ──────────────────────────────────────────────────
#> ✔ Configured app
#> ── Running project hook function ───────────────────────────────────────────────
#> ✔ All set
#> ✔ Setting active project to '/tmp/RtmpFJejPj/dummygolem74271f138c71'
#> ── Done ────────────────────────────────────────────────────────────────────────
#> A new golem named dummygolem74271f138c71 was created at /tmp/RtmpFJejPj/dummygolem74271f138c71 .
#> To continue working on your app, start editing the 01_start.R file.

old_wd <- setwd(path_dummy_golem)

# Works fine
golem::document_and_reload()
#> Setting `RoxygenNote` to "7.2.0"
#> ℹ Loading dummygolem74271f138c71
#> Writing 'run_app.Rd'
#> ℹ Loading dummygolem74271f138c71

# Move config file
dir.create(
  "inst/config"
)
file.copy(
  from = "inst/golem-config.yml",
  to = "inst/config/golem-config.yml"
)
#> [1] TRUE
file.remove(
  "inst/golem-config.yml"
)
#> [1] TRUE

# {golem} cannot find golem-config.yml
golem::document_and_reload()
#> Error in get_current_config(path, set_options = TRUE): The golem-config.yml file doesn't exist.

# Edit R/app_config.R won't help
path_app_config <- "R/app_config.R"
writeLines(
  sub(
    pattern = "golem-config\\.yml", 
    replacement = "config/golem-config.yml",
    readLines(path_app_config)
  ),
  path_app_config
)
pkgload::load_all()
#> ℹ Loading dummygolem74271f138c71
golem::document_and_reload()
#> Error in get_current_config(path, set_options = TRUE): The golem-config.yml file doesn't exist.

# The error is triggered by which remains agnostic
# of the change in location of golem-config.yml
golem::get_golem_wd()
#> Error in get_current_config(path, set_options = TRUE): The golem-config.yml file doesn't exist.

# This will also also append with other functions trying to 
# access golem-config.yml
golem::get_golem_name()
#> Error in get_current_config(path, set_options = TRUE): The golem-config.yml file doesn't exist.

## The main culprit is golem:::get_current_config() (internal function)
# Where the path to "golem-config.yml" is hardcoded to "inst/golem-config.yml"
golem:::get_current_config()
#> Error in golem:::get_current_config(): The golem-config.yml file doesn't exist.

ALanguillaume avatar Jul 22 '22 11:07 ALanguillaume

@ColinFay I started working on a fix in branch fix-change-config-yml-loc. See thread on our Slack.

ALanguillaume avatar Aug 05 '22 15:08 ALanguillaume

@ALanguillaume are you still planning on working on this?

ColinFay avatar Oct 14 '22 06:10 ColinFay

Whenever golem::document_and_reload() calls get_golem_wd() to retrieve values from a yml-config, and the user changed the destination of that yml before that call, golem::document_and_reload() has no chance to know about that config change in the current setup.

So golem::document_and_reload() must be able to retrieve this information in case sensible default locations for the yaml-config fail to work via guess_where_config().

Currently, the user is asked to make changes to the location of the config-yaml in “R/app_config.R” e.g. like taken from @harrel’s initial bug report:

  # Modify this if your config file is somewhere else
  file = app_sys("config/golem.yml")

So that’s currently the best place for guess_where_config() to search.

Sorry for the prior mess in the now closed PR, but #1043 could be considered instead now.

ilyaZar avatar May 24 '23 12:05 ilyaZar