quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.89k stars 320 forks source link

quarto use template --no-prompt only works in empty directory #11127

Open allefeld opened 4 days ago

allefeld commented 4 days ago

Bug description

Interactively, it is possible to install a template into a directory which already contains files, even if one accepts the defaults:

$ quarto use template allefeld/ext-quarto/r-utils

Quarto templates may execute code when documents are rendered. If you do not 
trust the authors of the template, we recommend that you do not install or 
use the template.
? Do you trust the authors of this template (Y/n) › Yes
? Create a subdirectory for template? (Y/n) › Yes
? Directory name: › 
[✓] Downloading
[✓] Unzipping

Preparing template files...
[✓] Copying files...

Files created:
 - statsutils.R

With --no-prompt, under the same circumstances an error occurs:

$ quarto use template --no-prompt allefeld/ext-quarto/r-utils
ERROR: Unable to install in /home/ca/work/Projects/[censored] as the directory isn't empty.

Stack trace:
    at determineDirectory (file:///opt/quarto/bin/quarto.js:109925:19)
    at useTemplate (file:///opt/quarto/bin/quarto.js:109773:39)
    at eventLoopTick (ext:core/01_core.js:153:7)
    at async Command.actionHandler (file:///opt/quarto/bin/quarto.js:109760:9)
    at async Command.execute (file:///opt/quarto/bin/quarto.js:8017:13)
    at async Command.parseCommand (file:///opt/quarto/bin/quarto.js:7907:20)
    at async quarto (file:///opt/quarto/bin/quarto.js:118224:9)
    at async file:///opt/quarto/bin/quarto.js:118244:9
    at async mainRunner (file:///opt/quarto/bin/quarto.js:118128:9)
    at async file:///opt/quarto/bin/quarto.js:118235:5

Steps to reproduce

Run quarto use template --no-prompt in a directory which contains files.

Expected behavior

quarto use template --no-prompt should behave the same as interactively with accepting the default options.

Actual behavior

see above

Your environment

Debian 12.7

Quarto check output

Quarto 1.5.57
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.2.0: OK
      Dart Sass version 1.70.0: OK
      Deno version 1.41.0: OK
      Typst version 0.11.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.5.57
      Path: /opt/quarto/bin

[✓] Checking tools....................OK
      TinyTeX: (not installed)
      Chromium: (not installed)

[✓] Checking LaTeX....................OK
      Using: Installation From Path
      Path: /usr/bin
      Version: 2022

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK
      Version: 3.11.2
      Path: /usr/bin/python3
      Jupyter: (None)

      Jupyter is not available in this Python installation.
      Install with python3 -m pip install jupyter

[✓] Checking R installation...........OK
      Version: 4.4.1
      Path: /usr/lib/R
      LibPaths:
        - /home/ca/Store/R
        - /usr/local/lib/R/site-library
        - /usr/lib/R/site-library
        - /usr/lib/R/library
      knitr: 1.48
      rmarkdown: 2.27

[✓] Checking Knitr engine render......OK
cderv commented 4 days ago

I believe this is by designed from 0f5586d018d0a51ba4528e43c8d30f597a5be8a1 https://github.com/quarto-dev/quarto-cli/blob/2bc956a668ceba50ca47a3975f0ebf10b763ac13/src/command/use/commands/template.ts#L303-L317

We explicitly error when --no-prompt and directory is not empty to prevent any accidental override that would be destructive. This is why we prompt user. Initially, it was only working in empty directory. Quarto allows non empty directory, but only after prompt validation.

Changing this would be a change in behavior to reverse completely our assumptions. This is a change that should be carefully consider. Is there a use case where this is completely blocking you ?

General rule for us is usually to have explicit user action (prompt or empty dir + copy elsewhere for example), and to avoid as much as possibly implicit that could be harmful.

allefeld commented 4 days ago

I understand. Though in the case I tested, the process would not have been destructive: There were other files, but not the ones written by quarto use template from this specific template.

Then I propose, as an enhancement, that the answers to the questions can be specified on the command line, e.g. by extending the syntax of --no-prompt. Reason: I'd like to use this command in a script.

I have to admit that my intended use is a slight abuse: Not to initialise a project, but to add to it resusable files. It would be great if that would remain possible, including non-interactively, because there is no native way to download all files from a GitHub repository subdirectory.