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

Support install not in .git root #103

Open
jods4 opened this issue Feb 12, 2024 · 4 comments
Open

Support install not in .git root #103

jods4 opened this issue Feb 12, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@jods4
Copy link

jods4 commented Feb 12, 2024

Details

Currently, husky checks that it's being installed in the repository root, i.e. cwd contains a .git folder.

Some repository contain more than a .net solution, it's not uncommon to have other folders at root such as docs, infra, front-end, etc. In our setup, dotnet solution and .config local tools live in a Source folder, under root.

The issue with this is that:

  1. Local dotnet tool can't run outside Source because they're local and do not exist outside of that folder.
  2. Local tool Husky can't run inside Source because it requires .git in the same folder where it runs.

As 1. and 2. are mutually exclusive, there's no working configuration :(

Note

Husky -- the NPM project that inspired Husky.Net -- has a "How to" for this topic in its documentation:
https://typicode.github.io/husky/how-to.html#project-not-in-git-root-directory

Their solution is simply to change working directory appropriately in install script and hook.
The blocker is that whereas cd .. & husky works in NPM scripts, there's no way parameter to change the working directory in dotnet run, so husky init can't be run in a different cwd.

Suggested solution

Add a --cwd parameter to install command, so that the working directory can be changed before it runs:

dotnet husky install --cwd ../..

The reverse cd command should be inferred automatically (using Path.GetRelativePath) and introduced in hook script so that they run in the correct folder (otherwise dotnet husky & other local .net tool tasks will not be found).

@jods4 jods4 added the enhancement New feature or request label Feb 12, 2024
@alirezanet
Copy link
Owner

alirezanet commented Feb 12, 2024

Hi @jods4,
have you tried using the install's --dir option, you can move your .husky folder to the subdirectories if needed, just for the first time installing husky to the git repository you have to do it from the root folder. (if you don't want to add the .config folder in your root folder, you can install husky.net globally ). maybe I'm missing the point, in that case would be nice if you could provide a test project with your desired layers/folders ...

@jods4
Copy link
Author

jods4 commented Feb 12, 2024

Thanks for the quick answer!

--dir doesn't help. I'm ok with having the folder at the root next to .git, or lower in Source. Both would be fine.

husky install does not even run because of this safety guard: https://github.com/alirezanet/Husky.Net/blob/master/src/Husky/Cli/InstallCommand.cs#L73-L79

@bdarlt
Copy link

bdarlt commented Feb 15, 2024

I have a monorep with both an Angular app (/web) and a .Net solution (/api).

What mostly works is installing Husky.Net into the root folder (with the .git directory). You can use "cwd" in some way to pick the part of the monorep you want.

The prepare script of the Angular App (/web) installs/activates husky. I haven't figured out a way to "activate" Husky on the .NET side. Missing a monorep tool, I suppose.

{
  "tasks": [
    {
      "name": "dotnet-format",
      "cwd": "api",
      "group": "pre-commit",
      "command": "dotnet",
      "args": ["format", "--include", "${staged} --severity error"],
      "include": ["**/*.cs", "**/*.vb"]
    },
    {
      "name": "eslint",
      "group": "pre-commit",
      "pathMode": "absolute",
      "cwd": ".",
      "command": "npx",
      "args": ["lint-staged", "-r", "--cwd", "web"],
      "include": ["**/*.ts", "**/*.js"]
    },
    {
      "name": "prettier",
      "group": "pre-commit",
      "pathMode": "absolute",
      "cwd": ".",
      "command": "npx",
      "args": ["prettier", "--write", "${staged}"],
      "include": [
        "**/*.ts",
        "**/*.vue",
        "**/*.jsx",
        "**/*.js",
        "**/*.json",
        "**/*.yml",
        "**/*.css",
        "**/*.scss"
      ]
    },
    {
      "name": "Welcome",
      "output": "always",
      "command": "bash",
      "args": ["-c", "echo Nice work! 🥂"],
      "windows": {
        "command": "cmd",
        "args": ["/c", "echo Nice work! 🥂"]
      }
    }
  ]
}

@jods4
Copy link
Author

jods4 commented Feb 16, 2024

What mostly works is installing Husky.Net into the root folder

Yes, this is the requirement that I would like to see lifted with this issue.

Depending on your repo structure, which may be outside your control, it's not always ok to add .net tools at the root of the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants