jacobbien / litr-project

Writing R Packages with Literate Programming

Home Page:https://jacobbien.github.io/litr-project/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Duplicate code chunk labels

sangwon-hyun opened this issue · comments

If you extract example.zip and place the attached three files in a directory and run:

litr::render("index.Rmd", output_format = litr::litr_gitbook())

You get an error that says:

> litr::render("index.Rmd", output_format = litr::litr_gitbook())

processing file: _main.Rmd
Error in hash_package_directory(package_dir) : 
  Cannot find DESCRIPTION file.
Calls: local ... process_file -> split_file -> lapply -> FUN -> parse_block
In addition: Warning message:
In normalizePath(file.path(package_dir, "DESCRIPTION")) :
  path[1]="./frombookdown/DESCRIPTION": No such file or directory
Please delete _main.Rmd after you finish debugging the error.
Execution halted
Error: Failed to run 'with_cleanup(bookdown::render_book, package_dir)' in a new R session.

First of all, this is not an accurate description of the error; the fault is in 2serious-error.Rmd, where two code blocks have the same label!

But once this happens, the new files cause additional and persistent trouble! The only way I've found to successfully liter::render() again is to delete everything new that was created -- (1) _main.Rmd, (2) ./_book, and (3) ./frombookdown. Deleting only a subset of the three doesn't work.

I saw that the immediate cause of the above error was the creation of an empty package in ./frombookdown, since hash_package_directory() seems to be looking for a DESCRIPTION file that doesn't exist. (Maybe an empty package shouldn't be created in any situation!). I attempted to fix this by simply deleting that directory, but then I got:

> litr::render("index.Rmd", output_format = litr::litr_gitbook())
Error: [ENOENT] Failed to search directory './frombookdown': no such file or directory
Execution halted
Error: Failed to run 'with_cleanup(bookdown::render_book, package_dir)' in a new R session.

whose traceback output is:

> traceback()
3: stop(fail, call. = FALSE)
2: xfun::Rscript_call(with_cleanup(bookdown::render_book, package_dir), 
       c(input = input, args))
1: litr::render("index.Rmd", output_format = litr::litr_gitbook())

So, just deleting that directory doesn't seem to resolve this. You have to delete all three aforementioned files.

tldr: I'd like to suggest a more graceful exit for duplicated code block labels e.g. check for duplicated labels up front, warn the user, and simply abort without creating any new files.

Thanks for a very helpful description of the problem. It appears that the problem is occurring in the error handler in with_cleanup(), which calls write_hash_to_description() assuming that the DESCRIPTION file has already been created. The fix to this issue will be for the error handler to first check to see if the DESCRIPTION file exists. If it does not, then perhaps it can create a DESCRIPTION file that contains nothing more than the litr hash.

One this fix is made, the error message should be informative (since it won't be about the litr hash being written but rather about the root cause of the error in the first place, which in this case has to do with two chunks having the same label).

This is fixed by 90e29de