-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Prevent npm from ignoring .gitignore #36
Conversation
It worked! However, contents of `.gitignore` need to be mirrored in `.npmignore` in order for NPM to follow the same exclusions and prevent generated project files from being packed.
templates/boilerplate/.npmignore
Outdated
/.cache | ||
|
||
# Prevent NPM from ignoring the .gitignore file | ||
!.gitignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add a line break in the end to avoid that minus.
@@ -1,5 +1,7 @@ | |||
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files | |||
|
|||
## ATTENTION: Files ignored here must be mirrored in the sibling .npmignore file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this not a problem with other package managers such as yarn
or bun
? Is it because we hit this problem only while using npx
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rakeshpetit this is an issue with the npm package registry when packaging the library and publishing to npm. Yarn and bun don’t have package registries, so are not relevant here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this file gets copied into new apps generated by Belt, will this comment cause confusion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed writeup and for tackling this.
This approach seems to work well, though I have some concerns about having these three files (boilerplate/.gitignore
, boilerplate/.npmignore
, .gitignore
), each with similar but slightly different purposes. I'm worried that this will become too difficult to reason about:
boilerplate/.gitignore
: the gitignore for the generated app. This also has a secondary function as a gitignore for theboilerplate
directory in this repoboilerplate/.npmignore
: tells npm which files to ignore when packaging, main purpose of this is to tell it to include .gitignore which otherwise would be excluded. Have to include everything that would normally be in.gitignore
or else we run the risk of accidentally packaging unintended files, even whengit status
shows a clean working copy. This seems like a big danger..gitignore
: this file in the root can specify additional files to ignore from theboilerplate
directory in the Belt repo. Right now, we use this to specify files that we'd want to be committed in a generated app but not in the Belt repo (eg. package lock files)
My concerns over managing the .npmignore
and the risk of accidentally packaging unintended files even when git shows a clean working copy make me inclined to want to find an alternative solution. Maybe this is still the best way forward, but I am interested in seeing if there might be another option.
Are there any other solutions that come to mind for this? I can do some exploration as well.
@@ -1,5 +1,7 @@ | |||
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files | |||
|
|||
## ATTENTION: Files ignored here must be mirrored in the sibling .npmignore file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this file gets copied into new apps generated by Belt, will this comment cause confusion?
templates/boilerplate/.npmignore
Outdated
@@ -0,0 +1,45 @@ | |||
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the .gitignore
gets copied to user apps and therefore isn't an ideal place to document the behavior we're fixing, what would you think about instead documenting the purpose of this file and instructions for how to use it as a comment here?
I wanted to add some additional context and propose a potential solution. Context
The Downsides of npmignoreThe main downsides I see are that it introduces potential for accidentally packaging unintended files because it is difficult to keep in sync with ProposalWhat if we keep the Does this seem like it would solve the issues? |
Unfortunately, it seems that this approach will not work after all, details below. I just pushed up the change to dynamically generate the
The output of the npm publish looked good and indicated that the I then ran ![]() even though npm shows that the .gitignore file is correctly published with v0.3.1 of the package, and that there is no This seems to be documented in the linked issue thread from the PR description as well as in npm/npm#12917 and npm/npm#7252. It seems that even though we're packaging the file correctly, |
I started playing around with an alternative approach:
This seems to be working but will need some cleanup, code is here: https://github.com/thoughtbot/belt/compare/fix-gitignore...sh/fix-gitignore?expand=1. Feel free to merge it into this branch, or I can do that. Right now if you try to commit and the |
I'll go ahead and assign myself on this and get the fix in place. |
Closing in favor of #39 |
This PR supercedes #36. See that ticket for full context. At a high level, the issues are: * Our app boilerplate includes a `.gitignore` file that should be copied for all new apps * This `.gitignore` file also needs to be in place so the git repo ignores certain files in `templates/boilerplate` _and_ so NPM doesn't package those same files with the package * NPM does not by default allow including a `.gitignore` file in a package * Even though we eventually found a way to add the `.gitignore` in the raw package, NPM renames this to `.npmignore` when installing our package with `npm install` Solution: * Rename `templates/boilerplate/.gitignore` to `templates/boilerplate/.gitignore.eta` * When copying the boilerplate for new apps, the `.eta` suffix will automatically be removed. This takes advantage of the ETA template parsing that is already implemented in Belt * Also include `templates/boilerplate/.gitignore`, so that git and npm properly ignore the specified files * To ensure that `.gitignore` and `.gitignore.eta` stay in sync, add a `pre-commit` Husky hook that validates the files match. If they do not match, instruct the developer to run `node bin/generate-gitignore.js` to re-generate `.gitignore` from `.gitignore.eta` * I opted to make this manual and not have the pre-commit hook auto-generate the `.gitignore` because I didn't want it to inadvertently overwrite changes someone had accidentally made to `.gitignore`. Here is a [🎥 video walkthrough](https://www.loom.com/share/8d0b82fe01094295ac8fcce5afc7e9fa?sid=eff2f5d0-e397-4663-939b-327b9f0c9636) as well where I explain this all. --------- Co-authored-by: Diego Oliveira <[email protected]>
Issue
From this Slack thread:
Options
.gitignore.eta
or other template naming!.gitignore
"templates/boilerplate/.gitignore"
explicitly to thepackage.json
'sfiles
array..npmignore
to root and/or boilerplate folder!.gitignore
.gitignore
need to be mirrored in.npmignore
for NPM to follow the same exclusions and prevent generated project files from being packed..npmignore
as!**/.gitignore
to make NPM keep any.gitignore
file but that didn't work. To keep them the.npmignore
file has to be in the same folder (sibling file).Solution
I went with solution 5.2 as it uses the built-in
.npmignore
functionality, adds less complexity to the project over other options, and resolves the issue as designed by NPM.