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

Initializing a project with a submodule fails if directory exists #839

Open
abizjak opened this issue Jan 5, 2023 · 1 comment
Open

Comments

@abizjak
Copy link

abizjak commented Jan 5, 2023

Describe the bug

Suppose the template to generate from is in a repository b that has a git submodule a as a dependency.

So the repository b looks like the following (assuming ahas a filefoo`).

├── b
│   └── a
│       └── foo

Now suppose we wish to initialize a project using cargo generate in /root where

/root
    └── a
        └── somefile

that is, root has a non-empty subdirectory called a.

This fails with the error.

Caused by:
    'a' exists and is not an empty directory; class=Invalid (3); code=Exists (-4)

To Reproduce
Steps to reproduce the behavior:

  1. cargo generate <WHAT DID YOU RUN>
  2. output it generated..
  3. See error

Expected behavior

cargo generate succeeds and offers the usual options.

Desktop (please complete the following information):

  • OS: Linux
  • Version: v0.5.0-453-g51deb01
  • Build tools: gcc
  • cargo generate --version: 0.17.4

** Root cause **

The root cause of this is the following issue in libgit2 libgit2/libgit2#5830 .
Essentially during the cloning of the submodule there is an incorrect check which checks in the current working directory whether the directory a exists even though that is not relevant.

It is possible to work around the issue in cargo-generate by modifying the clone_with_submodules function to change the working directory

    pub fn clone_with_submodules(self, dest_path: &Path) -> Result<Repository> {                                                                                                                 
        self.clone(dest_path).and_then(|repo| {
            // set the working directory to the root of the newly created repository
            // this works around the issue https://github.com/libgit2/libgit2/issues/5830
            // in libgit2                                                                                                                                                  
            std::env::set_current_dir(dest_path)?;                                                                                                                                               
            for mut sub in repo.submodules()? {                                                                                                                                                  
                sub.update(true, None)?;                                                                                                                                                         
            }                                                                                                                                                                                    
                                                                                                                                                                                                 
            Ok(repo)                                                                                                                                                                             
        })                                                                                                                                                                                       
    }

Seeing that the libgit2 has not resolved the issue in more than a year, would it be acceptable to this project to work around the issue here? I'm more than happy to contribute a PR.

@sassman
Copy link
Member

sassman commented Jan 7, 2023

I guess the proposed workaround is fine, but IMO it is important to restore the CWD to it's previous value in order to minimize side effects as much as possible.

Update: feel free and very welcome to open a PR for it.

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

2 participants