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

wrong default file path in run_dev() documentation, file path hard coded #886

Closed
Teebusch opened this issue Jul 18, 2022 · 11 comments
Closed
Milestone

Comments

@Teebusch
Copy link

Teebusch commented Jul 18, 2022

The run_dev() docstring says that the default path for the project's run_dev.R file is R/run_dev(),

#' @param file File path to `run_dev.R`. Defaults to `R/run_dev.R`.

but the default is actually set to dev/run_dev():
file <- "dev/run_dev.R"

If the user supplies a file argument, the function checks whether that file exists but then goes on to eval the content of dev/run_dev.R anyway

run_dev_lines <- readLines("dev/run_dev.R")


I can't make a PR right now or test this, but I think this should fix it (assuming that the default location of run_dev.R is supposed to be in dev):

#' Run run_dev.R
#'
#' @param file File path to `run_dev.R`. Defaults to `dev/run_dev.R`.
#' @param pkg Package name to run the file. Defaults to current active package.
#'
#' @export
#'
#' @return Used for side-effect
run_dev <- function(
  file,
  pkg = pkgload::pkg_name()
) {
  if (missing(file)) {
    file <- "dev/run_dev.R"
  }

  if (pkgload::is_dev_package(pkg)) {
    run_dev_lines <- readLines(
      file.path(
        pkgload::pkg_path(),
        file
      )
    )
  } else {
    try_dev <- file.path(
      pkgload::pkg_path(),
      file
    )

    if (file.exists(try_dev)) {
      run_dev_lines <- readLines(try_dev)
    } else {
      stop("Unable to locate dev file")
    }
  }

  eval(parse(text = run_dev_lines))
}

Let me know if you agree with this solution and I will make a PR.

@ALanguillaume
Copy link
Member

Hi @Teebusch,
Thanks a lot for reporting this and fixing our mistakes.
Go ahead with a PR !

@ALanguillaume
Copy link
Member

Make sure to start from the dev branch, though, as specified in CONTRIBUTING.md

https://github.com/ThinkR-open/golem/blob/dev/R/run_dev.R

@ColinFay
Copy link
Member

Thanks a lot, this is indeed a typo in the documentation.

As far as I can tell from re-reading the code, if you supply another path to a dev file (say, dev/run_dev2.R for example) the function will read this one (because the arg is not missing() anymore), so the behavior is correct here, unless I'm missing something?

That being said, I would suggest moving to a clearer option where the param has the default value file = "dev/run_dev.R".

Let us know if you feel like doing a PR.

Cheers,
Colin

@VincentGuyader
Copy link
Member

I'm thinking about something
do we have to set here::here("dev/run_dev.R") to be more defensive ?
(what if someone using fusen launch golem::run_dev() from dev/flat_file.Rmd)

@Teebusch
Copy link
Author

Teebusch commented Jul 18, 2022

As far as I can tell from re-reading the code, if you supply another path to a dev file (say, dev/run_dev2.R for example) the function will read this one (because the arg is not missing() anymore), so the behavior is correct here, unless I'm missing something?

@ColinFay it does use the user-supplied value to build a file path (ln. 25) but then reads the content of dev/run_dev.R anyways, because it is hard coded (ln. 31).

 try_dev <- file.path(
    pkgload::pkg_path(),
    file
  )

  if (file.exists(try_dev)) {
    run_dev_lines <- readLines("dev/run_dev.R")   # this right here
  } else {
    stop("Unable to locate dev file")
  }

@Teebusch
Copy link
Author

Teebusch commented Jul 18, 2022

I'm thinking about something do we have to set here::here("dev/run_dev.R") to be more defensive ? (what if someone using fusen launch golem::run_dev() from dev/flat_file.Rmd)

@VincentGuyader Sounds like a good idea. In addition, the function does not always check if the file exists before trying to read it. Only if is_dev_package() is false. This will throw an error if the file does not actually exist. Is there a good reason to assume that the file will always exist in this case and one can skip the test?

if (pkgload::is_dev_package(pkg)) {
  run_dev_lines <- readLines(    # immediately tries to read file w/o checking existence
    file.path(
      pkgload::pkg_path(),
      file
    )
  )
} else {
# ...
}

@Teebusch
Copy link
Author

Teebusch commented Jul 18, 2022

Here's a suggestion with some additional changes. I'll make a PR later. [Edit: using get_golem_wd(), as seen on the dev branch]

  • always check if file exists (nice side effects: removes one if-else)
  • use here::here() instead of file.path()
  • update docstring title to reflect that the dev file an be called anything, not just "run_dev.R"
#' Run dev file
#'
#' @param file File path to dev file. Defaults to `dev/run_dev.R`.
#' @inheritParams add_module
#'
#' @export
#'
#' @return Used for side-effect
run_dev <- function(
    file = "dev/run_dev.R",
    pkg = get_golem_wd()
) {
  f <- here::here(pkg, file)
    
  if (!file.exists(f)) {
    stop("Unable to locate dev file")
  } else {
    dev_lines <- readLines(f)
    eval(parse(text = dev_lines))
  }
}

@ColinFay
Copy link
Member

That sounds awesome 👏

Just a note: there is no need to here::here() the path, as get_golem_wd() is already returning the output of here::here() :)

@ColinFay ColinFay added this to the Version 0.5.0 milestone Dec 15, 2022
ilyaZar added a commit to ilyaZar/golem that referenced this issue Jun 2, 2023
- use path constructed from 'file' argument instead of hard code "dev/run_dev.R"
- improve error message on failure

Refs: ThinkR-open#886
ilyaZar added a commit to ilyaZar/golem that referenced this issue Jun 5, 2023
- use path constructed from 'file' argument instead of hard code "dev/run_dev.R"
- improve error message on failure

Refs: ThinkR-open#886
VincentGuyader pushed a commit that referenced this issue Aug 8, 2023
…ev.R` (#1050)

* fix: user supplied 'file'-argument works correctly

- use path constructed from 'file' argument instead of hard code "dev/run_dev.R"
- improve error message on failure

Refs: #886

* tests: improve test-covr. of run_dev.R form 0% to 100%

- add test for default 'file' argument
- add test for user supplied 'file' argument where
    - run_dev.R renamed to run_dev2.R
    - path to run_dev changed to top-level golem path
- add test for error if run_dev cannot find the 'file'

* docs: update docs for run_dev

- give a bit more context on usage
- fix some typos
- use markdown highlighting consistently
- add invisible return as this is a pure side-effect function
VincentGuyader added a commit that referenced this issue Aug 8, 2023
* add user supplied `file` for `run_dev()` & make covr. 100% for `run_dev.R`  (#1050)

* fix: user supplied 'file'-argument works correctly

- use path constructed from 'file' argument instead of hard code "dev/run_dev.R"
- improve error message on failure

Refs: #886

* tests: improve test-covr. of run_dev.R form 0% to 100%

- add test for default 'file' argument
- add test for user supplied 'file' argument where
    - run_dev.R renamed to run_dev2.R
    - path to run_dev changed to top-level golem path
- add test for error if run_dev cannot find the 'file'

* docs: update docs for run_dev

- give a bit more context on usage
- fix some typos
- use markdown highlighting consistently
- add invisible return as this is a pure side-effect function

* core update README

---------

Co-authored-by: Ilya Zarubin <[email protected]>
@odysseyjake
Copy link

Hi folks...just noting that this is still an issue in the currently released CRAN version of the package 0.4.1. When is a new version going to be released?

@ColinFay
Copy link
Member

@odysseyjake currently working on a new release, so I suppose in november :)

@ColinFay
Copy link
Member

That being said, as it is now in the dev branch, I'll close this issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants