[BUG] create_golem() doesn't restore the working directory when the hook fails, and `with_git` fails on paths with spaces

Open Beginner friendly
#1,258 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
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
r
Domain
devtools

Research direction

The two problems sit in R/create_golem.R: the setwd(path_to_golem) call near line 140 needs a restore that runs even when project_hook() errors, and the system(paste("git init", ...)) call near line 175 needs the path quoted. Run the reproduction in the issue to confirm both behaviours. Done means the working directory is restored after a failing hook and git init succeeds for a path containing spaces.

Written by the indexing model from the issue text.

Description

bug

Describe the bug

Two issues in create_golem():

  1. The working directory is changed with old <- setwd(path_to_golem) (R/create_golem.R#L140) and restored with a plain setwd(old) later. If project_hook() errors, the user is left inside the new project folder.
  2. with_git = TRUE runs system(paste("git init", path_to_golem)) (R/create_golem.R#L175) without quoting the path, so it fails whenever the path contains a space (common on macOS/Windows, e.g. ~/Mes Documents/).

To Reproduce

library(golem)
setwd(tempdir())
try(
  create_golem(
    file.path(
      tempdir(),
      "app_hook"
    ),
    open = FALSE,
    check_name = FALSE,
    project_hook = function(path, package_name, ...) stop("boom")
  )
)
getwd()
# with_git and a space in the path
p <- file.path(
  tempdir(),
  "dir with space",
  "app_git"
)
dir.create(dirname(p))
create_golem(
  p,
  open = FALSE,
  check_name = FALSE,
  with_git = TRUE
)
dir.exists(
  file.path(
    p,
    ".git"
  )
)

Output:

Error in project_hook(path = path_to_golem, package_name = package_name,  : 
  boom
[1] "/tmp/RtmpfiwYQq/app_hook"
x Error initializing git repository.
[1] FALSE

Expected behavior

  1. The working directory is restored even when the hook fails.
  2. The git repository is initialized regardless of spaces in the path.

Additional context

Suggested fixes: on.exit(setwd(old), add = TRUE) (or withr::with_dir()), and system2("git", c("init", shQuote(path_to_golem))).

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.