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

Yo doesn't check parent directories for yo-rc.json #1291

Open
juliankrieger opened this issue Apr 28, 2021 · 4 comments
Open

Yo doesn't check parent directories for yo-rc.json #1291

juliankrieger opened this issue Apr 28, 2021 · 4 comments

Comments

@juliankrieger
Copy link

Type of issue: Bug

My environment

  • OS version/details: MacOs 11.13
  • Node version: 14.16.1 (run node --version in your terminal)
  • npm version: 6.14.12 (run npm --version in your terminal)
  • Version of yo : 4.0.0 (run yo --version in your terminal)

Expected behavior

Yo correctly determines tmp (or whatever folder it was first run in) as the project root, and places files correctly even when run from a subdirectory. The subdirectory is not mistakenly marked as a new project root, the subdirectory does not mistakenly include a yo-rc.json itself.

Current behavior

  1. Instead of creating new files in tmp/files, the following file structure occurs
tmp
│   yo-rc.json    
└───files
│       │   <files here>
└───subdir
│   │   yo-rc.json
│   └───subfolder
│       │   <files here>

Steps to reproduce the behavior

  1. Set up simple project
  2. Create a "new" BaseGenerator, which all other generators extend from
  3. Write an initialise function as follows
_initialize() {
        const defaults: Config = {
          // Some defaults here
        }

        this.config.defaults(defaults)
    }
  1. In each Generator or Subgenerator, call _initialise in the initializing context. Generators create files either with this.destinationRoot or any configuration option in the config
  2. Setup Generator with npm link
  3. Create a directory, we'll call it tmp
  4. Run generator in tmp
  5. Observe it working as intended. Files are created in tmp/files. Config file yo-rc.json is created in tmp, marking project root
  6. Now, create a subdirectory, tmp/subdir
  7. Cd into subdir
  8. Run Generator again
  9. Observe wrong behaviour, as seen above
@mshima mshima transferred this issue from yeoman/yo May 2, 2021
@mshima
Copy link
Member

mshima commented May 2, 2021

This is the [email protected] behavior.
While there are some use cases it may make sense, with workspaces it doesn't.

workspaces
│   yo-rc.json   (generator-workspaces)
└───packages
│   └───package1 (generator-node)
│       └   yo-rc.json
│   └───package2 (generator-node)
│       └   yo-rc.json

@juliankrieger
Copy link
Author

@mshima Is there any way to get the intended behaviour? Otherwise, the whole deal with yo-rc.json doesn't seem to make sense to me, or the documentation about it is outdated

@mshima
Copy link
Member

mshima commented May 3, 2021

Install and load 'find-up' package

Add at your constructor something like:

const rootPath = findUp.sync('.yo-rc.json', {
  cwd: this.destinationRoot()
});
if (rootPath) {
  this.destinationRoot(rootPath);
}

@irvingmoar
Copy link

irvingmoar commented Apr 29, 2022

I used the solution suggested by @mshima and found an incompleteness: the rootPath value at the end includes the '.yo-rc.json' filename. Consider removing it before passing rootPath to this.destinationRoot, or you will get an error on writing.
Also keep in mind that since version 6.0.0 find-up uses ESM. The last CJS version of it is 5.0.0.

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

No branches or pull requests

3 participants